Most developers prefer to keep all their CSS custom properties in one place, and a pattern that has emerged in recent years is to put those on :root, a pseudo-class that targets the topmost element in your document (so that's always <html> on web pages). But just because they're in one place and in the […]
::backdrop doesn’t inherit from anywhere
December 2023 update! The specification got updated to say that
::backdrop
inherits from “it’s originating element” (thedialog
in this article). The change has been made it all three browser engines, but currently hasn’t shipped yet. Thanks @Schepp for letting me know.
February 2024 update! The specification update is now live in Polypane 18, Chromium 122, Safari 17.4 and Firefox 120!
Earlier this month I was implementing a lightbox for devtoolstips.org using <dialog>
. I'll be writing about that soon but you can find the implementation in that link, it's remarkable how little code you need. While styling, I made use of the CSS custom properties that where already defined in the CSS to style the dialog and it's backdrop. Or so I thought.
For some reason, the color of the backdrop wasn't what I expected, even though devtools picked up the right css var being set. It was just that the CSS var was not define. This puzzled me because it was clearly defined earlier in the CSS under the :root
selector. So why couldn't devtools find it?
Turns out ::backdrop
doesn't inherit from anywhere. It says so clearly in the spec:
"It does not inherit from any element and is not inherited from."
And honestly, that's pretty annoying. You'll either have to duplicate your vars in a specific declaration for ::backdrop
, or add ::backdrop
to your :root
declaration containing all your CSS vars:
:root, ::backdrop {
--my-var-1: #fff;
--my-var-2: #000;
}
If you use :root
only to set CSS vars this isn't that much of an issue, but since it targets the html
element anyway, I usually also add my page styling straight away.
Both the ::before
and ::after
pseudo elements behaved like this in the past though those got updated, and I'm not sure why the same hasn't been done for ::backdrop
, it seems like an oversight.
In any case, Florens came up with the idea of introducing a :globalThis
in CSS, similar to globalThis
in JavaScript, which just always refers to the global environment, be it window
, global
or something else. That would be a great solution for the use case of CSS custom properties that you just want available throughout your CSS. We can dream.
Anyway, now you know to look out for this situation.
Spread the word
Feel free to share this tweet or toot to tell other people.
TIL that ::backdrop does not inherit from anywhere, which means CSS custom properties added to :root won't work for it.
The spec: https://t.co/YHstFAjQc1 pic.twitter.com/w1d8tEwUyi
— Kilian Valkhof (@kilianvalkhof) January 5, 2023