Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the color of an icon in Angular Material?

I have this, which I would assume to work, but doesn't:

<mat-icon color="white">home</mat-icon>

Then, I also have:

<button mat-raised-button color="accent" type="submit"
 [disabled]="!recipientForm.form.valid">
    <mat-icon color="white">save</mat-icon>SAVE
</button>

This code snippet, for some reason, does work (shows the icon as white).

How do I get the lone mat-icon to show up as white using the color attribute? (I can easily just add a white class, but I want to understand this)

like image 447
Joshua Kemmerer Avatar asked Oct 18 '17 14:10

Joshua Kemmerer


People also ask

How do I change the color of my 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.

How do you change the color of angular material stepper step icons?

There does not seem to be option to change color of mat stepper icon, you can use this css as workaround. Custom theme class can be used across application,just wrapp any material component and use color attribute primary,accent or warn as defined in class.

How do I use material icons in Angular 12?

To use Material icons in Angular, use <mat-icon> component. The <mat-icon> directive supports both icon fonts and SVG icons, but not bitmap-based formats. Icons are the necessary components when building modern-day web apps, and sometimes they can be frustrating.


Video Answer


4 Answers

That's because the color input only accepts three attributes: "primary", "accent" or "warn". Hence, you'll have to style the icons the CSS way:

  1. Add a class to style your icon:

    .white-icon {
        color: white;
    }
    /* Note: If you're using an SVG icon, you should make the class target the `<svg>` element */
    .white-icon svg {
        fill: white;
    }
    
  2. Add the class to your icon:

    <mat-icon class="white-icon">menu</mat-icon>
    
like image 66
Edric Avatar answered Oct 16 '22 11:10

Edric


In the component.css or app.css add Icon Color styles

.material-icons.color_green { color: #00FF00; }
.material-icons.color_white { color: #FFFFFF; }

In the component.html set the icon class

<mat-icon class="material-icons color_green">games</mat-icon>
<mat-icon class="material-icons color_white">cloud</mat-icon>

ng build

like image 29
sfanjoy Avatar answered Oct 16 '22 10:10

sfanjoy


Or simply

add to your element

[ngStyle]="{'color': myVariableColor}"

eg

<mat-icon [ngStyle]="{'color': myVariableColor}">{{ getActivityIcon() }}</mat-icon>

Where color can be defined at another component etc

like image 14
Jimmy Kane Avatar answered Oct 16 '22 10:10

Jimmy Kane


color="white" is not a known attribute to Angular Material.

color attribute can changed to primary, accent, and warn. as said in this doc

your icon inside button works because its parent class button has css class of color:white, or may be your color="accent" is white. check the developer tools to find it.

By default, icons will use the current font color

like image 9
Hareesh Avatar answered Oct 16 '22 10:10

Hareesh