The proper way to do it is to define a new theme for the form field. A theme consists of a primary palette, a secondary palette, and an optional warning palette, which defaults to red.
There are a number of pre-defined palettes located in node_modules/@angular/material/core/theming
, but you can also define your own.
In the below example I give all form fields under the search
class an orange and yellow theme.
@use "@angular/material" as mat;
$my-primary: mat.define-palette(mat.$orange-palette);
$my-accent: mat.define-palette(mat.$yellow-palette);
$my-theme: mat.define-light-theme(
(
color: (
primary: $my-primary,
accent: $my-accent,
),
)
);
.search {
@include mat.form-field-theme($my-theme);
}
This line: @include mat.form-field-theme($my-theme);
returns all of the necessary css to change the theme of a form field. There is one of these functions for every Material component, you can see them being exported in node_modules/@angular/material/_index.scss
Note: this scss needs to be global so you need to put it in a global style file like styles.scss
.
Alternatively you can make the component's scss global by disabling View Encapsulation.
@Component({
...
encapsulation: ViewEncapsulation.None,
})
export class MyComponent {
...
}
More info: https://material.angular.io/guide/theming
As a quick hack, you could also override the css like so:
Just the border
.search
.mat-form-field-appearance-outline.mat-focused
.mat-form-field-outline-thick {
color: red;
}
Label as well
.search {
.mat-form-field-appearance-outline.mat-focused .mat-form-field-outline-thick {
color: red;
}
.mat-form-field.mat-focused .mat-form-field-label {
color: red;
}
}
The devs don't recommend overriding css like this since it is prone to break in later versions: https://material.angular.io/guide/customizing-component-styles