'Angular Material SideNav autosize not working
I was using Angular Material to build a sidenav that can hide the nav text when onclick menu. I follow the https://material.angular.io/components/sidenav/overview using autosize on mat-sidenav-container
But the sidenav still overlay the mat-sidenav-content part and it doesn't auto resize the content part
I using "@angular/material": "^8.0.2"
html
<mat-sidenav-container class="example-container" autosize>
<mat-sidenav #sidenav class="example-sidenav" mode="side" opened="true">
<mat-nav-list>
<mat-list-item>
<button mat-icon-button (click)="isExpanded = !isExpanded">
<mat-icon *ngIf="!isExpanded">chevron_right</mat-icon>
<mat-icon *ngIf="isExpanded">chevron_left</mat-icon>
</button>
</mat-list-item>
<mat-list-item>
<mat-icon mat-list-icon>home</mat-icon>
<p matLine *ngIf="isExpanded">Home</p>
</mat-list-item>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
Main content
</mat-sidenav-content>
</mat-sidenav-container>
ts file
import {Component} from '@angular/core';
import { MatSidenav } from '@angular/material/sidenav';
@Component({
selector: 'sidenav-autosize-example',
templateUrl: 'sidenav-autosize-example.html',
styleUrls: ['sidenav-autosize-example.css'],
})
export class SidenavAutosizeExample {
isExpanded = false;
}
css
.example-container {
width: 100%;
height: 300px;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.example-sidenav-content {
padding: 10px;
height: 100%;
}
Solution 1:[1]
You can easily fix this issue by adding autosize="true"
Example:
<mat-sidenav-container autosize="true">
Solution 2:[2]
None of the above solution worked for me. I think the proper solution is to rather than setting the width of <mat-sidenav></mat-sidenav> in the CSS , set it to
min-width:50px
In order to resize according, you should be mentioning the min-width and max-width in the CSS.
Solution 3:[3]
Remove the element
<mat-sidenav-content>
I tried in many ways, this was the only one that worked
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Kamil Kafoor |
| Solution 3 | Natan Cipriano |


