Sub topic 6
Styling popovers and their backdrop
This chapter assumes you can open and close a popover with popovertarget and know that popovers render in the top layer. Here you will learn what the browser styles for you, what you must override, and how to style the open state and the backdrop. Animation and positioning get their own chapters, so this one only touches them.
What the browser styles for you
An element with the popover attribute gets a set of default rules from the browser's user agent stylesheet. Knowing them explains most surprises when you first style a popover.
| Property | Default value | Effect |
|---|---|---|
| `display` | `none` while closed | The popover takes no space and is not rendered. |
| `position` | `fixed` | The popover is placed relative to the viewport, not its parent. |
| `inset` | `0` | All four offsets are zero. |
| `margin` | `auto` | With `inset: 0` and `fit-content` sizing, this centers the popover. |
| `width`, `height` | `fit-content` | The popover is only as large as its content. |
| `border` | `solid` | A visible border in the current text color. |
| `padding` | `0.25em` | A small inner gap. |
| `overflow` | `auto` | Long content scrolls inside the popover. |
| `color`, `background-color` | `CanvasText`, `Canvas` | These follow the page's color scheme. |
The two lines that matter most are display: none and the centering from inset plus margin. A popover with no custom CSS appears in the middle of the viewport, which is rarely what you want next to a button. You override that placement yourself. The positioning chapter covers it in detail.
Selecting popovers
Use the attribute selector [popover] for rules that apply to every popover, whether open or closed. Use the :popover-open pseudo-class for rules that apply only while a popover is showing. The pseudo-class matches on the element itself, so you write [popover]:popover-open.
[popover] {
padding: 1rem;
border: 1px solid #888;
border-radius: 8px;
max-width: 24rem;
}
[popover]:popover-open {
background: white;
color: #222;
}Keep the base [popover] rule to appearance that makes sense in both states: spacing, borders, radius, size limits. Put anything that describes "currently showing" in the :popover-open rule. This split also makes later animation easier, because the closed rule becomes the "from" state.
The display trap
The most common styling bug is setting display on the base rule. The browser hides closed popovers with display: none from the user agent stylesheet. Your author stylesheet wins the cascade, so this rule makes the popover visible all the time:
/* Bug: the popover is now always visible, even when closed. */
[popover] {
display: flex;
gap: 0.5rem;
}The fix is to put layout display values only in the open state:
[popover]:popover-open {
display: flex;
gap: 0.5rem;
}The closed popover keeps the default display: none, and the open one gets your layout. The same rule applies to display: grid and display: block.
A complete working example
This page runs as shown in a browser that supports popovers. It styles the popover, its backdrop, and the invoker button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Styled popover</title>
<style>
[popover] {
margin: 0;
inset: auto;
top: 4rem;
left: 2rem;
padding: 1rem 1.25rem;
border: 1px solid #8a8a8a;
border-radius: 8px;
box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
max-width: 20rem;
}
[popover]:popover-open {
display: grid;
gap: 0.5rem;
background: white;
color: #222;
}
[popover]::backdrop {
background: rgb(0 0 0 / 0.25);
}
button:has(+ [popover]:popover-open) {
background: #222;
color: white;
}
</style>
</head>
<body>
<button popovertarget="help">Show help</button>
<div id="help" popover>
<strong>Help</strong>
<p>Press Escape or click outside this box to close it.</p>
</div>
</body>
</html>Several things happen in this example:
margin: 0andinset: autocancel the default centering. Withoutmargin: 0, the automatic margins would still affect wheretopandleftplace the box.topandleftput the popover at a fixed spot in the viewport. This is a placeholder until you learn anchored positioning.display: gridappears only in the open rule, which avoids the display trap.- The
button:has(+ [popover]:popover-open)selector styles the invoker while its popover is open. It works here because the popover is the button's next sibling. If your markup differs, adjust the combinator.
Styling the backdrop
::backdrop is a pseudo-element that browsers draw behind any element in the top layer, directly above the page and below the element. Popovers get one just as modal dialogs do. By default it is fully transparent, so you see no difference until you style it.
[popover]::backdrop {
background: rgb(0 0 0 / 0.2);
backdrop-filter: blur(2px);
}Things to know about ::backdrop:
- It is a separate box that covers the viewport. You style its
background,backdrop-filter, andopacity. Layout properties such aswidthandpaddingdo nothing useful. - You must write it as
[popover]::backdrop, or as a selector for the specific element such as#help::backdrop. It cannot be a descendant target, so[popover] ::backdropwith a space does not work. - A rule such as
[popover]:popover-open::backdropalso works, but it is usually unnecessary. The backdrop exists only while the element is in the top layer. - Inheritance for backdrops has been inconsistent. Older implementations did not let the backdrop inherit from the popover, so a custom property set on
[popover]might not resolve inside::backdrop. To stay safe, set shared values on:root, or repeat literal values. - Clicking the backdrop of an
autopopover counts as a click outside, so it light-dismisses the popover. The backdrop does not block the click from reaching the dismiss logic.
Respecting color schemes
The default Canvas and CanvasText colors adapt to light and dark mode when the page declares color-scheme. If you hard-code background: white, as the examples above do for clarity, the popover stays white in dark mode. For a page that supports both modes, prefer system colors or custom properties:
:root {
color-scheme: light dark;
--popover-bg: Canvas;
--popover-fg: CanvasText;
--popover-border: color-mix(in srgb, CanvasText 40%, transparent);
}
[popover] {
border: 1px solid var(--popover-border);
border-radius: 8px;
padding: 1rem;
}
[popover]:popover-open {
background: var(--popover-bg);
color: var(--popover-fg);
}Defining the variables on :root also sidesteps the backdrop inheritance issue described above.
How the styling states fit together
A popover moves between two states, and each state matches a different set of selectors.
Every open popover matches :popover-open. In a nested stack, each open popover matches it, not only the topmost. The next chapters cover how stacking works.
Common pitfalls
- Specificity fights. The user agent rules have low priority, so most author rules win, but
[popover]and:popover-openhave different specificity.[popover]:popover-openoutranks a plain[popover]rule. If an open-state value seems ignored, check that a more specific selector is not overriding it. - Forgetting to reset margin. Setting
topandleftwithout clearingmargin: autoandinsetcan leave the popover somewhere unexpected. - Clipped shadows.
overflow: autois on by default. Abox-shadowis drawn outside the box and is not clipped by the popover's own overflow, but an inner element that needs to overflow the popover will be clipped. Setoverflow: visibleon the popover only if the content cannot grow past the viewport. - Styling by
[open]. Dialogs use theopenattribute, but popovers do not. Writing[popover][open]matches nothing. Use:popover-open. - Targeting the wrong state.
[popover]:not(:popover-open)matches closed popovers, but they aredisplay: none. Rules there only matter for transitions, covered in the animation chapter.
Exercise
A developer writes [popover] { display: flex; gap: 1rem; } and then finds the popover visible when the page loads, and the toggle button seems to do nothing. Why does this happen, and how should the CSS be rewritten?
Show answer
The base [popover] rule overrides the browser's default display: none for closed popovers, so the element is always rendered. Move the layout declarations to the open state: [popover]:popover-open { display: flex; gap: 1rem; }. Closed popovers then keep the default display: none, and open ones get the flex layout.
Exercise
You want a dark, blurred layer behind an open popover with id confirm, and you want to tint the invoker button while the popover is open. Write the CSS, assuming the button is the popover's previous sibling.
Show answer
For the backdrop: #confirm::backdrop { background: rgb(0 0 0 / 0.4); backdrop-filter: blur(3px); }. For the invoker: button:has(+ #confirm:popover-open) { background: #222; color: white; }. The backdrop selector must be attached directly to the element, with no space before ::backdrop.