简体   繁体   English

无法更改Angular材质输入占位符的文本颜色

[英]Cannot change the text color of Angular material input placeholder

I am trying to change the placeholder text color on an Angular Material input. 我正在尝试在Angular Material输入上更改占位符文本颜色。 Appears to default to black but I want it to be white due to having a dark background color. 似乎默认为黑色,但由于背景颜色较深,我希望它为白色。

I have read probably every post on this site on how to do this but nothing seems to work. 我可能已经阅读了该网站上有关如何执行此操作的所有帖子,但似乎没有任何效果。 ::ng-deep and mat-placeholder are not options. :: ng-deep和mat-placeholder不是选项。

Here is a snippet from my HTML: 这是我的HTML的摘录:

    <form #searchForm="ngForm" class="example-full-width mr-auto">
      <mat-form-field style="width: 350px; font-size: 14px; margin-bottom: -15px;">
        <mat-label style="color: white;">Search for an Employee</mat-label>
        <input matInput [(ngModel)]="userIdInput" placeholder="Enter at least 2 characters of a name or ID"

Try this 尝试这个

  <mat-form-field>
    <mat-label>Search for an employee</mat-label>
    <input matInput placeholder="Enter at least 2 characters of a name or ID">
  </mat-form-field>
.mat-form-field {
  width: 350px;
  font-size: 14px;
}

::ng-deep .mat-input-element::placeholder {
  color: orange;
}

::ng-deep .mat-form-field-appearance-legacy .mat-form-field-label {
  color: red;
}

To change the css of your placeholder , all you need to do is modify the color of your matInput placeholder. 要更改placeholder的CSS,您需要做的就是修改matInput占位符的颜色。 You can make use of the mat-input-element class in the matInput element to do this. 您可以在matInput元素中使用mat-input-element类来执行此操作。

Ideally, I would also suggest you avoid using inline styles and use classes instead. 理想情况下,我还建议您避免使用内联样式,而应使用类。 It makes your code more readable as well. 它也使您的代码更具可读性。

HTML 的HTML

<form #searchForm="ngForm" class="example-full-width mr-auto">
  <mat-form-field class="employee-search-field">
    <mat-label>Search for an Employee</mat-label>
    <input matInput [(ngModel)]="userIdInput" name="userIdInput" placeholder="Enter at least 2 characters of a name or ID"/>
  </mat-form-field>
</form>

In your css, add the below code. 在您的CSS中,添加以下代码。

.employee-search-field {
  width: 350px;
  font-size: 14px;
  margin-bottom: -15px;
}

.employee-search-field mat-label {
  color: white;
  /* add label text color here */
}

.employee-search-field .mat-input-element {
  color: white;
  /* add input text color here */
}

.employee-search-field .mat-input-element::placeholder {
  color: white;
  /* add placeholder css here */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM