I forked the example on angular material table with sticky header and I added more data. I see that the headers are not sticky. Stackblitz here
Anyone knows how headers can remain sticky?
I forked the example on angular material table with sticky header and I added more data. I see that the headers are not sticky. Stackblitz here
Anyone knows how headers can remain sticky?
 
    
     
    
    The problem in your example is that the table container has overflow: auto and height: 100%; this is unnecessary, since the page will already have a scrollbar attached if the table content is bigger than the viewport.
I've fixed it by removing all the styles that are attached to .example-container.
In this way, the sticky element will be set relative to the top of the page.
Example: https://stackblitz.com/edit/angular-brzwrz-aejes6
Let me know if it works for you.
 
    
    By limiting your table height. https://stackblitz.com/edit/angular-brzwrz-hkevwi
 
    
    Give height to your table container :
.example-container {
  height: 400px;
  overflow: auto;
} 
    
    Same issue with material expansion table and related example. I solved by setting overflow: hidden in style.css under scr folder, as below:
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; overflow: hidden; }
and modifying the CSS .example-container in
.example-container {
  height: 100vh;
  overflow: auto;
 }
You will see only the table scrollbar.
Hope help someone
 
    
    In my case I just removed the overflow: auto from my table container, and the sticky worked.
 
    
    Found a much easier way... It's all about the syntax in the definition...
<cdk-virtual-scroll-viewport tvsItemSize
                             class="wrapper mat-elevation-z2">
  <table mat-table [dataSource]="dataSource">
<!-- Notice the definition for *matHeaderRowDef and the 'sticky: true' in quotes. -->
    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    <ng-container matColumnDef="id">
      <th mat-header-cell
          *matHeaderCellDef
          class="col-sm">No.</th>
      <td mat-cell
          *matCellDef="let element"
          class="col-sm">{{element.id}}</td>
    </ng-container>
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef>Name</th>
      <td mat-cell *matCellDef="let element">{{element.name}}</td>
    </ng-container>
  </table>
</cdk-virtual-scroll-viewport>
