Create a dynamic design with Angular Material's mat-drawer at full height using flexbox, allowing content

Yesterday, I encountered an interesting CSS problem involving the mat-drawer and Angular's router-outlet. My layout consists of a full-page flexbox with two children: a mat-toolbar at the top, and a custom component called app-sidenav at the bottom. This setup works well as the app-sidenav expands to fill the remaining space due to the flexible nature of the flexbox. Here is a simplified version of the structure:

<div class="flex">
  <mat-toolbar></mat-toolbar>
  <app-sidenav></app-sidenav>
</div>

The relevant CSS styles are:

.flex { width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: stretch; }
mat-toolbar { flex: 0 0 auto; }
app-sidenav { flex: 1 1 0; }

Now, within the app-sidenav component, the template looks like this:

<mat-drawer-container>
  <mat-drawer></mat-drawer>
  <mat-drawer-content>
    <main><router-outlet></router-outlet></main>
  </mat-drawer-content>
</mat-drawer-container>

And the accompanying styles are:

mat-drawer-container, mat-drawer-content { display: block; height: 100%; }
main { height: 100%; overflow-y: auto; }

While everything seems to be working fine, there is one issue. If the content in the app-sidenav component isn't larger than its height, the scrollbar appears on the outer flexbox rather than the main tag. I've tried using !important, setting heights to 100vh, but so far without success. Any ideas on how to make the overflow-y in the main tag function correctly?

I'm sure there's a simple solution to this, but I just can't seem to figure it out right now. Appreciate any help or insights you might have. Thanks!


Edit:

To further illustrate this issue, I've created a StackBlitz demo. When you navigate to the ciao component, you'll notice that the scrollbar appears at the document root instead of within the main tag.

Answer №1

Aside from the solution provided by @Sakkeer, I discovered a different approach that avoids manipulating the position attribute by utilizing flex properties. Simply include (without replacing) the following CSS rules along with the existing styles:

app-sidenav { display: flex; }
mat-drawer-container { flex: 1 1 auto; }

Answer №2

Consider using this as your primary css style

main { 
  position: fixed;
  right: 0;
  left: 0;
  bottom: 0;
  top: 0; 
}

Answer №3

There will be two scrollbars added - one for the main page scroll and another for the mat-drawer that slides in from the left side. The mat-drawer will have its own scrollbar, similar to what is shown in the screenshot.

.mat-drawer.mat-drawer-end {
    position: fixed;
    overflow: auto;
}

https://i.sstatic.net/ziBK6.png

With a long page, you'll see the mat-drawer at the top with its own scrollbar. This should help achieve our desired layout with minimal code required.

Answer №4

Success! I found a solution that worked perfectly for me. Simply add the following code snippet to your style.scss:

.mat-drawer-inner-container{ overflow: hidden !important; }

If the above code doesn't work for you, give this alternative a try in your style.scss:

 *{ overflow: hidden !important; } 

Answer №5

     <mat-drawer-container autosize style="position: absolute;right: 0px;left: 0px;bottom: 0px;top: 0px;">
        <mat-drawer #drawer mode="side" class="toolbar">
            <app-sidenav></app-sidenav>
        </mat-drawer>
        <router-outlet></router-outlet>
     </mat-drawer-container>

The code above worked perfectly for me. By adding the style directly and using autosize, I prevented any overlap of the mat-drawer-container with the router page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cleaning arrays in Angular 2 forms: A step-by-step guide

Need some assistance. I'm developing an app in Angular2(4) which includes a Form with 2 select boxes and 1 checkbox. My goal is to establish a dependency between these boxes using the following array: dataJSON = [ { productName: 'Produ ...

Fixed Navigation Menu on Top with Specific Width Size

Currently diving into the world of CSS and eagerly following a tutorial on creating a responsive menu for my website. Managed to get it up and running, but hit a few roadblocks along the way: The navigation menu seems to shrink in both desktop and mobile v ...

Issue with Animate.css animations not working in my Vue 3 application

My goal is to incorporate some basic HTML animations into my Vue 3 application, however I am encountering issues with the Animate.css examples. More specifically, even after installing the animate css dependency using npm install animate.css --save, the s ...

Even after importing the API key, I am still receiving the message "This domain is not registered with Tiny Cloud."

I have implemented the TinyMCE code provided in their documentation: <editor [init]="{ apikey: '<secret>', height: 500, plugins: [ 'a11ychecker','advlist','advcode','advtable',&apos ...

