Branded Angular Material Themes With One Build
Define branded Angular Material themes with the modern Sass API and toggle them by host class without multiple builds.
14 Aug 2026, 03:32 UTC

The problem: default colors and multiple builds
Angular Material ships with a default indigo and pink palette. If your product needs a branded primary color, you face a common choice: override the theme globally and ship every component’s styles, or maintain separate builds for each brand variant. Both approaches bloat the CSS or the release pipeline.
The useful takeaway is that you can define one custom light theme per brand variant with the modern Sass theming API, scope it under a CSS class on the host element, and switch variants at runtime without rebuilding.
Use the modern Sass theming API
Angular Material 12 and later replace the legacy @import '~@angular/material/theming' with the module syntax @use '@angular/material' as mat;. Sass is a CSS preprocessor that compiles to standard CSS before the browser sees it. A mixin is a reusable Sass block that emits CSS when included.
The API centers on three pieces:
mat.define-palettecreates a color palette from a brand hue, lighter and darker shades, and a contrast color.mat.define-light-themecombines primary, accent, and warn palettes into a theme object.mat.all-component-themes, or individual component mixins, emit the actual component styles.
Worked example: scoped brand theme
Assume an Angular CLI workspace with Angular Material 12 or later installed. Create src/styles/_brand-theme.scss:
@use 'sass:map';
@use '@angular/material' as mat;
$brand-primary: mat.define-palette((
50: #e3f2fd,
100: #bbdefb,
500: #1976d2,
900: #0d47a1,
), (
default: 500,
lighter: 100,
darker: 900,
contrast-color: #ffffff
));
$brand-theme: mat.define-light-theme((
color: (
primary: $brand-primary,
accent: mat.define-palette((
500: #ff4081,
), (
default: 500,
)),
warn: mat.define-palette((
500: #f44336,
), (
default: 500,
)),
)
));
Then expose it under a host class in src/styles.scss:
@use './brand-theme' as brand;
.mat-core {
@include mat.core();
}
.app-brand-theme {
@include mat.all-component-themes(brand.$brand-theme);
}
Apply the class to the root host element, typically src/app/app.component.html:
<div class="app-brand-theme">
<router-outlet></router-outlet>
</div>
Only components nested inside .app-brand-theme receive the branded styles. To toggle variants, add another class such as .app-brand-theme-alt and switch the class on the host element at runtime.
Run the build from the project root:
ng build
No special permissions are required beyond normal file access to the workspace. Replace ng build with ng serve for local development. A failed build usually indicates a Sass version mismatch or an invalid palette map.
Verify the output
- Open
package.jsonand confirm@angular/materialis version 12 or later. - Run
ng buildand inspect the generated stylesheet indist/<project>/. - Search for your primary hex value, for example
#1976d2, and confirm it appears in component rules scoped under.app-brand-theme. - Confirm
mat-corebase styles appear once, and that unused component themes are not duplicated if you switch to individual mixins.
Trade-offs and limitations
- A Sass build step is required. The Angular CLI supports it, but Sass APIs and module paths change between major versions, so check your installed version before copying examples.
- Including
mat.all-component-themescan significantly increase CSS size. For production, include only the component themes you use, such asmat.button-themeormat.card-theme. - This example covers a light theme only. It does not automatically adapt to system dark mode; you would need a separate dark theme definition and a media query or class toggle.
- Theme switching by class is a runtime CSS change, not a build-time artifact, so there is no build rollback. Reverting means removing the host class or restoring the previous stylesheet.
Closing
Start with one branded light theme, scope it under a host class, and verify the compiled CSS contains your brand color. If the bundle grows too large, replace all-component-themes with per-component mixins before adding dark mode or additional brand variants.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.