How to change background color of specific angular material tabs?

若如初见. 提交于 2020-02-25 04:06:34

问题


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.

The problem is that I can only apply the CSS on all tabs with

.mat-tab-label {
  background-color: red;
}

How to create a CSS class which I can apply on a specific tab.

I have a standard component. I tried using encapsulation: ViewEncapsulation.None but this only allowed me to change all tabs as mentioned above.

HTML:

<mat-tab-group mat-align-tabs="center" dynamicHeight="true">
  <mat-tab label="tab1">
    Hello
  </mat-tab>
  <mat-tab label="tab2">
    Hello2
  </mat-tab>
</mat-tab-group>

回答1:


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



来源:https://stackoverflow.com/questions/54435122/how-to-change-background-color-of-specific-angular-material-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!