::backdrop doesn’t inherit from anywhere | Kilian Valkhof
source link: https://kilianvalkhof.com/2023/css-html/backdrop-doesnt-inherit-from-anywhere/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
::backdrop doesn’t inherit from anywhere
Kilian Valkhof
Building tools that make developers awesome.
Search term::backdrop doesn’t inherit from anywhere
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:
:is(: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
Hi, I'm Kilian. I make Polypane, the browser for responsive web development and design. If you're reading this site, that's probably interesting to you. Try it out!
Related Posts
I don’t get to work on a lot of new sites nowadays, but I recently got the opportunity to set one up from scratch. For most sites I built when I was still running an agency, I would use some form of CSS Reset, most often Normalize.css, but I figured that this time round I […]
I recently sat down with the folks at Devtools.fm to talk about my experiences as an indie developer building Polypane and Superposition, my experience working with Electron and my thoughts on the framework, and on FixA11y, the browser extension I’m building in my spare time. Listen to it below:
We’re on the verge of a whole lot of new CSS media queries becoming available. But if you’re a developer that wants to get a head start and try them out, you’re out of luck. Browsers are prototyping them one by one before making them available and we as developers have to wait.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK