My goal is to have an architecture where the shadow is defined once, but the color changes depending on HTML attribute data-color-mode.
Note: code provided is just for the isolated problem demonstration.
For some reason when nesting variables in CSS, the value doesn't seem to update.
I am having some hypotheses about why this happens, but I don't really know. AI produces faulty answers and says to remove functionality.
:root {
--dark-color: #333;
--light-color: #f0f0f0;
--responsive-color: var(--light-color);
--responsive-shadow: 0 20px var(--responsive-color);
}
[data-color-mode="dark"] {
--responsive-color: var(--dark-color);
}
div {
background: var(--responsive-color);
box-shadow: var(--responsive-shadow);
color: grey;
}
<section>
<div>Expected: Background color and shadow color to be light</div>
</section>
<br>
<br>
<section data-color-mode="dark">
<div>Expected: Background color and shadow color to be dark</div>
</section>
One dirty solution (which I want to avoid) is to duplicate --responsive-shadow: 0 20px var(--responsive-color); to inside [data-color-mode="dark"] {} CSS block. It works. But horrible for many or variable amount of color palettes.
Idea's?
:root,[data-color-mode="dark"] {}instead of:root {}to re-evaluate the result.:root,[data-color-mode] {}worked when added as a separate selector and is the only place that contains vars that use vars! Can we make a proper answer out of this?:rootselector to:root, [themes]with nothing else changed. See jsfiddle.net/vta1gdpn . But glad you found an answer!