title: Sticky toolbar material 2 and sidenav
body: <p>Started recently learning material 2 and ran to issue. My approach is to do a sidenav as in <code>Youtube</code> website. There is a hamburger menu on left which expands/collapses side menu. 
  Code:</p>
  
  <pre><code>&lt;md-toolbar class="fixed-header"&gt;
       &lt;button class="app-icon-button" (click)="start.toggle()"&gt;
            &lt;i class="material-icons app-toolbar-menu"&gt;menu&lt;/i&gt;
       &lt;/button&gt;
  &lt;/md-toolbar&gt;
  
  &lt;md-sidenav-container&gt;
      &lt;md-sidenav #start class="app-sidebar" mode="side"&gt;test&lt;/md-sidenav&gt;
  
      &lt;div class="text"&gt;
        {{ text }}
      &lt;/div&gt;
  &lt;/md-sidenav-container&gt;
  </code></pre>
  
  <p>Now seems like I achieved the layout, but the positioning is not <code>fixed</code>, if i scroll <code>toolbar</code> moves and <code>sidenav</code> content also moves.</p>
  
  <p><code>Edit:</code></p>
  
  <p>On angular material documentation I later found the example which one I have been looking for:</p>
  
  <p><a href="https://stackblitz.com/angular/rmjxrpxdgmx?file=app%2Fsidenav-responsive-example.html" rel="nofollow noreferrer">https://stackblitz.com/angular/rmjxrpxdgmx?file=app%2Fsidenav-responsive-example.html</a></p>
  
  <p>if link will be removed, here is the code:</p>
  
  <p><code>HTML File:</code></p>
  
  <pre><code>&lt;div class="example-container" [class.example-is-mobile]="mobileQuery.matches" *ngIf="shouldRun"&gt;
    &lt;mat-toolbar color="primary" class="example-toolbar"&gt;
      &lt;button mat-icon-button (click)="snav.toggle()"&gt;&lt;mat-icon&gt;menu&lt;/mat-icon&gt;&lt;/button&gt;
      &lt;h1 class="example-app-name"&gt;Responsive App&lt;/h1&gt;
    &lt;/mat-toolbar&gt;
  
    &lt;mat-sidenav-container class="example-sidenav-container"
                           [style.marginTop.px]="mobileQuery.matches ? 56 : 0"&gt;
      &lt;mat-sidenav #snav [mode]="mobileQuery.matches ? 'over' : 'side'"
                   [fixedInViewport]="mobileQuery.matches" fixedTopGap="56"&gt;
        &lt;mat-nav-list&gt;
          &lt;a mat-list-item routerLink="." *ngFor="let nav of fillerNav"&gt;{{nav}}&lt;/a&gt;
        &lt;/mat-nav-list&gt;
      &lt;/mat-sidenav&gt;
  
      &lt;mat-sidenav-content&gt;
        &lt;p *ngFor="let content of fillerContent"&gt;{{content}}&lt;/p&gt;
      &lt;/mat-sidenav-content&gt;
    &lt;/mat-sidenav-container&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><code>TS File:</code></p>
  
  <pre><code>import {MediaMatcher} from '@angular/cdk/layout';
  import {ChangeDetectorRef, Component, OnDestroy} from '@angular/core';
  
  /** @title Responsive sidenav */
  @Component({
    selector: 'sidenav-responsive-example',
    templateUrl: 'sidenav-responsive-example.html',
    styleUrls: ['sidenav-responsive-example.css'],
  })
  export class SidenavResponsiveExample implements OnDestroy {
    mobileQuery: MediaQueryList;
  
    fillerNav = Array.from({length: 50}, (_, i) =&gt; `Nav Item ${i + 1}`);
  
    fillerContent = Array.from({length: 50}, () =&gt;
        `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
         labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
         laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
         voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
         cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`);
  
    private _mobileQueryListener: () =&gt; void;
  
    constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher) {
      this.mobileQuery = media.matchMedia('(max-width: 600px)');
      this._mobileQueryListener = () =&gt; changeDetectorRef.detectChanges();
      this.mobileQuery.addListener(this._mobileQueryListener);
    }
  
    ngOnDestroy(): void {
      this.mobileQuery.removeListener(this._mobileQueryListener);
    }
  }
  </code></pre>
  
  <p><code>CSS File:</code></p>
  
  <pre><code>.example-container {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
  }
  
  .example-is-mobile .example-toolbar {
    position: fixed;
    /* Make sure the toolbar will stay on top of the content as it scrolls past. */
    z-index: 2;
  }
  
  h1.example-app-name {
    margin-left: 8px;
  }
  
  .example-sidenav-container {
    /* When the sidenav is not fixed, stretch the sidenav container to fill the available space. This
       causes `&lt;mat-sidenav-content&gt;` to act as our scrolling element for desktop layouts. */
    flex: 1;
  }
  
  .example-is-mobile .example-sidenav-container {
    /* When the sidenav is fixed, don't constrain the height of the sidenav container. This allows the
       `&lt;body&gt;` to be our scrolling element for mobile layouts. */
    flex: 1 0 auto;
  }
  </code></pre>