Sub topic 7
Theming by overriding tokens in a scope
This chapter assumes you know CSS selectors and the cascade, how to declare and read custom properties with var(), and the split between primitive and semantic tokens. Here you will build themes by redefining semantic tokens inside a scope.
The idea: change the token, not the component
Custom properties inherit. A token defined on :root is available everywhere, but any element can define the same name again. Every descendant of that element then sees the new value.
This gives you theming without touching component rules. Components read semantic tokens such as --color-surface. A theme is a block that redefines those tokens. The browser resolves var() against the nearest definition, so the same .card rule renders differently in each scope.
A scoped theme
Define the defaults on :root. Then override only the tokens that change, using an attribute selector as the scope.
:root {
--color-surface: #f9fafb;
--color-text: #111827;
--color-accent: #2563eb;
}
[data-theme="brand"] {
--color-surface: #fff7ed;
--color-accent: #c2410c;
}
.card {
background: var(--color-surface);
color: var(--color-text);
border-top: 4px solid var(--color-accent);
}<section class="card">Default theme</section>
<div data-theme="brand">
<section class="card">Brand theme</section>
</div>The brand theme does not redefine --color-text. That token still inherits from :root, so the text color stays the same. A theme only needs to list what differs.
Choosing the scope
The scope can be any element. Common choices are:
| Scope | Selector | Typical use |
|---|---|---|
| Whole page | `:root[data-theme="dark"]` | User-selected site theme |
| One region | `.sidebar` or `[data-theme="brand"]` | A promo block or an embedded widget |
| One component | `.alert--danger` | A variant of a single component |
An attribute such as data-theme is easy to switch from JavaScript and easy to inspect in developer tools. A class works the same way. Pick one convention and keep it.
Nested scopes
Scopes can nest. The innermost definition wins for each token, and tokens it does not define come from the nearest ancestor that does.
[data-theme="dark"] {
--color-surface: #0b1020;
--color-text: #e5e7eb;
}
[data-theme="inverted"] {
--color-surface: #111827;
--color-text: #f9fafb;
}<div data-theme="dark">
<p>Dark text colors apply here.</p>
<aside data-theme="inverted">
<p>Inverted colors apply here.</p>
</aside>
</div>Because the override sits on the element, descendants pick it up through inheritance. This also works for third-party components, provided they read tokens and do not hard-code colors.
Common errors
- Overriding a primitive instead of a semantic token. If you redefine
--color-blue-500in a scope, every semantic token built from it changes too, but only if those semantic tokens are resolved in that scope. A semantic token defined on:rootasvar(--color-blue-500)is resolved once at:rootand then inherited as a fixed value. Override the semantic token in the scope, or define the semantic tokens in the scope as well. - Scope specificity.
[data-theme="dark"]and:roothave different specificity. If both sit on the same element, such as<html data-theme="dark">, the attribute selector wins over:rootonly when its specificity is higher.:rootand[attr]both count as one class-level selector, so source order decides. Put theme blocks after the defaults. - Hard-coded values in components. A component with
color: #111827ignores every theme. Search for raw colors when a scope looks partly themed.
Check your understanding
Exercise
A page defines --color-text: #111827 on :root. A <div data-theme="promo"> defines only --color-surface and --color-accent. What color does text inside the div use, and why?
Show answer
It uses #111827. The div does not define --color-text, so the property inherits from the nearest ancestor that defines it, which is :root.
Exercise
You set --color-blue-500 to a new value inside [data-theme="brand"]. The root defines --color-accent: var(--color-blue-500). A button inside the scope still uses the old blue. How do you fix it?
Show answer
--color-accent was resolved on :root, so descendants inherit its computed value. Redefine --color-accent inside [data-theme="brand"], either with a new value or with var(--color-blue-500) so it resolves in the scope.