How to overwrite angular 2 material styles?

前端 未结 11 1025
南旧
南旧 2020-12-01 12:06

I have this select in angular material:

Its code :



        
11条回答
  •  鱼传尺愫
    2020-12-01 12:34

    The top solutions of /deep/, >>> and ::ng-deep are being deprecated and should no longer be used.

    The recommended method is now view encapsulation


    Edit: Word of warning. I do not recommend using this method at all (as of Jan 2019) as setting ViewEncapsulation.None will result in any of that components css becoming global styles (it stops Angular from creating ng_xxx attributes for component scoped css). This will result in global style conflict, especially with lazy loaded module css.

    Our solution to ViewEncapsulation was to override very specific css using highly specific css selectors in 1) global css or 2) creating separate style files for certain views / styles / elements, importing into every component required (e.g. styleUrls: [material-table-override.css, component.css]).


    I used ViewEncapsulation.None to successfully override material table styles within a single component in Angular 6.

    On your component:

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

    Here's a great article on the subject

提交回复
热议问题