9

I'm using mat-tab-group and I want the tab headers to be fixed when I scroll down to the content. So, I fixed the height of tab content. I'm able to scroll through the content with fixed header. But, then I see two scroll bars on the page which is weird. Is there any other way that I can fix the tab headers while scrolling through the content? Attaching the snippet:

<mat-tab-group>
  <mat-tab label="Tab 1">
    <div style="overflow: scroll; height: 50%">
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </div>
  </mat-tab>
  <mat-tab label="Tab 2">Tab 2</mat-tab>
  <mat-tab label="Tab 3">Tab 3</mat-tab>
</mat-tab-group>

enter image description here

[Edit]: I'm more of looking to make header of mat-tab fixed. So, is there any straight forward way to do it instead of fixing height in styles?

Eranki
  • 750
  • 1
  • 11
  • 30

2 Answers2

10

app.component.html

<mat-tab-group>
  <mat-tab label="Content 1"> Content </mat-tab>
  <mat-tab label="Content 2"> Content </mat-tab>
  <mat-tab label="Content 3"> Content </mat-tab>
</mat-tab-group>

app.component.scss

:host ::ng-deep .mat-tab-header {
    top: 0;
    z-index: 1000; 
    position: sticky;
    position: -webkit-sticky; /* macOS/iOS Safari */    
}
Alonso Espí
  • 101
  • 2
  • 3
0

Change overflow style, like:

<div style="overflow-x: auto; overflow-y: visible; height: 50%">

So, horizontal scroll will be visible only if needed, and the vertical won't appear.

Nimer Awad
  • 3,967
  • 3
  • 17
  • 31