Sub topic 3
How custom properties cascade and inherit
This chapter assumes you know basic CSS selectors, specificity, and how a stylesheet is linked to a page. Tokens only work if you can predict which value an element receives, so this chapter covers that.
Custom properties are ordinary cascading properties
A custom property such as --color-accent takes part in the cascade like color or margin. Several declarations can target the same element. The browser picks one by origin, specificity, and source order.
Custom properties also inherit by default. An element that does not declare --color-accent gets the value from its parent. Most regular properties, such as margin and border, do not inherit. This is why declaring tokens on :root makes them available to the whole page.
:root {
--color-accent: #2563eb;
}
.sidebar {
--color-accent: #7c3aed;
}
.link {
color: var(--color-accent);
}<a class="link">Outside the sidebar</a>
<aside class="sidebar">
<a class="link">Inside the sidebar</a>
</aside>The first link is blue. The second is purple, because it inherits --color-accent from .sidebar, the nearest ancestor that sets it.
Resolution happens per element
The browser resolves var() separately for each element, using that element's own computed value of the property. The rule is not looked up once globally. The lookup works like this:
- Find the winning declaration of the custom property on the element itself, by the normal cascade.
- If the element has none, use the inherited value from its parent.
- Substitute that value wherever
var()refers to it.
This has a consequence for tokens built from other tokens.
:root {
--color-brand: #2563eb;
--color-link: var(--color-brand);
}
.promo {
--color-brand: #dc2626;
}
.promo a {
color: var(--color-link);
}You might expect the link in .promo to be red. It is blue. --color-link is computed on :root, where --color-brand is blue. Children inherit the already-computed blue value of --color-link. They do not re-evaluate var(--color-brand).
To get red, redeclare the dependent token in the scope:
.promo {
--color-brand: #dc2626;
--color-link: var(--color-brand);
}Specificity and source order
Because the cascade applies, a more specific selector wins over a less specific one, and a later rule wins over an earlier one at equal specificity.
:root { --space-card: 1rem; }
.layout .card { --space-card: 2rem; }
.card { --space-card: 0.5rem; }For a .card inside .layout, the value is 2rem. The selector .layout .card has higher specificity than .card, even though it comes earlier. A value set directly on an element with the style attribute beats both. This is how JavaScript overrides work, which a later chapter covers.
The flow of values
Inheritance can be turned off
Registering a property with @property lets you set inherits: false. Unregistered custom properties always inherit. A later chapter covers registration. For now, remember that the default is inheritance, and that the global keywords work on custom properties too:
inherittakes the parent's value explicitly.initialresets the property to the guaranteed-invalid value, as if it were never set.unsetacts likeinheritfor custom properties, because they inherit by default.
Common errors
- Declaring a token on the wrong element. A token set on
.cardis not visible to its siblings or parents. Inheritance only flows down. - Expecting dependent tokens to update. See the warning above.
- Misreading specificity. If a theme override does nothing, check whether a more specific selector sets the same token.
- Case sensitivity.
--Color-Accentand--color-accentare different properties.
Exercise
Given :root { --a: 1rem; --b: var(--a); } and .box { --a: 3rem; }, what padding does <div class="box"><p style="padding: var(--b)"></p></div> give the paragraph, and how would you make it 3rem?
Show answer
The padding is 1rem. --b is computed on :root as 1rem, and the paragraph inherits that computed value through .box. To get 3rem, declare --b: var(--a); inside .box as well, so it is recomputed in that scope.
Exercise
Why do tokens declared on :root work in every component, while a margin declared on :root does not apply to every element?
Show answer
Custom properties inherit by default, so every descendant receives the :root value. The margin property does not inherit, so descendants do not get the root's margin.