Can I change the color of Angular Material checkbox with some custom color and how?

后端 未结 7 1919
盖世英雄少女心
盖世英雄少女心 2020-12-28 16:10

I\'m trying to do this, but is looks like it is only possible to change it to primary or warn.

相关标签:
7条回答
  • 2020-12-28 16:40

    The proper way to control colors of Angular Material elements is to use differrent pallettes, but the functionality is limited. If you want more control, you can add custom CSS, and in most cases you will need to use ::ng-deep to force your styles to apply, for example:

    ::ng-deep .mat-checkbox .mat-checkbox-frame {
      border-color: violet;
    }
    

    Here is a DEMO, where I changed the color of the border and background when it checked. You can follow this way to implement any styling you want.

    If you're not familiar with ::ng-deep and don't really understand, when you need it, you can look through THIS short answer.

    UPDATE 1

    There is another one way via encapsulation disabling (thanks to @Ventura). It has a right to exist, but be careful if you decided to disable encapsulation, don't confuse with stylesheets, because it behaves differently from what you expect from default Angular logic regarding CSS.

    For more info: https://coryrylan.com/blog/css-encapsulation-with-angular-components

    SO answer: https://stackoverflow.com/a/54981478/6053654

    UPDATE 2

    The method is deprecated, see more info HERE or HERE.

    0 讨论(0)
提交回复
热议问题