Theming with CSS Variables: OS Dark Mode + Manual Override

intermediate
By FrontendAtlas Editorial · Updated Oct 15, 2026
Create a themeable panel using CSS custom properties. Define light defaults on :root, redefine the same tokens for OS dark mode via @media (prefers-color-scheme: dark), then add a manual :root:where(.theme-dark) override after the media query. :where() adds no specificity, so the two root selectors tie and later source order decides the winner. The panel and button should consume theme tokens only for colors and shadow values.

What you'll practice:

  • CSS custom properties as live theme tokens
  • Semantic token naming for background, text, surface, border, accent, contrast, and shadow
  • prefers-color-scheme for OS-level dark mode
  • Cascade/source order for predictable theme overrides
  • Manual root-level theme overrides with :root:where(.theme-dark)

Requirements

  • Define light theme tokens on :root: --bg, --text, --surface, --accent, --border, --accent-contrast, and --panel-shadow.
  • Define dark values for the same tokens inside @media (prefers-color-scheme: dark).
  • Define :root:where(.theme-dark) after the media query and repeat the dark token values there.
  • Use token references inside .panel and .btn; do not place literal color values in those component declarations.
  • Use --accent-contrast for button text so the accent background has an explicit readable foreground token.

Acceptance criteria

  • No hard-coded component colors or colored shadows remain inside .panel or .btn.
  • Dark OS preference switches the page, panel, border, accent, contrast, and panel shadow through variables.
  • Adding class="theme-dark" to html forces the dark token set even when the OS preference is light.
  • Button text contrast is handled through --accent-contrast rather than a literal color.

Expected behavior

Your CSS theme should:

  • Render a light theme by default from :root tokens.
  • Switch to dark values when the system prefers a dark color scheme.
  • Let :root:where(.theme-dark) win because it has the same specificity as :root and is declared later.
  • Keep component styles stable while only token values change between themes.

Implementation notes

  • Keep the same variable names across :root, @media (prefers-color-scheme: dark), and :root:where(.theme-dark).
  • Put :root:where(.theme-dark) after the media query. :where(.theme-dark) contributes zero specificity, so both root selectors have equal specificity and source order breaks the tie.
  • Treat --panel-shadow as part of the theme token set because shadows include color.
  • Use rgb(... / alpha) or another color syntax inside token definitions, not inside constrained component declarations.

Common mistakes

  • Placing :root:where(.theme-dark) before the media query, which lets the later equal-specificity OS rule win.
  • Changing body colors while .panel or .btn keep hard-coded backgrounds, borders, text colors, or shadows.
  • Forgetting --accent-contrast and making button text unreadable in one theme.
  • Using a higher-specificity selector and then incorrectly attributing the result to source order.

Interview explanation

This pattern matters because real UI systems need components that consume semantic tokens instead of owning theme colors directly. CSS custom properties let a design system swap values at runtime without recompiling styles or rewriting every component. prefers-color-scheme respects the user's OS preference, while a root class gives product UI a manual override. The interview signal is understanding cascade order, token completeness, and contrast instead of only toggling a body background.

Testing checklist

  • Test with a light OS preference and confirm :root values are used.
  • Test with a dark OS preference and confirm @media (prefers-color-scheme: dark) values are used.
  • Add the theme-dark class manually and confirm the later :root:where(.theme-dark) rule wins over the OS preference.
  • Inspect computed styles for .panel and .btn to verify variables resolve to the final colors and shadow.

Solution idea

A high-signal CSS theming setup is tokens on :root, an OS dark override via prefers-color-scheme, then an equal-specificity :root:where(.theme-dark) rule declared later. Because :where() contributes zero specificity, source order decides between the two root selectors. Components consume tokens so theme swaps stay instant and consistent.

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Apple interview account. Apple frontend interview questions