Sub topic 6
Styling popovers and their backdrop
This chapter assumes you can write basic CSS selectors, understand the cascade, and know how to open a popover with popovertarget. It covers what the browser styles for you, how to style the open state, and how to style the backdrop.
What the browser gives you
A popover is an ordinary element with a default style sheet attached. Two parts of that sheet matter.
- While closed, the element gets
display: none, so it takes no space. - While open, it is centered in the viewport. The browser sets
position: fixed,inset: 0,margin: auto, andwidthandheightoffit-content. It also adds a thin border, a little padding, and system colors.
Because the open popover sits in the top layer, z-index has no effect on it. You never need to raise it above other content.
Styling the open state with :popover-open
The :popover-open pseudo-class matches a popover only while it is showing. Use it to apply styles that should exist only when the popover is visible. Put the base look on [popover] itself.
[popover] {
padding: 1rem;
border: 1px solid #888;
border-radius: 8px;
}
[popover]:popover-open {
background: white;
}
[popover]::backdrop {
background: rgb(0 0 0 / 0.2);
}The first rule changes the default padding, border, and corner radius. The second rule applies only to the open popover. The third rule styles the backdrop, which is covered below.
The same applies to grid layouts:
[popover]:popover-open {
display: grid;
gap: 0.5rem;
}The ::backdrop pseudo-element
When a popover opens, the browser can draw a layer directly behind it in the top layer. That layer is the ::backdrop pseudo-element. By default it is transparent, so you see nothing until you give it a style.
Some points to know:
::backdropis not a child element. You cannot select it with[popover] > *, and it needs its own rule.- It does not inherit from the popover. Custom properties set on
[popover]are not visible inside the::backdroprule, so use values you can write directly, or define the properties on:root. - A popover backdrop is purely visual. Unlike a modal dialog, it does not make the rest of the page inert. A click on it still counts as an outside click, so an
autopopover closes.
:root {
--scrim: rgb(0 0 0 / 0.35);
}
[popover]::backdrop {
background: var(--scrim);
backdrop-filter: blur(2px);
}Resetting default styles
If you want the popover to sit next to its invoker instead of the viewport center, remove the centering. The default margin: auto and inset: 0 would otherwise fight your own position values.
.tooltip[popover] {
margin: 0;
inset: auto;
position: fixed;
top: 4rem;
left: 2rem;
}Positioning relative to the invoker is a separate topic. For now, note that resetting margin and inset is the first step.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `display` set on `[popover]` | Popover is always visible | Move it into `:popover-open` |
| Using `z-index` to raise it | No effect | Remove it; the top layer handles stacking |
| Using a custom property from `[popover]` in `::backdrop` | Value is empty | Define it on `:root` |
| Leaving `margin: auto` when positioning manually | Popover stays centered | Set `margin: 0` and `inset: auto` |
Exercise
You write [popover] { display: flex; } and now your popover is visible when the page loads. Why, and how do you fix it?
Show answer
The rule overrides the browser's display: none for closed popovers. Move the declaration into [popover]:popover-open { display: flex; }, so it applies only while the popover is open.
Exercise
You set --scrim on [popover] and use var(--scrim) in [popover]::backdrop, but the backdrop stays transparent. What is wrong?
Show answer
The backdrop does not inherit from the popover, so the custom property is not defined there. Define --scrim on :root, or write the color directly in the backdrop rule.