CSS Custom Properties & Cascade
CSS Custom Properties (--token) are live, inheriting cascade values resolved at computed-value time rather than compile time. Because var() bypasses parse-time type checks, a mismatched substitution triggers Invalid At Computed Value Time (IACVT), resetting the property to its initial value instead of an earlier CSS line.
var() Substitution & IACVT Fallback
Trace how custom properties pass parse time, inherit, and trigger IACVT.
Declarations with var() always pass CSS syntax parsing, discarding earlier static fallbacks.
background: blue;
background: var(--missing, 12px); /* Passes parse! */- parseValid
- true
- fallbackDiscarded
- blue
Under the Hood
Sass variables are baked into concrete numbers before the browser ever sees them; CSS custom properties are live pointers evaluated inside Specificity & Inheritance.
- Parse-Time Optimism: Any rule containing
var()is assumed valid during parsing, so earlier fallback lines are thrown away. - Inheritance:
--tokensinherit down the DOM unless overridden by a closer ancestor or@propertywithinherits: false. - IACVT Reset: If
var(--bg)resolves to12pxinsidebackground, it becomes Invalid At Computed Value Time, falling back toinitial(transparent), not the line above.
.alert {
/* Use var() inline fallback, not a separate line above */
background: var(--alert-bg, #fef2f2);
color: var(--alert-fg, #991b1b);
}To add parse-time type safety and animation interpolation, upgrade key tokens with CSS Houdini @property.
Problem Genealogy
- 2009–2015: Sass/Less variables solved static theming but could not respond to media queries,
@container, or runtime dark-mode toggles without duplicating CSS. - 2016+: CSS Custom Properties shipped across browsers, turning design tokens into runtime cascade primitives.
Production Pitfalls
- Mutating
:rootvariables onscrollormousemove: Updating--xondocument.documentElementinvalidates inherited styles across thousands of elements per frame. - Space-toggle tricks: Overly clever
--on: initial; --off: ;hacks hurt readability and break minifiers; prefer explicit classes or container style queries.
- When not to use custom properties: Avoid putting high-frequency per-frame animation coordinates on
:root; animatetransformor scope the variable to the single animated element.
Connections & Logical Next Steps
Zero-runtime engines bind dynamic prop values to inline CSS custom properties instead of generating new classes.
Custom properties participate in the standard cascade and inherit down DOM descendants.
Untyped custom properties defer validation to var() substitution, whereas @property type-checks at parse time.
Selector specificity arbitrates competing rule declarations, whereas custom properties inherit down the DOM tree.
Seen in Architecture Autopsies
Mechanical Comparisons
Untyped custom properties defer validation to var() substitution, whereas @property type-checks at parse time.
Selector specificity arbitrates competing rule declarations, whereas custom properties inherit down the DOM tree.