Sub topic 7
Theming by Overriding Tokens in a Scope
This chapter assumes you can write custom properties and read them with var(). It also assumes you know how they inherit down the DOM tree, and that you separate primitive tokens from semantic ones. Here you will use those pieces to change a theme without touching a component rule.
The core idea: change the value, not the rule
A theme is a different set of values for the same token names. Components read semantic tokens such as --color-surface and --color-text. If you define those names again on another element, every component inside that element picks up the new values.
Custom properties inherit by default. Declaring a token on an element makes it the value for that element and all its descendants, until a closer ancestor declares the same name. So a theme is a selector plus a block of token overrides.
:root {
--color-surface: #ffffff;
--color-text: #111827;
--color-accent: #2563eb;
}
.theme-ocean {
--color-surface: #0c4a6e;
--color-text: #f0f9ff;
--color-accent: #38bdf8;
}
.card {
background: var(--color-surface);
color: var(--color-text);
border-top: 4px solid var(--color-accent);
padding: 1rem;
}The .card rule never mentions a theme. Place the card inside .theme-ocean and it renders with the ocean values. Place it elsewhere and it uses the :root values.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scoped theme</title>
<link rel="stylesheet" href="tokens.css">
</head>
<body>
<article class="card">Default theme</article>
<section class="theme-ocean">
<article class="card">Ocean theme</article>
</section>
</body>
</html>How the browser resolves a token in a scope
For each element, the browser looks at the custom properties declared on it. If the element declares none for that name, it uses the inherited value from its parent. Inheritance runs up to :root, so the nearest declaration wins.
This is the same cascade and inheritance model from earlier. Theming adds no new mechanism. It uses the existing one on purpose.
Choosing the scope
The scope is any element you can select. Common choices are listed below.
| Scope | Selector | Use it when |
|---|---|---|
| Whole page | `:root` or `[data-theme="x"]` on `<html>` | The user picks a site-wide theme |
| Page region | `.theme-ocean` on a `<section>` | One area, such as a hero or footer, looks different |
| Single component | `.card--featured` | One variant needs its own look |
| Attribute on any element | `[data-theme="dark"]` | JavaScript or server-rendered markup chooses the theme |
A data attribute is usually better than a class for a global theme. It reads as state, and it is easy to set from JavaScript. The next chapters cover prefers-color-scheme and reading and writing tokens from JavaScript. This chapter only needs the attribute selector.
[data-theme="dark"] {
--color-surface: #0b1020;
--color-text: #e5e7eb;
--color-accent: #60a5fa;
}
[data-theme="high-contrast"] {
--color-surface: #000000;
--color-text: #ffffff;
--color-accent: #ffff00;
}Because any element can carry the attribute, the same rules work for the whole page and for one panel.
Override only the semantic tier
Override semantic tokens, not primitives. Primitives are the raw palette. A theme chooses which primitives fill each role.
:root {
/* Primitives: never overridden by themes */
--blue-500: #2563eb;
--blue-300: #93c5fd;
--gray-50: #f9fafb;
--gray-900: #111827;
/* Semantic tokens: the theme surface */
--color-surface: var(--gray-50);
--color-text: var(--gray-900);
--color-accent: var(--blue-500);
}
[data-theme="dark"] {
--color-surface: var(--gray-900);
--color-text: var(--gray-50);
--color-accent: var(--blue-300);
}The dark theme reuses the same primitives with different assignments. It adds no new colors, and the palette stays in one place. A theme block should list only the semantic tokens that change. Anything it does not mention keeps its inherited value.
Nested themes
Scopes can nest. The innermost declaration wins, and tokens a nested scope does not redefine pass through from outside.
<body data-theme="dark">
<main>
<p>Dark page</p>
<aside data-theme="light">
<p>A light panel inside a dark page</p>
</aside>
</main>
</body>:root {
--color-surface: #ffffff;
--color-text: #111827;
}
[data-theme="dark"] {
--color-surface: #0b1020;
--color-text: #e5e7eb;
}
[data-theme="light"] {
--color-surface: #ffffff;
--color-text: #111827;
}
body, aside {
background: var(--color-surface);
color: var(--color-text);
}The [data-theme="light"] block must exist. The :root defaults would not apply inside <aside>, because the aside inherits the dark values from <body>. A nested scope has to restate every token it wants to change back.
Nested themes also expose a trap with body, aside above. Setting background and color on the scope element itself is what makes it look themed. Without that rule, the tokens would change but the aside would still show the page background.
The most common gotcha: tokens resolved in the wrong place
A var() reference is resolved on the element where the property is declared. The child then inherits the resolved result, not the formula. This breaks a scoped override when a token that depends on another is declared too high.
:root {
--color-accent: #2563eb;
--button-bg: var(--color-accent); /* resolved once, at :root */
}
.theme-warm {
--color-accent: #ea580c;
}
.button {
background: var(--button-bg);
}Inside .theme-warm, the button is still blue. At :root, --button-bg was computed as #2563eb. Descendants inherit that finished value. Overriding --color-accent in the scope does not recompute it.
There are two fixes. The first is to declare the dependent token on the element that uses it.
:root {
--color-accent: #2563eb;
}
.theme-warm {
--color-accent: #ea580c;
}
.button {
--button-bg: var(--color-accent); /* resolved on the button, inside the scope */
background: var(--button-bg);
}The second is to override the dependent token in the theme as well. That is more maintenance, since each theme must know every token derived from the one it changes.
This is why the earlier component-token pattern (--card-padding declared inside .card) works with scopes. The component token is evaluated where the component lives, so it sees the scope's values.
Specificity and source order
Custom properties follow the ordinary cascade. A theme override can lose to another declaration of the same token.
:root:not([data-theme]) { --color-text: #111827; } /* specificity (0,2,0)... */
[data-theme="dark"] { --color-text: #e5e7eb; } /* (0,1,0) */If <html> has data-theme="dark", the first selector does not match, because :not([data-theme]) excludes it. That avoids the conflict. A subtler case is an unconditional :root block that comes after the theme block in the stylesheet. :root has class-level specificity (0,1,0), the same as [data-theme="dark"], so the later rule wins and the theme appears to do nothing.
Two habits prevent this:
- Put the default
:rootblock first, then theme blocks after it. - Keep every theme selector at the same specificity, so source order alone decides.
If the order is out of your control, you can wrap the defaults in a lower-priority cascade layer.
@layer tokens, themes;
@layer tokens {
:root {
--color-surface: #ffffff;
--color-text: #111827;
}
}
@layer themes {
[data-theme="dark"] {
--color-surface: #0b1020;
--color-text: #e5e7eb;
}
}Layers declared later win regardless of specificity, so the theme layer beats the defaults even if the files load in a different order.
Partial themes and fallbacks
A theme does not have to be complete. A "compact" theme might change only spacing.
[data-density="compact"] {
--space-3: 0.5rem;
--space-4: 0.75rem;
}Colors, type, and everything else inherit unchanged. This lets you combine independent axes, such as data-theme="dark" with data-density="compact", on the same element. Each axis owns a separate set of token names, so the two never collide.
If you want a component to survive a missing token, give var() a fallback: var(--color-accent, #2563eb). Use that sparingly. A fallback hides the fact that a token is missing from the theme, and a fallback value can drift out of sync with the real token.
Checking what a scope produces
When a scope does not look right, check these in order:
- In dev tools, select the element and look at the Computed pane. Find the token and see which rule it came from.
- Confirm the scope element is an ancestor of the component, not a sibling.
- Check whether the token is derived from another token at a higher level, as in the gotcha above.
- Check that the theme selector is not beaten by a later rule with equal or higher specificity.
Summary of the approach
- Components read semantic tokens only.
- A theme is a selector that redefines those semantic tokens.
- Any element can be a scope, and the nearest declaration wins.
- Nested scopes must restate every token they want to change back.
- Declare derived and component tokens where they are used, so they resolve inside the scope.
Exercise
A page defines :root { --color-accent: #2563eb; --link-color: var(--color-accent); } and a { color: var(--link-color); }. A footer has the class .theme-warm, which sets --color-accent: #ea580c;. Links in the footer are still blue. Explain why, and give one fix.
Show answer
--link-color is resolved at :root, so its computed value is #2563eb. The footer inherits that finished value, and overriding --color-accent inside .theme-warm does not recompute it. One fix is to declare the token on the element that uses it, for example a { --link-color: var(--color-accent); color: var(--link-color); }, so the reference resolves inside the footer scope. Another fix is to also override --link-color: var(--color-accent); in .theme-warm.
Exercise
A dark <body data-theme="dark"> contains an <aside data-theme="light">, but the aside still has a dark background. The stylesheet has [data-theme="dark"] token overrides, no [data-theme="light"] block, and a rule aside { background: var(--color-surface); }. What is missing?
Show answer
Nothing in the stylesheet defines what "light" means. The :root defaults do not apply to the aside, because it inherits the dark values from <body>. Add a [data-theme="light"] block that sets --color-surface and the other semantic tokens back to their light values.