How to change background color of specific angular material tabs?

后端 未结 2 982
一生所求
一生所求 2020-12-20 06:30

I have a component using mat tab from angular material 7.

I want to change the background color of my tabs depending on a boolean value of my typescript variable.

相关标签:
2条回答
  • 2020-12-20 07:24

    You might not want to set encapsulation: ViewEncapsulation.None, this will make the styling for this component be applied to all other components too.

    Instead you can set the color for the tab in your styles.scss like this:

    .mat-tab-header{
        background-color: #424242;
    }
    
    0 讨论(0)
  • 2020-12-20 07:34

    Edited: If you want to change a single tab you can use the aria-label input parameter.

    You'll have to add the

    encapsulation: ViewEncapsulation.None
    

    And use a specific css selectors like so:

    HTML:

    <mat-tab-group [color]="colorToggle.value" [backgroundColor]="backgroundColorToggle.value">
      <mat-tab label="First" [aria-label]=backgroundColorToggle.value> Content 1 </mat-tab>
      <mat-tab label="Second"> Content 2 </mat-tab>
      <mat-tab label="Third"> Content 3 </mat-tab>
    </mat-tab-group>
    

    CSS:

    [aria-label=primary] {
      background-color: blue;
    }
    
    [aria-label=accent] {
      background-color: aqua;
    }
    

    You can find example here

    If you want for all tabs:

    You have a dedicated API for it.

    Just use the backgroundColor property like so:

    <mat-tab-group [color]="colorToggle.value" [backgroundColor]="backgroundColorToggle.value">
    

    You can find the full example here

    0 讨论(0)
提交回复
热议问题