Why doesn't Angular 6 show the results of Next right away?

Although I don't have the code on hand, this is more of a theoretical question that I believe can still be answered. In my scenario, there is a MsgService that provides messages to components. When I update or set messages in the service from one of ...

Having trouble successfully deploying the app generated by "dotnet new angular" onto Azure

After creating an app using the dotnet new angular template and running dotnet run, everything seemed to be working smoothly. However, when I pushed the code to GitHub and set up continuous deployment in Azure, the build process failed. I turned on Devel ...

Add CSS styling to input text that is not empty

Is it possible to achieve this using CSS3 alone? var inputs = document.getElementsByTagName("INPUT"); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "text") { if (inputs[i].value != "") { inputs[i].s ...

Can anyone recommend a user-friendly JavaScript dialog box compatible with jQuery that is mobile-responsive?

Mobile browsers. Using Jquery dialog alert box. Avoiding the use of alert() due to its unattractive appearance in web browsers. In the process of creating a website for both mobile and web platforms, seeking a dialog box that is compatible with both. ...

Troubleshooting Display Problems when Switching Bootstrap Themes in ASP.NET MVC

I recently made changes to my MVC project by switching the default Bootstrap theme to Bootswatch - Cosmos. However, I am facing an issue with the Navbar as shown in the image below: View Header Display Issue Image Initially, I was using Bootstrap 3.0 and ...

JavaScript is utilized to update HTML content through an Ajax request, but unfortunately, the styling is

Apologies for the unconventional title, I am currently in the process of creating a website. When the page is initially loaded, I call upon two scripts within the 'head' tag to create an attractive dynamic button effect. <script src="https:// ...

"Enhance Your Website with Creative Image Hover Effects using HTML

I'm looking to add a simple hover effect to the images AboutUsLink.png and AboutUsColourLink.png, but I'm unsure of the process. Here is the HTML code: <section class="link1"> <a href="#SecondLink"><img src="images/LogoAndLin ...

Guide to navigating to a different webpage with Node.js

I am a beginner user of NodeJS and I have a specific request. I need assistance with setting up a page redirect from one page to another upon clicking on a link. Any guidance provided will be greatly appreciated. Here is the code snippet: app.js var expr ...

Toggling the visibility of a div using JavaScript

When I click the button, I want to show and then hide a div. However, it doesn't work on the first click - only on the second click. How can I make it work on the first click? HTML <p>Click the button</p> <button onclick="myFu ...

SVG Opacity Transitions seem to ignore the constraints of CSS rules and bounce chaotically beyond their designated boundaries

I'm currently working on implementing a hover effect for an SVG rect embedded in HTML. The transition I've set up is not smooth, and the opacity values seem to be inconsistent during the animation. My browser of choice is Firefox. The rect I&apo ...

Organize objects neatly in a grid formation

I have a .test div with grid-template-columns: auto auto auto;. The column width is set to vary based on the element width, and I also used justify-content: start; to align the columns to the left. I chose to use auto instead of 1fr to prevent the columns ...

Ensure the background image completely fills the div, regardless of the image's aspect ratio

Can anyone help me figure out how to fill a div's background with an image, regardless of its aspect ratio (landscape or portrait)? I've tried different methods from using background-size: cover; to background-size: 100% 100%;, but the div doesn& ...

Troubleshooting issue with jquery.backgroundSize.js on Internet Explorer 8

After trying out the jquery.backgroundSize.js plugin, I ran into issues getting it to work in IE8. I followed the steps from the official website and tested on various browsers like IE9 and Firefox, but nothing seemed to happen on IE8. css: #main{ b ...

Connecting CSS auto-complete with JSP content within Eclipse

Is there a way to connect the CSS autocomplete with the content of a JSP page? For instance, if I have an element like <div id="myid"> in the JSP file, can Eclipse auto-complete "myid" when typing "#" in the CSS file? I am aware that NetBeans has th ...

Aligning HTML form elements side by side

I am having trouble displaying multiple Bootstrap elements and buttons in a single line. Here is an example of what I have: <span><input class="form-control" id="filename" value="Select a ZIP file to upload..." disabled style="display: inline"> ...

HTML / CSS / JavaScript Integrated Development Environment with Real-time Preview Window

As I've been exploring different options, I've noticed a small but impactful nuance. When working with jQuery or other UI tools, I really enjoy being able to see my changes instantly. While Adobe Dreamweaver's live view port offers this func ...