skip to content

In Angular Material 22, when do you change a system token with `mat.theme-overrides` instead of a component's overrides mixin, and what does each one reach?

level: seniorimportance: should knowfreq 31%

answer

  1. one value everywhere vs one component
  2. component token falls back to system
  3. custom var() readers change too
  4. plain value replaces light-dark pair
  5. $overrides argument on the theme

basics

~20 s

mat.theme-overrides, or mat.theme's $overrides map, rewrites a --mat-sys system variable, so every component and custom style reading it changes under that selector. A component overrides mixin sets only that component's tokens, which win over the system fallback.

solid answer

~40 s

Material components read a component token first and fall back to a system variable, for example a card background is `var(--mat-card-elevated-container-color, var(--mat-sys-surface-container-low))`. `mat.theme-overrides((primary: ...))` rewrites `--mat-sys-primary` under the current selector, so filled buttons, checkboxes, sliders and your own components that read the variable all change; the same keys can go in `mat.theme`'s `$overrides` argument when you emit the theme. `mat.card-overrides(...)` sets only card tokens, and because those are read before the fallback, they win over any system value. Use system overrides for brand rules that should apply everywhere, component overrides for exceptions. Two traps: a plain color replaces the theme's `light-dark()` pair, so the value no longer changes with the scheme unless you pass `light-dark()` yourself, and `mat.theme-overrides` emits only names it recognises as system tokens.

code

scss · 23 lines
scss
@use '@angular/material' as mat;

html {
  color-scheme: light dark;
  @include mat.theme((
    color: mat.$rose-palette,
    typography: Roboto,
  ), $overrides: (
    primary: light-dark(#7a1f2b, #ffb3b5),
  ));
}

// One panel gets a different container role
.waitlist-panel {
  @include mat.theme-overrides((
    primary-container: light-dark(#ffd9dc, #5c1020),
  ));
}

// Cards alone get a squarer corner
html {
  @include mat.card-overrides((elevated-container-shape: 4px));
}

go deeper

for a junior

Recall that mat.theme-overrides changes --mat-sys system values for everything, while a component mixin such as mat.card-overrides changes one component.

for a middle

Explain the fallback chain from component token to system token and why it makes component overrides win over system overrides.

for a senior

Choose between the two for each brand rule, scope them with selectors, keep light and dark values with light-dark(), and know that theme-overrides skips unknown names silently.

for a principal

Shape the theme file so brand decisions live once at the system level and component overrides stay rare, documented exceptions.

## Two layers of tokens Angular Material 22 styles its components through two layers of CSS custom properties: - **System tokens**, the `--mat-sys-*` variables that `mat.theme` declares: color roles such as `primary` and `surface-container-low`, the type scale, shapes such as `corner-medium`, elevation levels and state values. - **Component tokens**, such as `--mat-card-elevated-container-color` or `--mat-button-filled-container-color`, which belong to one component and are normally left undeclared. Each component's stylesheet reads its component token and falls back to a system token. A raised card's background is effectively: ```scss background-color: var( --mat-card-elevated-container-color, var(--mat-sys-surface-container-low) ); ``` That fallback chain decides what each override API reaches. ## What each API changes | API | Writes | Reaches | |---|---|---| | `mat.theme-overrides((primary: ...))` | `--mat-sys-primary` under the current selector | every component token that falls back to it, plus your own `var(--mat-sys-primary)` styles | | `mat.theme(config, $overrides: (...))` | the same system variables, as the theme is emitted | the same, at the selector the theme is included on | | `mat.card-overrides((...))` | `--mat-card-*` under the current selector | cards only | Order of resolution for a raised card, read by the browser: 1. Is `--mat-card-elevated-container-color` set on the card or an ancestor? Use it. 2. Otherwise use `--mat-sys-surface-container-low`, whatever value the nearest theme or system override gave it. So wherever both reach the card, the component override beats the system override for that property, regardless of which one comes later in the stylesheet, because the component token is checked first. ## Choosing Use **system overrides** when a rule in the brand book is about the whole interface: - the institution's primary color should drive buttons, selection controls, focus and custom banners; - every surface container should be a touch warmer; - one subtree, such as a destructive-action panel, should use a different role value. Use **component overrides** when the rule is about one component: - cards get a 4px radius while FABs keep their rounder default; - outlined form fields get a darker outline; - table rows get a different height. Reaching for a component mixin to apply a brand color means repeating it in every component that uses the role, and missing the ones you forgot, including your own components. Reaching for a system override to change one component changes everything else that reads that role: rewriting `corner-medium` to round cards also reshapes FABs and stepper elements in that scope. ## Scope Both kinds are inherited custom properties, so both are scoped by the selector they are included under, and a top-level include lands on `html`. A registration portal can set its brand primary on `html` and give the waitlist panel a different `primary-container` with `mat.theme-overrides` on `.waitlist-panel`; both Material components and the portal's own banner inside that panel pick up the new value. ## A worked decision The registration portal's brand book has four rules. Sorting them by reach: 1. "Our maroon is the primary action color." Every component that uses the primary role should follow, and so should the portal's own banners, so it is a **system** value: `primary` in the theme's `$overrides`. 2. "Waitlist panels use a softer container color." One region, every component in it: a **scoped system** override of `primary-container` on `.waitlist-panel`. 3. "Cards have 4px corners." One component, everywhere: a **component** override with `mat.card-overrides`. 4. "Outlined fields have a darker outline." One component, everywhere: `mat.form-field-overrides`. Rule 3 shows why the choice matters. Doing it with `corner-medium` would also square off FABs and stepper elements, which the brand book never asked for. ## Light and dark pitfalls With a color-scheme theme, every color system variable is written as `light-dark(light, dark)`. An override replaces the whole value with what you pass: - `mat.theme-overrides((surface: #ffffff))` makes the surface white in dark mode too; - `mat.theme-overrides((surface: light-dark(#ffffff, #1a1a1a)))` keeps both schemes. The same applies to component overrides with plain colors. ## Validation differences Component mixins validate every key and fail the build on an unknown one. `mat.theme-overrides` emits only names it recognises as system tokens and does not raise an error for others, so a misspelled role name there simply has no effect; check the output when a system override seems to do nothing. ## Common mistakes - Expecting a later `mat.theme-overrides` to beat an earlier `mat.card-overrides` on the card. - Recoloring the brand primary in a dozen component mixins. - Overriding colors with plain hex values in a light-and-dark theme. - Writing a role name that is not a system token and assuming it applied.

  • A card inside `.notice` gets both `mat.theme-overrides((surface-container-low: ...))` and `mat.card-overrides((elevated-container-color: ...))`; which color shows?
    The card override. The card reads `--mat-card-elevated-container-color` first and only falls back to `--mat-sys-surface-container-low` when the component token is unset, so stylesheet order does not matter here.
  • Why might `mat.theme-overrides((primary-colour: ...))` appear to do nothing?
    It emits only names it recognises as system tokens and skips the rest without an error, unlike the component mixins, which fail the build on an unknown key. The correct key is `primary`; check the generated CSS when a system override has no visible effect.

  • Thinks a later theme-overrides beats an earlier card-overrides for cards.
  • Applies the brand primary color separately in every component mixin.
  • Overrides a system color with a plain hex and expects dark mode to adapt.
  • Believes theme-overrides only affects Material components, not custom var() styles.
  • Rewrites corner-medium to round cards and expects nothing else to change.

Assumes Angular Material 22; mat.theme-overrides and the $overrides argument of mat.theme belong to the Material 3 theme API introduced in Material 19.