Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mat-checkbox custom color cannot be changed

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.

enter image description here

enter image description here

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?

like image 426
ajaleksa Avatar asked Jan 13 '19 11:01

ajaleksa


People also ask

How do I change the color of my mat checkbox?

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' .

How do I change the color of my check box?

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.


3 Answers

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.

like image 180
frido Avatar answered Oct 20 '22 12:10

frido


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;
    }
}
like image 21
varun Avatar answered Oct 20 '22 14:10

varun


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;
}
like image 21
Mr. Dang Avatar answered Oct 20 '22 14:10

Mr. Dang