skip to content

In Angular Material 22, how do `color-scheme` and the CSS `light-dark()` function switch a `mat.theme` between light and dark?

level: middleimportance: should knowfreq 45%

answer

  1. one theme, both modes
  2. default theme-type is color-scheme
  3. each value is a light-dark pair
  4. nothing declared means light
  5. OS preference or a toggled class

basics

~20 s

By default mat.theme writes each colour variable as a light-dark(light, dark) pair, and the browser picks one from the color-scheme in effect. Declare color-scheme: light dark to follow the operating system, or light or dark on a toggled class.

solid answer

~40 s

Unless you set another `theme-type`, `mat.theme` emits every `--mat-sys-*` colour as `light-dark(lightValue, darkValue)`. The CSS `light-dark()` function returns its first argument when the element's used colour scheme is light and its second when dark, and that scheme comes from the `color-scheme` property. So `html { color-scheme: light dark; }` follows the operating system, while `color-scheme: dark` on a class such as `body.dark-mode` gives a manual toggle. With no `color-scheme` declared anywhere, the light values always apply. Angular Material itself adds no `prefers-color-scheme` queries. For browsers without `light-dark()`, set `theme-type: light` or `dark` in the color map to emit one set of plain values. The prebuilt CSS files are compiled that way, which is why they never switch.

code

scss · 14 lines
scss
@use '@angular/material' as mat;
@use './theme-colors' as transit;

html {
  color-scheme: light dark; // follow the operating system
  @include mat.theme((
    color: (primary: transit.$primary-palette),
    typography: Roboto,
  ));
}

// Rider override, set from the app. Remove the class to follow the OS again.
body.theme-light { color-scheme: light; }
body.theme-dark { color-scheme: dark; }

go deeper

for a junior

Recall that one mat.theme holds both modes and that color-scheme picks the mode. Know that light dark follows the operating system.

for a middle

Explain light-dark() resolving from the inherited color-scheme, the theme-type options, and why the prebuilt files and the generated body rule pin a mode.

for a senior

Show you can ship a toggle on top of the OS setting with inheritance in mind, and plan a theme-type fallback for browsers without light-dark().

for a principal

Weigh a single light-dark() build against separate light and dark builds for browser reach and CSS size, and keep the mode policy in one place for every app.

## How the colour values are written `mat.theme` has a `theme-type` setting inside its color map. Its default, and the only mode a single-palette `color` value gets, is **`color-scheme`**. In that mode the mixin computes a light and a dark value for every system colour and writes both into one declaration: ```css html { --mat-sys-primary: light-dark(#984061, #ffb0c8); --mat-sys-on-primary: light-dark(#ffffff, #5e1133); } ``` (The hex values are illustrative.) **`light-dark()`** is a CSS colour function: it resolves to its first argument when the element's used colour scheme is light and to its second when it is dark. The used colour scheme comes from the **`color-scheme`** property, which is inherited. So a single compiled theme carries both modes, and switching between them is one CSS property, with no second stylesheet and no recompilation. ## Who decides the mode | `color-scheme` in effect | Colours shown | Use it for | |---|---|---| | not declared anywhere | always light | (usually an oversight) | | `light` | light | an app that is light only, or the default of a toggle | | `dark` | dark | a dark-only app, or the dark side of a toggle | | `light dark` | follows the operating system | an app that respects the OS setting | Angular Material **does not** add `prefers-color-scheme` or `prefers-contrast` media queries for you. You choose: rely on `color-scheme: light dark`, write your own media queries, or read a saved preference and set a class. The `color-scheme` property also tells the browser to draw its own form controls and scrollbars in the matching mode, so native elements stay consistent with the Material ones. ## The `theme-type` options | `theme-type` | Emitted values | When | |---|---|---| | `color-scheme` (default) | `light-dark()` pairs | the normal case | | `light` | plain light values only | browsers without `light-dark()`, or a light-only product | | `dark` | plain dark values only | the same, dark | With `light` or `dark`, `color-scheme` has nothing to choose between. That is exactly how the four prebuilt M3 CSS files were compiled: `azure-blue.css` and `rose-red.css` are light, `cyan-orange.css` and `magenta-violet.css` are dark, and none of them reacts to `color-scheme`. ## A journey planner that follows the OS but lets riders override it The transit app should match the phone's setting, yet a rider on a bright platform may want light mode at night. One theme plus two classes covers it: 1. Declare `color-scheme: light dark` on `html` next to the `mat.theme` include. 2. Add `body.theme-light { color-scheme: light; }` and `body.theme-dark { color-scheme: dark; }`. 3. In the app, toggle the class on `body` (or remove it to go back to the OS setting). Because `color-scheme` inherits, the nearest declaration wins for each element. That cuts both ways: - The `ng add` schematic writes `body { color-scheme: light; }`. If you add `light dark` on `html` but leave that rule, `body` and everything inside it stays light. Edit or delete the generated body rule. - A region can opt out: a map widget that must stay dark can set `color-scheme: dark` on its own container. ## Pitfalls worth naming - **Expecting a dark palette.** The prebuilt palettes have no dark variants; every M3 tonal palette already contains the tones for both modes. - **Swapping themes with two `mat.theme` calls** for light and dark, as was common with M2 themes. With `light-dark()` that doubles the CSS for no gain; reserve it for `theme-type` builds. - **Old browsers.** `light-dark()` is widely available, but an app that must support a browser without it should use an explicit `theme-type`, and then switch modes by including a second, dark-typed theme under a class. - **Mixing a prebuilt file in.** A prebuilt light file declared later in the cascade overrides the `light-dark()` values and pins light mode. - **Custom styles that skip the variables.** A component style that hard-codes `#ffffff` stays white in dark mode. Styles that read the theme's system variables switch with everything else, because the variables themselves carry both values. - **Testing only one mode.** Since one property flips the whole app, set `color-scheme: dark` in the browser's dev tools (or emulate the operating-system preference there) and walk the key screens before every release.

  • How would you keep a light-only fallback for a browser that lacks light-dark() without giving up dark mode elsewhere?
    Compile with `theme-type: light` on `html`, and include a second `mat.theme` call with `theme-type: dark` under a class such as `.theme-dark`, passing only `color`. Each call emits plain values, so no `light-dark()` is needed, at the cost of a second set of colour variables.
  • Does setting color-scheme change anything besides Material's colours?
    Yes. It tells the browser which scheme the element supports, so built-in form controls, scrollbars and the default canvas colour follow it too. That is useful: native inputs next to Material components switch with them.

  • You need a separate dark palette for dark mode
  • Angular Material reads prefers-color-scheme and switches by itself
  • With no color-scheme declared, the OS setting decides the mode
  • Setting color-scheme: dark turns azure-blue.css dark
  • Light and dark need two mat.theme calls in every app

Assumes Angular Material 22; mat.theme's light-dark() output and the theme-type option arrived with the v19 theming API.