Sub topic 3
Scope and inheritance: where a custom property applies
This chapter assumes you know basic CSS syntax, the cascade, and specificity. It also assumes you can declare a custom property and read it with var(). The chapter covers where a custom property is visible, how children get its value, and one inheritance mistake that catches many people.
Custom properties inherit by default
Most CSS properties, such as margin or border, do not inherit. Custom properties always inherit unless you register them otherwise (see the @property chapter). If an element has no declaration for --brand-color, it takes the computed value from its parent.
That is why the usual place to define global values is :root, which matches the <html> element. Every other element is a descendant of <html> and inherits from it.
:root {
--brand-color: #0b5fff;
--space: 8px;
}
.button {
background: var(--brand-color);
padding: calc(var(--space) * 2);
}.button never declares --brand-color. It receives the value from :root through inheritance.
Scoping to a component
A custom property is not global. You can declare it on any selector, and the value applies to matching elements and their descendants. Elsewhere in the page, the property keeps whatever value it had before.
.btn {
--btn-bg: var(--brand-color);
background: var(--btn-bg);
color: white;
}
.btn.danger {
--btn-bg: crimson;
}The .btn.danger rule changes --btn-bg only for those elements. The background rule stays the same. Only the input to it changes.
The cascade still applies
Custom properties go through the normal cascade. When two declarations target the same element, the usual rules decide the winner, in this order:
- Importance (
!important). - Specificity.
- Source order.
Inheritance only matters when no declaration targets the element. A declaration on the element itself, however weak its selector, beats an inherited value. This matches ordinary properties and explains most "why is my variable not changing" reports.
Substitution happens where the property is declared
This is the main gotcha. A custom property that contains var() is resolved on the element where it is declared. Its descendants inherit the resolved value, not the original formula.
:root {
--base: 4px;
--gap: calc(var(--base) * 2);
}
.tight {
--base: 2px;
}
.list {
gap: var(--gap);
}<div class="tight">
<ul class="list">
<li>One</li>
<li>Two</li>
</ul>
</div>You might expect the list gap to be 4px (2px times 2). It is 8px. The browser resolved --gap on :root using --base: 4px. The .list element inherits the finished 8px. Changing --base on .tight does not recompute --gap because .tight never declares --gap.
To fix it, declare the dependent property in the scope that changes its input:
.tight {
--base: 2px;
--gap: calc(var(--base) * 2);
}You can also move the derived value to the element that uses it, for example gap: calc(var(--base) * 2) on .list. In that rule, var(--base) reads the nearest value, which is 2px.
Resetting and blocking inheritance
Two keywords give you control over the inheritance chain:
--x: initial;sets the property to the guaranteed-invalid value, so anyvar(--x)with no fallback becomes invalid. It does not restore a previous value.--x: inherit;forces inheritance, which is useful when a rule with higher specificity would otherwise set a value.
Because a custom property inherits by default, you can cut it off inside a subtree by redeclaring it, for example .embed { --brand-color: initial; }. Then use a fallback in var() (see the previous chapter) to supply a safe default.
Common errors
| Symptom | Likely cause |
|---|---|
| Value works on `:root` but not in a component | Property declared on a sibling or child selector, not an ancestor |
| Override on a parent has no effect on a derived value | Derived property resolved at the declaring element, not redeclared |
| Property seems to leak into a nested component | Inheritance is on by default; reset it on the inner component |
| Declaration ignored | A more specific selector declares the same name on the same element |
Exercise
Given :root { --size: 10px; --double: calc(var(--size) * 2); } and .small { --size: 5px; }, what is the computed value of --double on an element inside .small, and how would you make it 10px?
Show answer
It is 20px. --double is resolved on :root with --size: 10px, and the element inherits that result. To get 10px, declare --double: calc(var(--size) * 2); inside .small as well, so it resolves with --size: 5px.