5.8 · Logic in CSS
Where var() resolves: the inheritance gotcha
A var() inside a custom property is resolved where that property is declared, not where it’s used.
:root { --accent: oklch(65% 0.18 var(--hue)); } looks
like a formula, but it isn’t one. The browser substitutes
var(--hue) on :root, and children inherit
the finished color. Setting --hue on a card later changes
nothing.
The fix is to declare the formula on the elements that change the
input, or on *. This bug happened while building this
site: the room colors were set on <body> and had no
effect.
:root {
--hue: 330;
--accent: oklch(65% 0.18 var(--hue));
}
/* Expected a blue card. Still pink. */
.card { --hue: 260; }
.card {
--hue: 260;
/* Re-declare it where --hue changes */
--accent: oklch(65% 0.18 var(--hue));
}