Sub topic 3
Scope and inheritance: where a custom property applies
This chapter assumes you know basic CSS selectors, how the cascade and specificity decide which declaration wins, and how a var() reference reads a custom property. Here you learn where a custom property is visible, how its value reaches descendants, and how to use that behavior on purpose.
Custom properties inherit by default
Ordinary CSS properties split into two groups. Some inherit, such as color and font-family. Others do not, such as margin and border. Custom properties always belong to the first group. Every custom property inherits unless you register it otherwise with @property, which a later chapter covers.
This means a value declared on an element is available to all of its descendants. The browser does not look up an enclosing "scope block" the way a programming language does. Instead, it computes each element's custom properties from its own declarations and from its parent's computed values.
.panel {
--panel-gap: 12px;
}
.panel .item {
margin-bottom: var(--panel-gap);
}
.panel .item .note {
padding-left: var(--panel-gap);
}Both .item and .note read --panel-gap even though neither declares it. The value flows down from .panel through inheritance.
The scope is the element's subtree
The scope of a custom property is the element that declares it plus every descendant. Elements outside that subtree cannot see it.
<div class="panel">
<p class="item">Inside the panel</p>
</div>
<p class="item">Outside the panel</p>With the CSS above, the first .item gets a 12px bottom margin. The second .item is not inside .panel, so --panel-gap is not defined there. In that case var(--panel-gap) is invalid at computed-value time, and margin-bottom falls back to its default behavior. The value-substitution chapter explains that rule in detail.
Why `:root` is the usual place for global values
The :root selector matches the <html> element, which is the ancestor of everything in the document. A custom property declared there is visible to the whole page.
:root {
--brand-color: #0b5fff;
--space: 8px;
}:root has one more advantage over html. Its specificity is that of a pseudo-class (0,1,0), while the html type selector has 0,0,1. A global declaration on :root therefore beats a competing one on html. In practice this rarely matters, but it explains why most codebases write :root.
How the browser computes inherited custom properties
Understanding the mechanism helps you predict behavior in edge cases. For each element, the browser does the following:
- It collects all declarations for custom properties that match the element, and applies the cascade to pick one winner per name.
- For any custom property with no winning declaration, it takes the parent's computed value. If the element has no parent, the property is undefined.
- It substitutes
var()references inside the winning custom property values, using values from step 1 and 2 on the same element. - It uses the result as the element's computed value, which children then inherit.
Step 3 has an important consequence. A custom property that contains var() is resolved on the element that declares it, and the resolved result is what descendants inherit. Descendants do not re-run the substitution with their own values.
:root {
--base: 10px;
--double: calc(var(--base) * 2);
}
.compact {
--base: 4px;
}<div class="compact">
<p class="measured">Measured</p>
</div>On :root, --double resolves to calc(10px * 2). The .compact element overrides --base, but it does not redeclare --double. It inherits the already-resolved --double from :root, which still uses 10px. So .measured sees --base: 4px and --double: calc(10px * 2).
This surprises many people. If you want --double to follow --base inside .compact, declare --double on a selector that matches both places, or redeclare it where --base changes.
:root,
.compact {
--double: calc(var(--base) * 2);
}Now each element resolves --double against its own --base.
Overriding in a narrower scope
Because inheritance flows downward and the cascade picks a winner per element, you can redefine a custom property for part of the page. The new value applies to that element and its subtree, and nothing else.
:root {
--text: #222;
}
.sidebar {
--text: #555;
}
p {
color: var(--text);
}Paragraphs in .sidebar are #555. All other paragraphs are #222. The rule for p is written once. Only the value it reads changes, depending on where the paragraph sits.
This is the main reason custom properties differ from preprocessor variables such as Sass $variables. A Sass variable is replaced at build time and has one value at each point in the source. A custom property is resolved by the browser per element, so the same rule can produce different results in different places.
The cascade still applies
A custom property takes part in the cascade like any other property. Specificity, source order, and !important all work.
:root {
--accent: teal;
}
.card {
--accent: orange;
}
.card.featured {
--accent: purple;
}
.card {
--accent: gray;
}For a .card.featured element, .card.featured has higher specificity than either .card rule, so --accent is purple. For a plain .card, the two .card rules have equal specificity, and the later one wins, so --accent is gray.
Inheritance is the weakest signal. A declaration that matches an element directly always beats a value that would be inherited, whatever its specificity.
#page {
--gap: 24px;
}
.list {
--gap: 8px;
}A .list inside #page gets 8px. The #page rule has more specificity, but it does not match .list. It only provides an inherited value, and a direct match overrides that.
Inline styles
An inline style attribute can set a custom property on a single element. This is how JavaScript and templates often pass per-element values.
<div class="tile" style="--tile-color: tomato">Tile</div>Inline declarations beat selector-based ones in the cascade, so this works without raising specificity elsewhere.
Resetting with `initial`, `inherit`, and `unset`
CSS-wide keywords work on custom properties.
| Keyword | Effect on a custom property |
|---|---|
| `inherit` | Takes the parent's computed value, even if a lower-priority rule would have set one. |
| `initial` | Makes the property undefined, as if it had never been declared. |
| `unset` | Acts as `inherit`, because custom properties inherit by default. |
| `revert` | Rolls back to the previous cascade origin. Author styles fall back to the user agent, which defines none, so the property becomes undefined. |
The initial keyword is useful for cutting off an inherited value so that a var() fallback applies.
:root {
--radius: 12px;
}
.sharp {
--radius: initial;
}
.box {
border-radius: var(--radius, 0);
}Inside .sharp, --radius is undefined, so .box uses the fallback 0.
Note that --radius: initial is different from --radius:; with an empty value. The empty declaration is valid and sets the property to an empty token stream. A var(--radius, 0) then substitutes that empty value instead of the fallback. The fallback is used only when the property is undefined (or invalid), not when it is empty.
.empty {
--radius:;
}
.box {
border-radius: var(--radius, 0);
}Inside .empty, border-radius becomes invalid at computed-value time, which acts like unset. Use initial when you want the fallback.
Scoping on components
Defining a property on a component's root element gives you a local scope. Everything inside the component can read it, and nothing outside is affected. The same property name can then hold different values in different components without collisions.
.btn {
--btn-bg: var(--brand-color);
background: var(--btn-bg);
color: white;
}
.btn.danger {
--btn-bg: crimson;
}A .btn.danger element sets --btn-bg for itself and its children. Other buttons are unaffected.
A name collision is the main risk. Custom properties share one namespace, and there is no module boundary. If two unrelated components both use --size, a value set by an ancestor of one can leak into the other. Prefix names with the component, such as --btn-bg or --card-gap, to avoid it.
Leakage into nested components
Inheritance also applies between components of the same kind. Suppose a card can contain another card.
.card {
--card-pad: 16px;
padding: var(--card-pad);
}
.card.compact {
--card-pad: 8px;
}<div class="card compact">
<div class="card">Inner card</div>
</div>The inner .card declares --card-pad: 16px itself, which matches it directly. So it is 16px, not 8px. The compact setting does not leak in, because the direct declaration wins over the inherited value.
The opposite happens when the inner element only reads a property and does not declare it.
.card.compact {
--card-pad: 8px;
}
.card-body {
padding: var(--card-pad, 16px);
}An inner .card-body inside .card.compact sees 8px, because it inherits the property. This is the behavior you want when children should follow their container. It is a bug when they should not.
> Design rule: Declare a default on the component root when the component should always start from a known state. Read without declaring, and supply a var() fallback, when the component should follow its context.
Debugging scope problems
When a value is not what you expect, check these in order:
- Does the element or an ancestor declare the property? Browser developer tools show the computed value of a custom property in the Computed pane and show which rule it came from in the Styles pane.
- Is a direct declaration on the element overriding the inherited one?
- Was a
var()inside the custom property resolved at the ancestor, as in the--doubleexample? - Is the name spelled exactly the same? Custom property names are case-sensitive, so
--Brandand--brandare different properties.
Summary
- Custom properties inherit by default, so a declaration applies to the element and its subtree.
:rootis the standard place for global values.- References to
var()inside a custom property are resolved on the declaring element, and descendants inherit the resolved value. - A declaration that matches an element directly overrides an inherited value, whatever its specificity.
initialmakes a property undefined so a fallback applies. An empty declaration does not.- Prefix names per component to avoid collisions in the shared namespace.
Exercise
Given :root { --a: 10px; --b: calc(var(--a) * 3); } and .wrap { --a: 2px; }, what value of --b does a child of .wrap see, and how would you change the CSS so the child gets calc(2px * 3)?
Show answer
The child sees calc(10px * 3), because --b was resolved on :root and .wrap only overrides --a. To fix it, declare --b on both selectors, for example :root, .wrap { --b: calc(var(--a) * 3); }, so it resolves against each element's own --a.
Exercise
A page sets :root { --radius: 12px; }. You want elements with class .sharp to use border-radius: 0 through the rule border-radius: var(--radius, 0). Which declaration inside .sharp makes the fallback apply: --radius: initial; or --radius:;?
Show answer
--radius: initial;. It makes the property undefined, so the fallback 0 is used. The empty declaration --radius:; is a valid empty value, so var() substitutes nothing and border-radius becomes invalid at computed-value time instead of using the fallback.