I am using Angular Material for my project and I want to change color of mat-checkbox, but don't want to use theming, so obviusly I used Elements and Styles sections in my Chrome Browser and traced classes which mat-checkbox uses and found this: ".mat-checkbox-ripple .mat-ripple-element".
::ng-deep .mat-checkbox-ripple .mat-ripple-element {
background-color: black!important;
}
So, this is my code, I tried ::ng-deep, /deep/ and without any of those, and result is always the same, checkbox color is not changed, but in "Styles" I can clearly see that it says that I have overwritten its color.
Here are versions of things I use:
"@angular/animations": "^7.1.4",
"@angular/cdk": "^7.2.0",
"@angular/common": "~7.1.0",
"@angular/compiler": "~7.1.0",
"@angular/core": "~7.1.0",
"@angular/forms": "~7.1.0",
"@angular/material": "^7.2.0",
"@angular/platform-browser": "~7.1.0",
"@angular/platform-browser-dynamic": "~7.1.0",
"@angular/router": "~7.1.0",
How can I change color?
The color of a <mat-checkbox> can be changed by using the color property. By default, checkboxes use the theme's accent color. This can be changed to 'primary' or 'warn' .
To style the checkbox the user first needs to hide the default checkbox which can be done by setting the value of the visibility property to hidden. Example 1: Consider the example where HTML checkbox is styled using CSS. When the user clicks the checkbox, the background color is set to green.
ripple
is the name of the water drop ripple effect that is displayed when you click on Material components, so you're targeting the wrong element.
Use:
// overwrite the checkbox background
::ng-deep .mat-checkbox-checked .mat-checkbox-background,
.mat-checkbox-indeterminate .mat-checkbox-background {
background-color: black !important;
}
// overwrite the ripple overlay on hover and click
::ng-deep .mat-checkbox:not(.mat-checkbox-disabled) .mat-checkbox-ripple .mat-ripple-element {
background-color: black !important;
}
If you want to change the color globally it's better to use theming.
for me above answers did not work for indeterminate state while overriding styles in a table, but the below code did
:host ::ng-deep .mat-checkbox-indeterminate,
.mat-checkbox-checked {
&.mat-primary .mat-checkbox-background {
background-color: $ your-color !important;
}
&.mat-accent .mat-checkbox-background {
background-color: $ your-color !important;
}
&.mat-warn .mat-checkbox-background {
background-color: $ your-color !important;
}
}
Use global scss in styles.scss and that should work.
.mat-checkbox-checked.mat-accent .mat-checkbox-background,
.mat-checkbox-indeterminate.mat-accent .mat-checkbox-background {
background-color: black;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With