In Angular Material 22, what are the `--mat-sys-*` CSS variables, and how do you use them to style your own components?
answer
- output of the theme mixin
- named after Material color roles
- read with var() in your styles
- container and on-container pairs
- light-dark values follow color-scheme
basics
~20 sThey are CSS custom properties that Angular Material's mat.theme mixin declares for the theme's color roles, type scale, shapes and elevation. Your own styles read them, such as background: var(--mat-sys-surface), and follow the theme, light and dark values included.
solid answer
~40 s`mat.theme` turns the theme config into CSS custom properties on the selector you include it under, usually `html`: color roles such as `--mat-sys-primary`, `--mat-sys-surface`, `--mat-sys-on-surface` and `--mat-sys-outline-variant`, type-scale values such as `--mat-sys-body-large`, shapes such as `--mat-sys-corner-medium`, and elevation levels `--mat-sys-level0` to `--mat-sys-level5`. Material's own components resolve most of their colors from the same variables, so a custom component that reads them with `var()` matches the theme and changes with it. Use the roles in pairs, such as a `primary-container` background with `on-primary-container` text, because each `on-*` role is the color designed to sit on its partner. A color-scheme theme writes each color as `light-dark(...)`, so one declaration works in light and dark mode. If you prefer classes, `mat.system-classes()` emits utilities like `mat-bg-surface` that read the same variables.
code
scss · 17 lines@use '@angular/material' as mat;
html {
color-scheme: light dark;
@include mat.theme((
color: mat.$azure-palette,
typography: Roboto,
density: 0,
));
}
.enrolment-notice {
background: var(--mat-sys-primary-container);
color: var(--mat-sys-on-primary-container);
border-radius: var(--mat-sys-corner-medium);
font: var(--mat-sys-body-large);
}go deeper
Remember that the theme produces --mat-sys-* CSS variables and that your own styles read them with var(), such as var(--mat-sys-surface) for a background.
Explain which variable groups the theme emits, why typography ones appear only when configured, and why container and on-container roles are used as pairs.
Show how light-dark() values and scoped theme calls let custom components follow a scheme or a sub-theme with no extra rules, and why Sass cannot transform these values.
Argue for building in-house components on the system roles rather than literal colors, so a later brand or scheme change touches the theme file and nothing else.
## What the system variables are When an Angular Material 22 app builds its theme with the Sass mixin `mat.theme`, the output is not a pile of finished component styles. It is a set of **CSS custom properties** (CSS variables) whose names start with `--mat-sys-`. They are called **system variables** because they hold the *system level* of the Material 3 design language: the color roles, the type scale, the corner shapes and the elevation shadows that every component draws from. A custom property is a value the browser resolves at runtime, so anything on the page can read it with `var()`. That is the whole point for your own code: a banner, a sidebar or a summary panel you write yourself can take its colors and fonts from the same source as Material's buttons and cards, and it will change whenever the theme does. ## What gets declared `mat.theme` declares color and elevation variables only when its config has a `color` key, and type-scale variables only when it has a `typography` key; the shape and state variables are emitted on every call. If the config has no `typography` key, a `var(--mat-sys-body-large)` in your stylesheet resolves to nothing. | Category | Example variables | Typical use in your own styles | |---|---|---| | Color roles | `--mat-sys-primary`, `--mat-sys-on-primary`, `--mat-sys-surface`, `--mat-sys-on-surface`, `--mat-sys-outline-variant`, `--mat-sys-error` | backgrounds, text, borders | | Type scale | `--mat-sys-body-large`, `--mat-sys-title-medium`, plus parts such as `--mat-sys-body-large-size` | the `font` shorthand or a single font property | | Shape | `--mat-sys-corner-small`, `--mat-sys-corner-medium`, `--mat-sys-corner-full` | `border-radius` | | Elevation | `--mat-sys-level0` to `--mat-sys-level5` | `box-shadow` | The full role list follows Material 3: `primary`, `secondary` and `tertiary` with their `on-*`, `*-container` and `on-*-container` partners, the `surface` family (`surface-container-lowest` up to `surface-container-highest`), `outline`, `outline-variant`, `error` and the inverse roles. ## Using them in a component A course-registration portal wants a notice banner that looks like part of the Material UI around it: - set `background` from a container role and `color` from its matching `on-*` role; - use `--mat-sys-outline-variant` for a quiet border; - take the font from a type-scale variable instead of hard-coding a size; - take the radius from a shape variable so it matches cards. ```scss .enrolment-notice { background: var(--mat-sys-primary-container); color: var(--mat-sys-on-primary-container); border: 1px solid var(--mat-sys-outline-variant); border-radius: var(--mat-sys-corner-medium); font: var(--mat-sys-body-large); } ``` If the team prefers template classes, including `mat.system-classes()` in the theme file emits utility classes that read the same variables, for example `mat-bg-surface`, `mat-text-on-surface` and `mat-font-body-lg`. ## How Material's own components use them Material's components read the same variables, one step removed. Each component has its own **component tokens**, such as a card's container color, and its stylesheet reads each one with a system variable as the fallback. A raised card's background is effectively `var(--mat-card-elevated-container-color, var(--mat-sys-surface-container-low))`. Unless someone overrides the card token, the card shows the system value. That is why your custom component and Material's components stay in step: - both end at the same `--mat-sys-*` values; - a scheme switch changes those values once, for everything; - a sub-theme on a container changes them for that subtree, for Material's components and yours alike. It also shows the limit. A custom component that copies the card's *look* by reading `--mat-sys-surface-container-low` will not follow a later card override, because that override sets the card's own token, not the system value. ## Why the pairs matter Material 3 defines its colors as **roles**, not as shades. The `on-*` role is the color chosen to be legible on its partner: `on-primary` on `primary`, `on-surface` on `surface`, `on-primary-container` on `primary-container`. Mixing roles from different pairs, such as `on-surface` text on a `primary` background, is how custom components end up with poor contrast in one of the two color schemes even though they looked fine in the other. ## Light, dark and scoped themes 1. With a color-scheme theme, every color variable is written as `light-dark(light value, dark value)`, so the browser picks the right one from the element's `color-scheme`; your component needs no dark-mode rules of its own. 2. Because the variables are inherited custom properties, a second `mat.theme` call on a container redefines them for that subtree, and your component inside it follows automatically. 3. Sass cannot read them. They exist only in the browser, so Sass color functions such as `color.adjust` cannot lighten `var(--mat-sys-primary)` at build time; use the role meant for the job, such as a container role, instead. ## Common mistakes - Copying a hex value out of DevTools into a component stylesheet, which freezes it and breaks the moment the theme or the scheme changes. - Reading a component's own token, such as a card's variable, from an unrelated element; those tokens are normally left undeclared so the component falls back to a system variable. - Expecting typography variables when the theme was built with color only. - Treating `--mat-sys-*` names as something to invent; only the names the theme emits exist.
- Why might `var(--mat-sys-title-large)` resolve to nothing in a component's stylesheet?`mat.theme` emits the type-scale variables only when its config has a `typography` key. If the theme was built with `color` and `density` but no `typography`, none of them exist, so the declaration that uses it is invalid at computed-value time. Add `typography` to the theme config, or a font family that the theme can build the scale from.
- Can you darken `--mat-sys-primary` with a Sass color function for a hover state?No. The variable is resolved by the browser, and Sass runs at build time, so it only ever sees the text `var(--mat-sys-primary)`. Pick a role meant for that purpose, such as a container role, or use a CSS-side technique such as `color-mix()` on the variable.
- Calls --mat-sys-* names Sass variables that exist only at build time.
- Copies computed hex values from DevTools into component styles.
- Puts on-surface text on a primary background and expects good contrast.
- Writes separate dark-mode rules for colors already defined with light-dark().
- Expects typography variables when the theme config had no typography.
Assumes Angular Material 22; the --mat-sys-* system variables are what the Material 3 mat.theme mixin emits, the default theming API since Material 19.