Setting a custom property to a value of inherit
does exactly what you’d expect for every other CSS property: it inherits the same property value of its parent.
normal property inheritance:
<style>
figure {
border: 1px solid red;
}
figure > figcaption {
border: inherit;
}
</style>
<figure>this figure has a red border
<figcaption>this figcaption has the same border
as its parent because it is inherited</figcaption>
</figure>
custom property inheritance (explicit):
<style>
figure {
--foobar: 1px solid green;
}
figure > figcaption {
--foobar: inherit;
border: var(--foobar);
}
</style>
<figure>this figure has no border
<figcaption>this figcaption has a green border
because it explicitly inherits --foobar</figcaption>
</figure>
custom property inheritance (implicit):
all custom properties (unlike border
) are inherited by default
<style>
figure {
--foobar: 1px solid green;
}
figure > figcaption {
border: var(--foobar);
}
</style>
<figure>this figure has no border
<figcaption>this figcaption has a green border
because it implicitly inherits --foobar</figcaption>
</figure>
my question
How do you set a literal value of inherit
to a custom property, when you want its value to actually calculate to the keyword inherit
?
<style>
figure {
border: 1px solid red;
--foobar: 1px solid green;
}
figure > figcaption {
border: var(--foobar);
}
figure > figcaption:hover {
--foobar: inherit;
}
</style>
<figure>this figure has a red border
<figcaption>this figcaption has a green border
because it inherits --foobar</figcaption>
</figure>
<!-- on hover -->
<figure>this figure has a red border
<figcaption>I want this figcaption
to have a red border (inherited from figure)
but its border is green!</figcaption>
</figure>
In this example, I want the second figcaption
(on hover) to inherit its parent’s red border, so I set --foobar
to inherit
. However as shown in example 2, this does not calculate to inherit
, it calculates to the value that is inherited from the parent’s property --foobar
(if it has one), which in this case is green.
I completely understand why the CSS authors designed it this way: --foobar
is just like any other CSS property, so setting inherit
should inherit its value. So I guess I’m asking if there is a workaround for getting the second figcaption
to inherit its parent’s border.
Note, I considered doing
figure > figcaption:hover {
border: inherit;
}
but this defeats the purpose of using a CSS variable.
In the case that there are many other properties in figure > figcaption
that all use the value var(--foobar)
, I don’t want to redefine them all over again for the hover scenario. I'd rather set these properties only once, and then reassign the variable based on context.
--foobar
isn't a property...it's a property value. Only properties can be inherited. – Proudfoot--foobar: inherit;
would compile to1px solid green: inherit
which makes no sense. – Proudfoot1px solid green: inherit
makes no sense. – Proudfoot1px solid green: <anything>;
makes no sense, yet--foobar: <anything>;
is still valid (whether or not the<anything>
isinherit
). I believe you aren't properly making the distinction between--foobar
, a property, andvar(--foobar)
, a value. You are partially correct: in my second (not first) example, the border is green becauseborder: var(--foobar);
calculates toborder: 1px solid green;
. And while you are also correct thatborder
itself is not inherited, the custom property--foobar
is inherited, which gives the border its value. – Cooperman