0

I need to change background color for this css code

.mat-toolbar.mat-primary {

background

: #d12626; color: #fff; }

.mat-toolbar.mat-primary {
    background: #d12626;
    color: #fff;
}


I need to change the background for the above code. How to do that
I tried

 [ngStyle]='    .mat-toolbar.mat-primary {
        background: #d12626;
        color: #fff;
    }
'


But not working
Then i got another suggestion from stackoverflow

[style.font-size]="fontSize+'px'" this code is working for 'font-size' but not woking for my need code this is my need  [style.mat-toolbar.mat-primary.background]='#d12626'; but not working



I used this

.mat-toolbar.mat-primary {
        background: #d12626;
        color: #fff;
    }

code inside CSS code and it is working but I need to change the color dynamically.
I also need to update his colors
primaryColor
primaryLightColor
primaryDarkColor
secondaryColor
secondaryLightColor
secondaryDarkColor
primaryTextColor
secondaryTextColor

Midhilaj
  • 4,905
  • 9
  • 45
  • 88

4 Answers4

1

dont set the material color as primary or secondary then you can use ngStyle like this

<mat-toolbar [ngStyle]="{background:bgColor , color:'#fff'}">
  <mat-toolbar-row>
    <span>Main Toolbar</span>
  </mat-toolbar-row>
</mat-toolbar>

bgColor is just a component property, when you don't set the color property the component don't have any class so it 's easy to change the style

demo

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
0

Dynamic styles

You should make the CSS properties as camelcase, for the font-size should be like this [style.fontSize.px]. You check other solutions from how-to-apply-dynamic-styles?.

Override styles

We can do so using by combining the :host with the ::ng-deep selector and most probably using for overriding Material styles:

:host ::ng-deep h2 {
    color: red;
}

Custom theme for Angular Material

You can change a set of colors that will be applied to the Angular Material components. https://material.angular.io/guide/theming

Abdelrhman Arnos
  • 1,404
  • 3
  • 11
  • 22
0

Without hack, it's not possible to change on the fly the colors of you angular application because the way to style your application using Angular Material is to use theming (that is described here https://material.angular.io/guide/theming) and your theming scss files will be compiled to css at build time.

I think the only hack to change colors of your app on the fly is to identify the hex color that you want to change (for primary, secondary, etc...) and you rewrite your style tag in your document using serch / replace.

But keep in mind that Angular Material is not designed to let user change theme and colors because they provide palette and hue that have been designed to provide good contrast and best practice regarding UX. That's why the solution I give you is really a hack.

thib_o_o
  • 156
  • 5
0

the matrial dynamic color will be possible if the main set by css variable instaid of static hex values ,someone already publish a library to solve this by overwrite the main color to be used by css variables.

1️⃣ install angular-material-css-vars library npm i angular-material-css-vars -S

2️⃣ import MaterialCssVarsModule on app module

@NgModule({
  imports: [
    ...
    MaterialCssVarsModule.forRoot(),
  ],
   ...
})
export class AppModule {}

3️⃣ use this service MaterialCssVarsService to change angular material main color like primary ,secondary, warm...

export class component {
  constructor(public materialCssVarsService: MaterialCssVarsService) {
    const hex = "#3f51b5"; // set default color color
    this.materialCssVarsService.setPrimaryColor(hex);
  }

  setPrimaryColor(color: string) {
    this.materialCssVarsService.setPrimaryColor(color);
  }
}

you need to remove any existing @import '~@angular/material/theming'; from your main stylesheet file.

demo ‍♂️

you may check this question create angular material theme with css variables for more help

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91