如果您想自定义 Angular 材质组件并为 mat-input 占位符提供自己的样式,我有以下建议。
1) 覆盖主 style.css(或 style.scss,无论您使用哪个)上的类。如果您想知道,它与您的 index.html、main.ts、package.json 等位于同一目录级别。
.mat-form-field-label {
font-size: 0.8em!important;
}
我在here 上创建了一个演示。
2) 使用ViewEncapsulation:None。在我看来,这是不那么推荐的,因为它删除了组件上所有形式的样式封装,这样 CSS 规则将具有全局效果。
在您的component.ts 上,您需要导入ViewEncapsulation,然后在您提供封装定义时选择None。
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'input-overview-example',
styleUrls: ['input-overview-example.css'],
templateUrl: 'input-overview-example.html',
encapsulation: ViewEncapsulation.None
})
您可以在组件的 css 上定义您的 CSS 样式,
,但没有!important 声明。
.mat-form-field-label {
font-size: 0.8em;
}
我在here 上创建了另一个演示。
3) 在同一组件的 css 中使用 :host ::ng-deep 伪选择器。这样做将允许您禁用该特定规则的视图封装。请注意,这种用法可能是有风险的,因为将来可能是deprecated。
在组件的 css 上,
:host ::ng-deep .mat-form-field-label {
font-size: 0.8em;
}
我在这里创建了另一个demo。