如何在 Angular/Material 输入框中更改占位符的颜色?
<mat-form-field>
<input matInput placeholder="Name">
</mat-form-field>
如何在 Angular/Material 输入框中更改占位符的颜色?
<mat-form-field>
<input matInput placeholder="Name">
</mat-form-field>
<mat-form-field>
<input matInput type="text">
<mat-placeholder class="placeholder">Search</mat-placeholder>
</mat-form-field>
CSS:
.mat-focused .placeholder {
color: #00D318;
}
我正在使用 Angular Material 8.2.3,但以上解决方案都不适用于我。在我的主题文件 theme.scss 中加入以下内容后问题得到解决:
.mat-input-element::placeholder{
color: red;
}
相反地,通过穿透以下内容应该可以在组件样式中工作。
::ng-deep .mat-input-element::placeholder{
color: red;
}
,.mat-select-placeholder
因为 selects 没有 mat-input-element 但使用它代替。 - Robin要更改占位符的颜色,请覆盖 .mat-form-field-placeholder 的 color 属性,例如:
```css .mat-form-field-placeholder { color: red; } ```::ng-deep .mat-form-field-placeholder{
color:red;
}
::ng-deep .mat-focused .mat-form-field-placeholder{
color:red;
}
改变下划线的颜色:
::ng-deep .mat-form-field-underline .mat-form-field-ripple{
background-color:red;
}
::ng-deep .mat-form-field-underline{
background-color:red;
}
这个运行完美。演示:https://plnkr.co/edit/yF4kXJ500YzefLOnLKNe?p=preview
以上的答案都不适用于我所使用的最新版本。
这里是可行的解决方案
::ng-deep .mat-form-field-label{
color:#000 !important;
}
::ng-deep .mat-form-field-underline{ background-color: seashell !important; }
和 ::ng-deep.mat-form-field-ripple { background-color: seashell !important;; }
以改变下划线的颜色。 - dpberry178mat-placeholder
元素。您无需覆盖 Material CSS 类。我认为这种方法是最好的。 <mat-form-field class="full-width">
<input matInput type="text" name="name" formControlName="name" >
<mat-placeholder style="color:brown">Enter your name here</mat-placeholder>
<mat-icon matSuffix class="material-icons">perm_identity</mat-icon>
</mat-form-field>
<mat-placeholder>
标签已弃用:https://material.angular.io/components/form-field/api#MatPlaceholder
/deep/
选择器将很快被弃用。 ::placeholder
选择器仍是实验性功能:https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder
正如@mohit uprim在上面的答案中建议的那样,我们可以使用影子穿透后代组合器即/ deep /或:: ng-deep或>>>。以下是一个例子
/deep/ .mat-form-field-placeholder {
color: #fff; // choose the color you want
}
或者
>>> .mat-form-field-placeholder {
color:red;
}
虽然目前在Angular文档中仍然指定了这种方法,但他们已经提到这种方法将很快被弃用。 阅读更多:https://angular.io/guide/component-styles#!#-deep-
根据Angular.io文档,正确的做法是将视图封装设置为none,然后在与该组件对应的css文件中编写自定义样式。请注意,这样做意味着您正在远离影子DOM和仿真技术(在Angular项目中默认),这意味着您在此组件中指定的样式可能会传播到父/子组件,并可能破坏那里的样式。
Angular Material本身不提供任何设置样式的指令。为此,您需要回到老派的方式。只需给您的输入元素分配一个id/class,然后使用伪类(::placeholder)。 参考链接:https://css-tricks.com/almanac/selectors/p/placeholder/