Sub topic 3
How custom properties cascade and inherit
This chapter assumes you know basic CSS selectors, specificity, and how to declare and use a custom property with var(). It covers where a token's value comes from and where it gets resolved. These two questions explain most surprises when you work with tokens.
Custom properties are ordinary cascaded properties
A custom property takes part in the cascade like color or padding. For one element and one property name, the browser collects every declaration that applies and picks a winner using the usual order:
- Origin and importance (user agent, user, author, and
!important). - Cascade layers (
@layer). - Specificity.
- Source order, where the later declaration wins.
The winning declaration gives the element its computed value for that property. Other elements can then inherit that value.
:root {
--color-accent: #2563eb;
}
.promo {
--color-accent: #dc2626;
}
.button {
background: var(--color-accent);
}Each .button uses whatever --color-accent its own element has. A button inside .promo is red. A button elsewhere is blue. The .button rule never changes. This is the base of scoped theming, which a later chapter covers in depth.
Inheritance is on by default
Standard properties such as color inherit, but margin and border do not. Custom properties always inherit unless you register them otherwise with @property (covered later in this tutorial). An element with no declaration of its own gets the computed value of its parent.
<section class="promo">
<div class="card">
<button class="button">Buy</button>
</div>
</section>The .promo rule sets --color-accent. The .card and .button elements declare nothing, so they inherit the value down the tree. .button reads the red accent through two levels of inheritance.
Inheritance is the reason you declare shared tokens on :root. Every element in the document is a descendant of it, so every element sees the values. It also means a token on a component's root element is visible to everything inside that component, and nowhere else.
var() resolves where it is used, not where it is declared
This is the most important rule in the chapter. A var() reference is resolved against the element the property is applied to. It does not look at where the rule was written, and it does not look at where the referenced token was first defined.
Consider this pair of rules.
:root {
--gap: 1rem;
--card-padding: var(--gap);
}
.compact {
--gap: 0.25rem;
}
.card {
padding: var(--card-padding);
}Place a .card inside .compact. What padding does it get? The answer is 1rem, not 0.25rem. The reason is the order of operations:
- On
:root, the browser resolves--card-paddingto1rem, becausevar(--gap)is evaluated on:root, where--gapis1rem. .compactoverrides--gapon its own element, but--card-paddingis not redeclared there..cardinherits the already-resolved--card-padding: 1remfrom:root.
A custom property that contains var() is substituted at the element where it is declared. The inherited result is a finished value. It is not a formula that gets re-run lower in the tree.
To get the behavior you probably wanted, redeclare the dependent token in the scope that changes its input.
.compact {
--gap: 0.25rem;
--card-padding: var(--gap);
}Another fix is to avoid the intermediate token and write padding: var(--gap) in .card. Then the reference resolves on the card itself, where --gap is 0.25rem.
This matters for the token tiers you will build later. A semantic token such as --color-surface: var(--color-gray-50) is resolved where it is declared. For a theme switch to work, either the semantic tokens must be redeclared in the theme scope, or the theme must change the primitives on an element that also declares the semantic tokens.
Fixing the pattern with a declaring scope
A common approach is to declare the semantic tokens on the same element that receives the override. The next example sets both layers on [data-theme="brand"], so the derived token is recomputed there.
:root {
--color-blue-500: #2563eb;
--color-accent: var(--color-blue-500);
}
[data-theme="brand"] {
--color-blue-500: #7c3aed;
--color-accent: var(--color-blue-500);
}
.button {
background: var(--color-accent);
}Redeclaring --color-accent looks repetitive. It is the price of the resolve-where-declared rule. Overriding the semantic token directly (--color-accent: #7c3aed) is shorter, and it is the pattern the theming chapter uses.
Specificity and source order apply to tokens
Because custom properties cascade, two rules that set the same token compete by specificity.
:root {
--color-text: #111827;
}
.theme-dark {
--color-text: #e5e7eb;
}
body {
--color-text: #374151;
}Take a <body class="theme-dark">. Both .theme-dark and body target the same element. The class selector has higher specificity than the type selector, so .theme-dark wins and the value is #e5e7eb, even though the body rule comes later. If both selectors had equal specificity, the later rule would win.
Here is a quick reference for where a declaration lands.
| Situation | Result |
|---|---|
| Token declared on `:root` only | Every element sees it through inheritance. |
| Token redeclared on a descendant | That element and its descendants see the new value. |
| Two rules on one element, different specificity | Higher specificity wins. |
| Two rules on one element, same specificity | The later rule wins. |
| Inline `style="--x: …"` | Beats normal selector rules, because inline styles have the highest specificity. |
| `!important` on a token | Beats normal declarations, regardless of specificity. |
Take care with :root versus html. Both match the same element, but :root is a pseudo-class and has specificity 0,1,0, while html is 0,0,1. A token set on :root will beat the same token set on html, whatever order they appear in.
Layers help keep token scopes predictable
Cascade layers put a rule above specificity in the sorting order. For tokens, this gives you a way to say that theme overrides always beat base values without relying on selector weight.
@layer tokens, themes, components;
@layer tokens {
:root {
--color-accent: #2563eb;
}
}
@layer themes {
[data-theme="brand"] {
--color-accent: #7c3aed;
}
}Layers are ordered by declaration, and a later layer beats an earlier one regardless of selector specificity. A bare :root rule would beat a theme selector in the earlier case if specificity differed. With layers, the order in the @layer line decides. One exception: unlayered styles beat all layered styles for normal declarations, so a stray unlayered :root { --color-accent: … } overrides everything above.
Where the value is stored: inheritance is computed per element
Each element has its own computed value for each custom property. When you open the Computed tab in browser dev tools and filter by --color, you see these per-element values. This is the quickest way to answer the question "which value did this element receive?"
The Styles tab shows the declarations that competed. Overridden ones appear struck through. If a token is not what you expect, check these in order:
- Does the Computed tab show a value at all? If not, the token is not defined for this element's ancestors.
- Is the winning declaration on the element you expected, or on an ancestor?
- If the token references another token, is the referenced token defined on the same element or inherited from above?
Inheritance can be interrupted
Three cases stop a token from flowing down the tree.
The all shorthand. all: initial resets standard properties, but it does not affect custom properties. Custom properties are untouched by all.
Explicit reset keywords. You can use CSS-wide keywords on a custom property itself.
.isolated {
--color-accent: initial;
}
.copy {
--color-accent: inherit;
}For an unregistered property, initial sets the value to the guaranteed-invalid value, which acts as if the token were not set. inherit forces inheritance from the parent. unset behaves like inherit here, because custom properties inherit by default.
Registration. A property registered with inherits: false stops flowing to children. You will meet this in the @property chapter.
A reset is useful for isolating a widget so it does not pick up the page theme. After --color-accent: initial, a var(--color-accent, #2563eb) inside that widget falls back to its second argument. Fallbacks are the next chapter's topic.
Cycles break the chain
If tokens reference each other in a loop, every token in the loop becomes invalid.
:root {
--a: var(--b);
--b: var(--a);
}Neither --a nor --b has a usable value. A property that uses either one is invalid at computed-value time and falls back to its inherited or initial value. A token that references itself (--x: var(--x)) is the same kind of cycle. This often happens by accident when you try to extend a token in a child scope:
:root {
--font-stack: system-ui, sans-serif;
}
.article {
/* Cycle: --font-stack now refers to itself */
--font-stack: Georgia, var(--font-stack);
}The fix is to use a different name for the derived value, for example --font-stack-article: Georgia, var(--font-stack);.
Tracing a value step by step
Use this procedure on any element when a token looks wrong.
- Find all declarations of the token that match the element. Sort them by origin, layer, specificity, and order. The top one is the winner.
- If there is no match on the element, move to the parent and repeat. The first match found going up the tree supplies the inherited value.
- If the winning value contains
var(), resolve each reference by repeating this procedure on the same element where the value was declared. - Use the final value in the property that consumes it, and check that the result is valid for that property.
Step 3 is where most mistakes occur.
Exercise
Given the CSS and HTML below, what is the computed padding of the .card? Explain why, and say how you would change the CSS so the card gets 0.5rem.
:root { --gap: 2rem; --pad: var(--gap); }
.tight { --gap: 0.5rem; }
.card { padding: var(--pad); }
<div class="tight"><div class="card">Hello</div></div>
Show answer
The padding is 2rem. --pad is resolved on :root, where --gap is 2rem. The result 2rem is inherited by .tight and .card, and the --gap override in .tight is never applied to --pad. To get 0.5rem, redeclare --pad: var(--gap) inside .tight, or change .card to use padding: var(--gap) directly.
Exercise
A <div class="a b"> is matched by .a { --c: red; } and .b { --c: blue; }, both in the same stylesheet, with .a written first. Which value wins, and what changes if you place .a and .b in separate @layer blocks with .a's layer declared later?
Show answer
Without layers, both selectors have the same specificity, so the later rule .b wins and the value is blue. With layers, the layer declared later wins regardless of source order inside the stylesheet, so .a wins and the value is red.