I'd like to use CSS variables to define some compound properties. In this example, I'm defining a --border-width
variable as well as a --border
variable that uses the --border-width
variable. I then override the border width value on certain elements:
:root {
--border-width: 1px;
--border: var(--border-width) solid #000;
}
.demo {
border: var(--border);
margin: 1em;
}
.demo.active {
--border-width: 10px;
}
<div class="demo">
Should have thin border
</div>
<div class="demo active">
Should have thick border
</div>
I know that the new variable is being applied to the element; I can see it in the inspector and if I add an explicit border-width: var(--border-width)
property to .demo
, it applies the expected width value. However, it appears that --border
hasn't been recalculated to reflect the change to --border-width
.
I don't want each of my elements to have to specify all of the longhand properties each time I use something with a shorthand, but it seems like I'm missing something fundamental about CSS variables.
For example, I'd prefer to not have to do this:
:root {
--border-width: 1px;
--border-style: solid;
--border-color: #000;
}
.demo {
border: var(--border-width) var(--border-style) var(--border-color);
/*
Or this
border-width: var(--border-width);
border-style: var(--border-style);
border-color: var(--border-color);
*/
margin: 1em;
}
.demo.active {
--border-width: 10px;
}
<div class="demo">
Should have thin border
</div>
<div class="demo active">
Should have thick border
</div>