Change color of matInput

Jar*_*rod 6 material angular stackblitz

How do I change matInput to a custom color. I want to change the placeholder and underline color.

I have read through most of the posts and could not find a solution to change the underline.

  <mat-form-field class="example-full-width">
    <input matInput placeholder="Favorite food" value="Sushi">
  </mat-form-field>
Run Code Online (Sandbox Code Playgroud)

Stackblitz example

Image example

Nen*_*dak 11

You can use plain css

 ::ng-deep .mat-focused .mat-form-field-label {
  /*change color of label*/
  color: green !important;
 }

 ::ng-deep.mat-form-field-underline {
  /*change color of underline*/
  background-color: green !important;
} 

::ng-deep.mat-form-field-ripple {
 /*change color of underline when focused*/
 background-color: green !important;;
}
Run Code Online (Sandbox Code Playgroud)

or create custom theme to apply on.Here is article,how to create custom themes

https://alligator.io/angular/angular-material-custom-theme/

  • 顺便不推荐`::​​ ng-deep`和`/ deep /`。 (3认同)
  • 对于新开发人员,请注意,这种方式现已弃用,不应再使用。如果您仍然需要一个解决方案来更改角度组件子元素上的 css,那么您仍然可以在主 styles.scss 中执行它们 (2认同)

小智 10

如果您使用材质主题,则可以使用重音或主色,只需在 mat-form-field 中添加颜色属性:

<mat-form-field class="example-full-width" color="accent">
   <input matInput placeholder="Favorite food" value="Sushi">
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)


小智 8

如果您将表单放入 div 中并为其指定一个类,然后使用 ::ng-deep className {...} 您将不会覆盖应用程序中的所有“mat-form-field”。

::ng-deep .create-account-form {
  .mat-focused .mat-form-field-ripple {
    /*change color of underline when focused*/
    background-color: @color-blue !important;
  }
  .mat-focused .mat-form-field-label {
    /*change color of label when focused*/
    color: @color-blue !important;
   }
}
Run Code Online (Sandbox Code Playgroud)
<form class="create-account-form">
      <mat-form-field class="input-size">
        <input type="email" mdInput formControlName="email" placeholder="Email" required matInput>
      </mat-form-field>
</form>
Run Code Online (Sandbox Code Playgroud)


小智 5

只需为这些类指定颜色即可

    .mat-focused .mat-form-field-label {
      /*change color of label*/
      color: white !important;
     }
    
     .mat-form-field-underline {
      /*change color of underline*/
      background-color: white !important;
    } 
    
    .mat-form-field-ripple {
     /*change color of underline when focused*/
     background-color: white !important;;
    }
Run Code Online (Sandbox Code Playgroud)