
Security News
Engineering with AI Podcast: The Promise of AI-First Development
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.
angular-material-css-vars
Advanced tools

You want to style your angular material dynamically with all the colors in the rainbow? Look no further!
npm i angular-material-css-vars -S
@import '~@angular/material/theming'; from your main stylesheet file if present.@import '~angular-material-css-vars/main';
// optional
$mat-css-dark-theme-selector: '.isDarkTheme';
$mat-css-light-theme-selector: '.isLightTheme';
// init theme
@include init-material-css-vars() {
// If your app has any theme mixins, call them here.
// $mat-css-theme gets set to an appropriate value before this content is called.
// @include your-custom-component-theme($mat-css-theme);
};
import {MaterialCssVarsModule} from 'angular-material-css-vars';
@NgModule({
imports: [
MaterialCssVarsModule.forRoot({
// all optional
isAutoContrast: true,
darkThemeClass: 'isDarkTheme',
lightThemeClass: 'isLightTheme',
// ...
}),
],
})
export class AppModule {
}
import {MaterialCssVarsService} from 'angular-material-css-vars';
export class SomeComponentOrService {
constructor(public materialCssVarsService: MaterialCssVarsService) {
const hex = '#3f51b5';
this.materialCssVarsService.setDarkTheme(true);
this.materialCssVarsService.setPrimaryColor(hex);
this.materialCssVarsService.setAccentColor('#333');
}
}
setAutoContrastEnabled(bool)setContrastColorThreshold(hueVal: HueValue)setVariable(cssVarName: MaterialCssVariables, value: string)MaterialCssVariables enum from here to make sure you get the variable name right.setDarkTheme(isDark: boolean)There are also several utility functions and mixins.
@import '~angular-material-css-vars/public-util';
.with-color {
border-color: mat-css-color-primary(300);
}
.color-and-contrast {
@include mat-css-color-and-contrast(300);
}
.with-bg {
@include mat-css-bg(300);
}
There are also some additional hacks (e.g. adding a color to the elevation shadow) available in case you need them.
You can provide different options before initialization to change the body class used for the dark theme and to provide different default styles:
// $mat-css-default-light-theme: ... ;
// $mat-css-text: ... ;
$mat-css-dark-theme-selector: '.isDarkTheme';
$mat-css-light-theme-selector: '.isLightTheme';
@include init-material-css-vars();
To make those variables take effect with your mixins, you need to make sure that they are also defined before using them. E.g.:
// probably best put in a common variables file and imported before the mixins
$mat-css-dark-theme-selector: '.isDarkThemeCUSTOM';
@import '~angular-material-css-vars/public-util';
.my-component {
@include mat-css-dark-theme {
// dark theme styles ...
}
}
A full list of the theme map can be found here.
There are two ways to set the default fallback theme. One is using the mat-css-palette-defaults mixin.
@import '../projects/material-css-vars/src/lib/public-util';
@import '../projects/material-css-vars/src/lib/main';
@include init-material-css-vars();
@include mat-css-set-palette-defaults($mat-light-blue, 'primary');
@include mat-css-set-palette-defaults($mat-pink, 'accent');
@include mat-css-set-palette-defaults($mat-red, 'warn');
The other is to include your own variables for $mat-css-default-light-theme.
@import '../projects/material-css-vars/src/lib/main';
$mat-css-default-light-theme: map-merge(
// if you don't want to enter ALL the properties
$mat-css-default-light-theme,
(
--palette-primary-50: _mat-css-hex-to-rgb(#e1f5fe),
--palette-primary-100: _mat-css-hex-to-rgb(#b3e5fc),
--palette-primary-200: _mat-css-hex-to-rgb(#81d4fa),
--palette-primary-300: _mat-css-hex-to-rgb(#4fc3f7),
--palette-primary-400: _mat-css-hex-to-rgb(#29b6f6),
--palette-primary-500: _mat-css-hex-to-rgb(#03a9f4),
// ...
)
);
@include init-material-css-vars();
This lib won't work with IE11 but thanks to @Coly010 there is a workaround for that too.
The init-material-css-vars mixin allows content to be passed into it. This allows you to create app themes that can take advantage of the dynamic theme created inside this mixin. It may be possible to do all theming using the utility mixins outlined above, but in other cases, you may need access to the theme palette, including foreground and background palettes.
See the Material guide on Theming your custom component for more information.
...goes to @zbirizdo project on which parts of this are based which is in turn supposedly based on this gist.
Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.
FAQs
Little library to use css variables for @angular/material
The npm package angular-material-css-vars receives a total of 2,596 weekly downloads. As such, angular-material-css-vars popularity was classified as popular.
We found that angular-material-css-vars demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.

Research
/Security News
A five-month operation turned 27 npm packages into durable hosting for browser-run lures that mimic document-sharing portals and Microsoft sign-in, targeting 25 organizations across manufacturing, industrial automation, plastics, and healthcare for credential theft.

Research
Fake “Phantom Shuttle” VPN Chrome extensions (active since 2017) hijack proxy auth to intercept traffic and continuously exfiltrate user credentials to attacker infrastructure.