CSS Custom Properties & Cascade vs CSS Specificity & Inheritance
Selector specificity arbitrates competing rule declarations, whereas custom properties inherit down the DOM tree.
Structured Comparison Matrix
| Dimension | CSS Custom Properties & Cascade | CSS Specificity & Inheritance |
|---|---|---|
| Mental Model | Custom properties inherit through the DOM tree and resolve var() at computed-value time, enabling dynamic theming without preprocessor compilation. | Cascade resolves Origin, @layer, (A,B,C) specificity, and source order; inherited properties propagate down only when no rule matches. |
| Maturity & Domain | Baseline · Widely · CSS & Layout Chrome 49+ · Firefox 31+ · Safari 9.1+ | Baseline · Widely · CSS & Layout Chrome 88+ · Firefox 78+ · Safari 14+ |
| Prerequisites | Foundational primitive | |
| When Not to Use | When not to use custom properties: Avoid putting high-frequency per-frame animation coordinates on :root; animate transform or scope the variable to the single animated element. | When not to use #id selectors in CSS: Never style with #id selectors in reusable components; reserve #id for DOM anchors and ARIA labelledby targets. |
Engine Mechanics & Trade-Offs
Engine stores --var as raw tokens; var() substitutes at computed-value time and falls back to initial/inherited (IACVT) if invalid.
Key Pitfalls: Mutating :root variables on scroll or mousemove: Updating --x on document.documentElement invalidates 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.
Style engine sorts matched declarations by Origin/Importance -> @layer -> (A,B,C) -> Order, then copies inherited properties from parent ComputedStyle.
Key Pitfalls: :is() specificity surprise: :is(#header, .nav) a gives all matches (even .nav a) the #header specificity of (1, 0, 1). Form control inheritance: <button> and <input> have user-agent font and color rules, blocking <body> inheritance unless inherit is set.