Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly change the color of a mat-icon with angular?

I have looked at the documentation and I have tried to change the color of a mat-icon like so:

<mat-icon class="white" color="primary" svgIcon="back"></mat-icon><span class="backText">back</span>

The above is how the element looks in the html. I have tried to change the color of the icon by adding a class with a color of white. Which does not work. I have tried adding the directive color="primary" which does not work either while all my buttons that look like this

 <button mat-button color='primary'>example button</button>

do receive the color. I want to get my mat-icon to change color with the color palette because this should work according to the docs. But ultimately I would also like to be able to change the color of the icon to white which is a color not on my color palette.

How can I change the color of <mat-icon> to a color from a working color palette as well as a color not on the color palette?

Thanks!

like image 802
Dan Avatar asked Apr 13 '18 14:04

Dan


People also ask

How do I change the color of a material icon?

If you want to use Jquery$(". material-icons"). css("color", themeColor); This will change color of the material icons inside an element eg input field.


2 Answers

Use NgStyle directive.

<mat-icon [ngStyle]="{'color':'white'}">home</mat-icon>
like image 165
ranjeet8082 Avatar answered Oct 16 '22 17:10

ranjeet8082


You have two options to change the color of a mat-icon.

Color Attribute

You can use the color attribute which will use the value specified in your theme. It only accepts three attributes: "primary", "accent" or "warn".

<mat-icon color="primary" svgIcon="archive"></mat-icon>
<mat-icon color="accent" svgIcon="archive"></mat-icon>
<mat-icon color="warn" svgIcon="archive"></mat-icon>

Custom Style

Add a custom style class with color specified:

.green-icon {
    color: green;
}

Add class to your icon:

<mat-icon class="green-icon" svgIcon="archive"></mat-icon>

Demo

I have created a demo on stackblitz that changes color of an SVG mat-icon.

like image 33
Chris Avatar answered Oct 16 '22 16:10

Chris