56

I want to add my own custom colors, similar to "primary", "warn" etc. For example, I added a class for an orange background, but I use it as a class and not as a color attribute. It works, but the code looks a bit confusing.

<button md-raised-button
        color="primary"
        class="btn-w-md ml-3 orange-500-bg">
          <i class="material-icons">description</i>
          Deactivate
</button>

What do I need to do to add it as color="orange"?

1
  • Simply put, you cant add color="orange". You have to make your own theme to do that. Commented Jul 17, 2017 at 12:32

11 Answers 11

135

You can add any color like below

 <a mat-mini-fab color="success" routerLink=".">link</a>
it generates the class with that value like 'mat-success' then create css for that class

.mat-success {
    background-color: green;
    color: #fff;
}

Sign up to request clarification or add additional context in comments.

9 Comments

If you just want to assign a string, you don't need binding: color="success".
Yeah the purpose here you can assign any "string" values , that will generated as CSS Class, with that we can customize required color, like the above one. I have assigned string "Success" and it generated mat-success as a class.
Excellent answer. Very simple and very effective. Thank you Vishnudev and Gopi
This appears to no longer work as of material v15
For angular 15 I had to add !important.
|
33

For version 16 use this STACKBLITZ . (Thanks to Lucas Tesseron)


I have created styles for all mat-button types

STACKBLITZ DEMO

angular material success button

(add to your global styles)

.mat-button.mat-success,
.mat-stroked-button.mat-success {
    color: #155724;
}
.mat-button.mat-success:hover,
.mat-stroked-button.mat-success:hover {
  background-color: #f0fff3;
}

.mat-raised-button.mat-success,
.mat-flat-button.mat-success,
.mat-fab.mat-success,
.mat-mini-fab.mat-success {
  color: #f0fff3;
  background-color: #155724;
}

.mat-icon-button.mat-success {
  color:#155724;
}

7 Comments

This is completely amazing, I had no idea you could do this. Thank you.
my god. you can just use mat-{custom-name} and it works perfect. That if you don't want to be constrains.
now classes are changed to mdc-button, mat-mdc-button-raised and cetera FYI
Here is an updated version for @angular/material 16.0.0 stackblitz.com/edit/oyd9fg?file=src/styles.scss
I don't know why in my Angular 16, I cannot apply those created colors. I can do it only by using .mat-mdc-raised-button:not(:disabled) selector and assign the color as important to overwrite the original one.
|
23

You can change the color of the button only in the normal css way by defining a class for background color orange and using it as class attribute.

If you need to use it as color="orange". Then you need to create your own theme with the colors you need. Refer Theming your angular material app on how to do it. You can even make it custom for each element. Through customisation you can choose different themes based on elements

But still you wont be able to use it like color="orange", you can define orange as your primary or accent color and use it as color="primary" or color="accent" based on your need.

Theme can have only following items with different colors

  • A primary palette: colors most widely used across all screens and components.
  • An accent palette: colors used for the floating action button and interactive elements.
  • A warn palette: colors used to convey error state.
  • A foreground palette: colors for text and icons.
  • A background palette: colors used for element backgrounds.

Comments

5

you can use a class with background

example:-

CSS:-

.save-button {
  background: green;
}

HTML:-

<button mat-mini-fab class="save-button" >
       <mat-icon >save</mat-icon>
</button>

Comments

4

This answer is in the context of an angular 5.1.1 project using angular/cli and angular material 5.1.

The mat-... seems to inherit its color from the parent so if you surround with a span or div and apply the color there it should fall through. In my particular use case we wanted to dynamically set the color of icons. Our initial implementation and new implementation are below.

This class assignment will override the mat-icon class. It appears that this use to work but does not anymore:

<mat-icon [class]="custom-class">icon name</mat-icon>

Now you can wrap:

<span [class]="custom-class"><mat-icon>icon name</mat-icon></span>

The first case lead to the words "icon name" colored as desired but no icon. The second case lead to the desired behavior.

Comments

4

Because the template checker gives an error/warning when you are using custom colors, there is a hacky way around it. This is only for the real desperate people who like things to be strict strict strict and have proper hinting from the IDE :)

First step is to create your own ambient declaration. I've added mine to the polyfills.ts, because I'm a bit lazy:

declare module '@angular/material/core/common-behaviors/color' {
  interface CanColor {
    // @ts-ignore
    color: CustomThemePalette;
  }
}

The @ts-ignore is necessary, otherwise you'll get the following error:

TS2717: Subsequent property declarations must have the same type. Property 'color' must be of type 'ThemePalette', but here has type 'CustomThemePalette'.

Then you can define your custom theme palette:

import type { ThemePalette } from '@angular/material/core/common-behaviors/color';

export type CustomThemePalette = ThemePalette | 'secondary' | 'success' | 'alert';

Now finally you can use those custom colors in your template, and your IDE will hint them and notify you when you've mistyped them. Victory!

victory

note: obviously things will go south this way, when material decides to change their CanColor interface or move it around, but I suppose it would be relatively easy to adapt to such changes

1 Comment

You, my friend, have made me super happy with this hint. :)
3

Using a stylesheet at the referenced at component level

.mat-button, .mat-raised-button{
    background-color:red;
} 

Note: Declare the stylesheet reference at the component level. LIVE DEMO

Comments

3

Similar solution adapted for Angular 15:

.mat-mdc.mat-mdc-button.mat-orange { color: orange; }

or

.mat-mdc-button.mat-orange:not(:disabled) { color: orange; }

2 Comments

Keep in mind that this solution works, because CSS is cascade. So in order to overwrite the styles provided by default, you have to have greater "weight" on your styles. That is why this solution works, adding more classes before the custom one you created adds to the "weight" of the style you want to enforce, thus making it more "important" than the default one.
EXAMPLE - By default button text color set to 'white' stackblitz:- strong text stackblitz.com/edit/uwskwr?file=package.json repo:- github.com/Amit006/Angular-material-custom-color-buttons.git
3

With the recent Angular 19 version, you can now use the mat.button-overrides mixin to custom your buttons :

@use '@angular/material' as mat;

.custom-button {
  @include mat.button-overrides((
    filled-container-color: orange,
    filled-label-text-color: red,
  ));
}

You can retrieve the documentation inside the style tab from the documentation

Note : This is only available if you use scss in your Angular project

2 Comments

Note that this solution works only if your file ends with ".scss", which is obvious for many of us, but this note might be helpful for newbies. :)
@include mat.button-overrides((text-state-layer-color: transparent)) will remove the hover for active, pressed, hover, etc. Agree with this post that mixins are the way to go...
1

For flat and raised buttons, a simple CSS should work as mentioned here.

But, to handle all variants (outline, simple, etc.) and all states (hover, focus, disabled, etc.) more complex setup is required.

I have created a stackblitz to show case the same.

If you want simpler setup, better approach would be to create another theme variant. For example, you can create a theme $custom-theme and have it applied under .custom-theme class. I have also created stackblitz to showcase the same.

Comments

0

For current angular material v15 please take a look on this answer to setup custom themes with mixin variants Angular Material custom colors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.