10. Angular Material Theming
Design tokens, custom palettes, dark mode, and dynamic theme switching
Angular Material ships with a powerful theming system built on Sass and CSS custom properties. Instead of overriding individual component styles, you define a theme — a set of palettes and typography — and Material applies it consistently across every component. This article covers how the theming system works, how to build custom themes, and how to implement dynamic theme switching.
How Material Theming Works
A Material theme is built from three layers:
- Color palettes — a primary color, a secondary (accent) color, and a warn (error) color, each with multiple tones
- Typography — font families, sizes, and weights for different text roles
- Density — component spacing (compact, default, comfortable)
Material 3 (M3), the current system as of Angular 18+, uses design tokens — CSS custom properties that every component references. Change a token, and every component that uses it updates automatically.
Setting Up a Custom Theme
Step 1: Define your theme in Sass
// styles/theme.scss
@use '@angular/material' as mat;
// Define custom palettes from a single source color
$my-theme: mat.define-theme((
color: (
theme-type: light,
primary: mat.$azure-palette,
tertiary: mat.$violet-palette,
),
typography: (
brand-family: 'Inter, sans-serif',
plain-family: 'Inter, sans-serif',
),
density: (
scale: 0,
),
));
// Apply the theme globally
html {
@include mat.all-component-themes($my-theme);
}
Step 2: Include in your build
Make sure your angular.json includes the theme file:
{
"styles": [
"src/styles/theme.scss",
"src/styles.scss"
]
}
That's it. Every Material component in your app now uses your custom palettes and typography.
Built-in Palettes
Material provides pre-defined palettes you can use directly:
| Palette | Hue | Use case |
|---|---|---|
mat.$red-palette |
Red | Errors, warnings, destructive actions |
mat.$blue-palette |
Blue | Primary, links, info |
mat.$green-palette |
Green | Success, confirmations |
mat.$azure-palette |
Azure | Primary alternative |
mat.$violet-palette |
Violet | Accent, creative, secondary |
mat.$yellow-palette |
Yellow | Warnings, attention |
mat.$orange-palette |
Orange | Warm accent |
mat.$cyan-palette |
Cyan | Info, secondary actions |
mat.$magenta-palette |
Magenta | Highlight |
mat.$chartreuse-palette |
Chartreuse | Nature, fresh |
mat.$rose-palette |
Rose | Soft accent |
Custom Palettes from a Source Color
For maximum brand alignment, generate a palette from a single hex color:
@use '@angular/material' as mat;
$my-theme: mat.define-theme((
color: (
theme-type: light,
primary: mat.$azure-palette,
tertiary: mat.$rose-palette,
),
));
Material's palette generation algorithm takes your source color and produces a full tonal palette (50, 100, 200... 900) with proper contrast ratios, following the M3 color system specification.
Dark Mode
Static dark theme
$dark-theme: mat.define-theme((
color: (
theme-type: dark,
primary: mat.$azure-palette,
tertiary: mat.$rose-palette,
),
));
html {
@include mat.all-component-themes($dark-theme);
}
Media-query based
$light-theme: mat.define-theme((
color: (
theme-type: light,
primary: mat.$azure-palette,
tertiary: mat.$violet-palette,
),
));
$dark-theme: mat.define-theme((
color: (
theme-type: dark,
primary: mat.$azure-palette,
tertiary: mat.$violet-palette,
),
));
html {
@include mat.all-component-themes($light-theme);
@media (prefers-color-scheme: dark) {
@include mat.all-component-colors($dark-theme);
}
}
Note: for the dark variant, use all-component-colors instead of all-component-themes — you only need to override colors, not typography and density.
Class-based toggle
For user-controlled theme switching:
html {
@include mat.all-component-themes($light-theme);
&.dark-theme {
@include mat.all-component-colors($dark-theme);
}
}
Then toggle the class in your component:
@Component({
selector: 'app-theme-toggle',
template: `
<button mat-icon-button (click)="toggle()">
<mat-icon>{{ isDark() ? 'light_mode' : 'dark_mode' }}</mat-icon>
</button>
`
})
export class ThemeToggleComponent {
isDark = signal(false);
constructor() {
effect(() => {
document.documentElement.classList.toggle('dark-theme', this.isDark());
});
}
toggle() {
this.isDark.update(d => !d);
}
}
Component-Level Theming
You can apply different themes to different parts of your app:
.admin-section {
@include mat.all-component-colors($admin-theme);
}
.customer-section {
@include mat.all-component-colors($customer-theme);
}
This scopes Material's CSS custom properties to the container element. Components inside .admin-section use the admin palette; components inside .customer-section use the customer palette.
Overriding Individual Component Styles
Sometimes you need to customize a specific component beyond what the theme provides. Use the component-specific theme mixins:
@use '@angular/material' as mat;
// Override just the button colors
.special-button {
@include mat.button-color($my-special-theme);
}
// Override card density
.compact-cards {
@include mat.card-density(-1);
}
Available component mixins
Each Material component has three mixins:
mat.<component>-color($theme)— colors onlymat.<component>-typography($theme)— typography onlymat.<component>-density($scale)— density onlymat.<component>-theme($theme)— all three
Typography Customization
Material defines typography through type scale roles:
$my-theme: mat.define-theme((
typography: (
brand-family: '"Fraunces", serif',
plain-family: '"Inter", sans-serif',
bold-weight: 700,
medium-weight: 500,
regular-weight: 400,
),
));
| Role | Description | Default |
|---|---|---|
brand-family |
Headlines, display text | Roboto |
plain-family |
Body, labels, captions | Roboto |
bold-weight |
Bold text weight | 700 |
medium-weight |
Medium text weight | 500 |
regular-weight |
Regular text weight | 400 |
Theme Service for Dynamic Theming
For apps that need runtime theme switching (e.g., user preferences, multi-tenant white-labeling):
@Injectable({ providedIn: 'root' })
export class ThemeService {
private themeMode = signal<'light' | 'dark'>('light');
private primaryHue = signal(210); // HSL hue
private accentHue = signal(280);
isDark = this.themeMode.asReadonly();
constructor() {
// Sync with system preference
const mq = window.matchMedia('(prefers-color-scheme: dark)');
this.themeMode.set(mq.matches ? 'dark' : 'light');
// Persist to localStorage
effect(() => {
localStorage.setItem('theme-mode', this.themeMode());
});
// Apply CSS custom properties
effect(() => {
const root = document.documentElement;
root.classList.toggle('dark-theme', this.themeMode() === 'dark');
root.style.setProperty('--app-primary-hue', String(this.primaryHue()));
root.style.setProperty('--app-accent-hue', String(this.accentHue()));
});
}
toggleMode() {
this.themeMode.update(m => m === 'light' ? 'dark' : 'light');
}
setPrimaryHue(hue: number) {
this.primaryHue.set(hue);
}
setAccentHue(hue: number) {
this.accentHue.set(hue);
}
}
Companion CSS for dynamic properties
:root {
--app-primary-hue: 210;
--app-accent-hue: 280;
}
// Use these in your own components alongside Material
.app-highlight {
background: hsl(var(--app-primary-hue), 60%, 50%);
}
.app-accent-text {
color: hsl(var(--app-accent-hue), 50%, 60%);
}
Tree-Shaking: Include Only What You Use
all-component-themes includes styles for every Material component. For smaller bundles, include only what you need:
@use '@angular/material' as mat;
$my-theme: mat.define-theme(( /* ... */ ));
// Core styles (required)
@include mat.core();
// Only include components you actually use
@include mat.button-theme($my-theme);
@include mat.card-theme($my-theme);
@include mat.input-theme($my-theme);
@include mat.toolbar-theme($my-theme);
@include mat.icon-theme($my-theme);
@include mat.dialog-theme($my-theme);
@include mat.table-theme($my-theme);
This can significantly reduce CSS bundle size in apps that only use a handful of Material components.
Theming Your Own Components
Follow the same pattern Material uses — reference CSS custom properties so your components automatically adapt to theme changes:
// my-widget.component.scss
:host {
// Reference Material's CSS custom properties
background: var(--mat-app-surface);
color: var(--mat-app-on-surface);
border: 1px solid var(--mat-app-outline);
border-radius: var(--mat-app-corner-medium);
}
.widget-header {
background: var(--mat-app-primary);
color: var(--mat-app-on-primary);
}
.widget-accent {
color: var(--mat-app-tertiary);
}
| Token | Light mode | Dark mode |
|---|---|---|
--mat-app-surface |
White | Dark grey |
--mat-app-on-surface |
Dark text | Light text |
--mat-app-primary |
Primary color | Primary (adjusted for dark) |
--mat-app-on-primary |
White | Dark text |
--mat-app-outline |
Light grey | Mid grey |
--mat-app-tertiary |
Accent color | Accent (adjusted for dark) |
Your component will automatically adapt to light/dark mode and any custom palette — no extra work needed.
Try It Yourself -- Theme Builder
Pick colors for primary, secondary, and error palettes. Toggle dark mode. Watch how Material components and design tokens update in real time.
Common Patterns
Multi-tenant theming
@Injectable({ providedIn: 'root' })
export class TenantThemeService {
private tenantConfig = signal<TenantConfig | null>(null);
constructor() {
effect(() => {
const config = this.tenantConfig();
if (!config) return;
const root = document.documentElement;
root.style.setProperty('--mat-app-primary', config.primaryColor);
root.style.setProperty('--mat-app-tertiary', config.accentColor);
if (config.fontFamily) {
root.style.setProperty('--mat-app-body-font', config.fontFamily);
}
});
}
loadTenant(tenantId: string) {
// Fetch tenant config from API and apply
fetch(`/api/tenants/${tenantId}/theme`)
.then(r => r.json())
.then(config => this.tenantConfig.set(config));
}
}
High-contrast mode
@media (forced-colors: active) {
// Material handles most of this automatically,
// but you may need adjustments for custom components
.my-widget {
border: 2px solid ButtonText;
color: ButtonText;
background: Canvas;
}
}
// Or a manual toggle:
html.high-contrast {
@include mat.all-component-colors($high-contrast-theme);
}
What's Next
Theming controls how components look. The next article goes deeper into the CDK — Angular's Component Dev Kit — starting with Virtual Scrolling: how the CDK renders only the visible items in a long list by recycling DOM elements as you scroll.