Angular Material 自定义标签
Angular Material customize tab
我正在使用 angular 4 我正在使用 Angular Material。
<md-tab-group [disableRipple]=true>
<md-tab label="Tab 1"></md-tab>
<md-tab label="Tab 2"></md-tab>
</md-tab-group>
如何完全自定义背景颜色(未选中/选中)、文本颜色等。我已经尝试使用伪 类...但仍然无济于事。 --- 我已成功设置 font-size
,但设置时文本颜色有点抖动。请帮忙。
更新:
我尝试在选中时将背景更改为透明...当未在选项卡中选中 link 时尝试覆盖颜色等等...但仍然无效。
/* Styles go here */
.mat-tab-label{
color:white;
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
/deep/ .mat-tab-label{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
.md-tab.ng-scope.ng-isolate-scope.md-ink-ripple.md-active{
background-color:transparent;
color:white;
font-weight: 700;
}
.md-tab.ng-scope.ng-isolate-scope.md-ink-ripple{
background-color:transparent;
color:white;
font-weight: 700;
}
.mat-tab-label:active{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
.mat-tab-label:selected{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
在您的组件中,将 ViewEncapsulation 设置为 None
并在 component.css 文件中添加样式。
Typescript 代码的变化:
import {Component, ViewEncapsulation} from '@angular/core';
@Component({
....
encapsulation: ViewEncapsulation.None
})
组件CSS:
/* Styles for tab labels */
.mat-tab-label {
min-width: 25px !important;
padding: 5px;
background-color: transparent;
color: red;
font-weight: 700;
}
/* Styles for the active tab label */
.mat-tab-label.mat-tab-label-active {
min-width: 25px !important;
padding: 5px;
background-color: transparent;
color: red;
font-weight: 700;
}
/* Styles for the ink bar */
.mat-ink-bar {
background-color: green;
}
更新
要自定义选项卡背景和墨栏,您可以定义自己的 theme then use the theme options on the tab group:
<div class="my-theme">
<mat-tab-group [backgroundColor]="'primary'" [color]="'warn'">
<mat-tab label="First"> Content 1 </mat-tab>
<mat-tab label="Second"> Content 2 </mat-tab>
<mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
</div>
这是 StackBlitz 上的示例。
旧答案:::ng-deep
如果您不想触摸 ViewEncapsulation,请使用 ::ng-deep 和 class 选择器(通过浏览器开发工具检查)。
例如(Angular 5, Material 2):
/* active tab */
::ng-deep .mat-tab-list .mat-tab-labels .mat-tab-label-active {
color:red;
background-color: green;
}
/* ink bar */
::ng-deep .mat-ink-bar {
background-color: var(--primary-color,#1F89CE) !important;
}
我花了很长时间才弄清楚如何通过覆盖 material 设计来更改选项卡的背景颜色。我不知道在哪里分享我的最终结果,所以在这里:
对于 .ts 文件:
@Component({
selector: 'app-tabs',
templateUrl: './tabs.component.html',
styleUrls: ['./tabs.component.css'],
encapsulation: ViewEncapsulation.None
})
对于 css 文件:
.mat-tab-labels, .mat-tab-label, .mat-tab-link {
color: white;
font-size: 16px;
background-color: #804A71;
}
.mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
background: white;
height: 5px;
}
这就是背景颜色、文本(标签)的颜色和大小,以及文本下方的标签栏。当我同时使用 .mat-tab-labels 和 .mat-tab-label 时它终于起作用了。
最新解决方案:-
1)覆盖 styles.css
2) 使用 material-tab 存在的组件的选择器
styles.css
app-child .mat-tab-label.mat-tab-label-active {
padding: 0px 15px ;
justify-content: flex-start;
}
app-child .mat-tab-label{
padding: 0px 15px ;
justify-content: flex-start;
}
.mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
background:#6168e7;
}
Angular 版本 6
自定义标签的新设计
html
<mat-tab-group [disableRipple]="true" mat-align-tabs="center">
<md-tab label="Tab 1"></md-tab>
<md-tab label="Tab 2"></md-tab>
</md-tab-group>
scss
::ng-deep .mat-tab-labels {
min-width: auto !important;
div {
border: 1px solid #fff;
background-color: #404040;
&.mat-tab-label-active {
background-color: #3aafa9;
.mat-tab-label-content {
border: none;
background-color: #3aafa9;
}
}
.mat-tab-label-content {
border: none;
color: #fff !important;
}
}
}
::ng-deep .mat-tab-group{
&.mat-primary {
.mat-ink-bar {
background-color: transparent;
}
}
}
::ng-deep .mat-tab-body-wrapper{
min-height: 550px
}
::ng-deep .mat-tab-label-container {
flex-grow: 0 !important;
}
::ng-deep .mat-tab-label{
opacity: 1 !important;
}
您还可以使用 ::ng-deep 伪元素设置默认 material 类 的样式。
:host ::ng-deep .mat-tab-label {
border-width: 9px;
border-style: solid;
border-color: orange;
}
:host 是可选的,它将样式范围限定为当前组件中的选项卡。
我正在使用 angular 4 我正在使用 Angular Material。
<md-tab-group [disableRipple]=true>
<md-tab label="Tab 1"></md-tab>
<md-tab label="Tab 2"></md-tab>
</md-tab-group>
如何完全自定义背景颜色(未选中/选中)、文本颜色等。我已经尝试使用伪 类...但仍然无济于事。 --- 我已成功设置 font-size
,但设置时文本颜色有点抖动。请帮忙。
更新:
我尝试在选中时将背景更改为透明...当未在选项卡中选中 link 时尝试覆盖颜色等等...但仍然无效。
/* Styles go here */
.mat-tab-label{
color:white;
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
/deep/ .mat-tab-label{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
.md-tab.ng-scope.ng-isolate-scope.md-ink-ripple.md-active{
background-color:transparent;
color:white;
font-weight: 700;
}
.md-tab.ng-scope.ng-isolate-scope.md-ink-ripple{
background-color:transparent;
color:white;
font-weight: 700;
}
.mat-tab-label:active{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
.mat-tab-label:selected{
min-width: 25px !important;
padding: 5px;
background-color:transparent;
color:white;
font-weight: 700;
}
在您的组件中,将 ViewEncapsulation 设置为 None
并在 component.css 文件中添加样式。
Typescript 代码的变化:
import {Component, ViewEncapsulation} from '@angular/core';
@Component({
....
encapsulation: ViewEncapsulation.None
})
组件CSS:
/* Styles for tab labels */
.mat-tab-label {
min-width: 25px !important;
padding: 5px;
background-color: transparent;
color: red;
font-weight: 700;
}
/* Styles for the active tab label */
.mat-tab-label.mat-tab-label-active {
min-width: 25px !important;
padding: 5px;
background-color: transparent;
color: red;
font-weight: 700;
}
/* Styles for the ink bar */
.mat-ink-bar {
background-color: green;
}
更新
要自定义选项卡背景和墨栏,您可以定义自己的 theme then use the theme options on the tab group:
<div class="my-theme">
<mat-tab-group [backgroundColor]="'primary'" [color]="'warn'">
<mat-tab label="First"> Content 1 </mat-tab>
<mat-tab label="Second"> Content 2 </mat-tab>
<mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>
</div>
这是 StackBlitz 上的示例。
旧答案:::ng-deep
如果您不想触摸 ViewEncapsulation,请使用 ::ng-deep 和 class 选择器(通过浏览器开发工具检查)。
例如(Angular 5, Material 2):
/* active tab */
::ng-deep .mat-tab-list .mat-tab-labels .mat-tab-label-active {
color:red;
background-color: green;
}
/* ink bar */
::ng-deep .mat-ink-bar {
background-color: var(--primary-color,#1F89CE) !important;
}
我花了很长时间才弄清楚如何通过覆盖 material 设计来更改选项卡的背景颜色。我不知道在哪里分享我的最终结果,所以在这里:
对于 .ts 文件:
@Component({
selector: 'app-tabs',
templateUrl: './tabs.component.html',
styleUrls: ['./tabs.component.css'],
encapsulation: ViewEncapsulation.None
})
对于 css 文件:
.mat-tab-labels, .mat-tab-label, .mat-tab-link {
color: white;
font-size: 16px;
background-color: #804A71;
}
.mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
background: white;
height: 5px;
}
这就是背景颜色、文本(标签)的颜色和大小,以及文本下方的标签栏。当我同时使用 .mat-tab-labels 和 .mat-tab-label 时它终于起作用了。
最新解决方案:-
1)覆盖 styles.css 2) 使用 material-tab 存在的组件的选择器
styles.css
app-child .mat-tab-label.mat-tab-label-active {
padding: 0px 15px ;
justify-content: flex-start;
}
app-child .mat-tab-label{
padding: 0px 15px ;
justify-content: flex-start;
}
.mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
background:#6168e7;
}
Angular 版本 6
自定义标签的新设计
html
<mat-tab-group [disableRipple]="true" mat-align-tabs="center">
<md-tab label="Tab 1"></md-tab>
<md-tab label="Tab 2"></md-tab>
</md-tab-group>
scss
::ng-deep .mat-tab-labels {
min-width: auto !important;
div {
border: 1px solid #fff;
background-color: #404040;
&.mat-tab-label-active {
background-color: #3aafa9;
.mat-tab-label-content {
border: none;
background-color: #3aafa9;
}
}
.mat-tab-label-content {
border: none;
color: #fff !important;
}
}
}
::ng-deep .mat-tab-group{
&.mat-primary {
.mat-ink-bar {
background-color: transparent;
}
}
}
::ng-deep .mat-tab-body-wrapper{
min-height: 550px
}
::ng-deep .mat-tab-label-container {
flex-grow: 0 !important;
}
::ng-deep .mat-tab-label{
opacity: 1 !important;
}
您还可以使用 ::ng-deep 伪元素设置默认 material 类 的样式。
:host ::ng-deep .mat-tab-label {
border-width: 9px;
border-style: solid;
border-color: orange;
}
:host 是可选的,它将样式范围限定为当前组件中的选项卡。