I Want to change the md-input color red (same as Country Dropdown in Image) when it is dirty and invalid . I have gone through many articles but didn't find any helpful ..
Asked
Active
Viewed 1.9k times
3 Answers
2
You need to include below css
.mat-input-invalid .mat-input-placeholder {
color: red;
}
.mat-input-invalid .mat-input-ripple {
background-color: red;
}

Lokesh Daiya
- 799
- 1
- 8
- 14
-
1No need for that now. New update in angular material has that feature too now – Ghanshyam Singh Jul 28 '17 at 12:30
-
@GhanshyamSingh Please elaborate. I couldn't find anything on the docs. – Ka Mok Mar 04 '18 at 23:52
-
1check the link `https://material.angular.io/components/form-field/overview` – Ghanshyam Singh Mar 05 '18 at 06:20
-
2you can use `
` element just below the input box. – Ghanshyam Singh Mar 05 '18 at 06:21 -
@GhanshyamSingh, what about valid state, I am trying to find a way to change the color to green if the input is valid – JSON Mar 17 '18 at 02:02
1
It can be solved by css, but needs a preparation regarding encapsulation to avoid deprecated variants like ::ng-deep
. A solution is given here:
https://stackoverflow.com/a/46042645/2692283
Here is a short description for your case:
Set
encapsulation: ViewEncapsulation.None
in your component:import { ViewEncapsulation } from '@angular/core'; @Component({ ... encapsulation: ViewEncapsulation.None })
A more detailed explanation is given here:
Now just use css to make it red for your case:
/* Sets the underline to red if dirty and invalid */ .mat-form-field.ng-dirty.ng-invalid .mat-form-field-underline { background-color: red; } /* Sets the label to red if dirty and invalid */ .mat-form-field.ng-dirty.ng-invalid .mat-form-field-label { color: red; }
It should work for Angular 4 and 5.

sagacity
- 341
- 3
- 7
1
Error color is set to your warn color in the theme.
https://material.angular.io/guide/theming
@import '~@angular/material/theming';
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();
// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue. Available color palettes: https://material.io/design/color/
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent: mat-palette($mat-pink, A200, A100, A400);
// The warn palette is optional (defaults to red).
$candy-app-warn: mat-palette($mat-red);
// Create the theme object (a Sass map containing all of the palettes).
$candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($candy-app-theme);

Todd Skelton
- 6,839
- 3
- 36
- 48