Styling mat-form-field input in Angular/Material

前端 未结 3 887
故里飘歌
故里飘歌 2020-12-16 16:33

I have a mat-form-field with input that I am wanting to add a custom style to, however I cannot find any documentation regarding this on the official Angular Material websit

3条回答
  •  青春惊慌失措
    2020-12-16 17:40

    For change the styles of material inputs with scss:

    Standard:

    ::ng-deep .mat-form-field {
        .mat-input-element {
            color: slategray;
        }
        .mat-form-field-label {
            color: slategray;
        }
        .mat-form-field-underline {
            background-color: slategray;
        }
        .mat-form-field-ripple {
            background-color: slategray;
        }
        .mat-form-field-required-marker {
            color: slategray;
        }
    }
    

    Focused: (when selected)

    ::ng-deep .mat-form-field.mat-focused {
        .mat-form-field-label {
            color: #ff884d;
        }
        .mat-form-field-ripple {
            background-color: #ff884d;
        }
        .mat-form-field-required-marker {
            color: #ff884d;
        }
        .mat-input-element {
            color: #ff884d;
        }
    }
    

    Invalid:

    ::ng-deep .mat-form-field.mat-form-field-invalid {
        .mat-input-element {
            color: #ff33cc;
        }
        .mat-form-field-label {
            color: #ff33cc;
            .mat-form-field-required-marker {
                color: #ff33cc;
            }
        }
        .mat-form-field-ripple {
            background-color: #ff33cc;
        }
    }
    

    DEMO

    you can also use ViewEncapsulation.None to avoid ::ng-deep which is deprecated:

    import { ViewEncapsulation } from '@angular/core';
    
    @Component({
        ...
        encapsulation: ViewEncapsulation.None
    })
    

提交回复
热议问题