Popovers, Dialogs and the Top Layer: Styling, ::backdrop and Entry Animations
The top layer that modal dialogs and popovers use, above every stacking context and outside any overflow: styling dialog and popover and their defaults, ::backdrop, :modal, :popover-open and :open, invoker buttons, and entry and exit animations.
kvi5hnuCode Sprout team member8 min readOn this page
Modals, menus, toasts and tooltips have always been fragile: they need to appear above everything, which runs into the stacking context traps of article 58, and they mustn't be clipped by a container's overflow, which absolute positioning can't avoid. HTML now has two elements for this, the <dialog> element and the popover attribute, and both use the browser's top layer. CSS styles them, and can animate them in and out.
The top layer
The top layer is a layer above the whole document. Elements in it:
- are painted above everything else, regardless of any
z-indexor stacking context in the page; - aren't clipped by
overflowon their ancestors, and aren't trapped by ancestors'transformorfilter: their containing block is the viewport; - are stacked in the order they were opened: the last opened is on top.
Tested in Chrome: a modal dialog inside a 50px wrapper with overflow: hidden and a transform still opens centered in the viewport, at full size.

No z-index can beat the top layer, and the wrapper that would normally clip and trap the popover has no effect on it.
Elements enter the top layer when they're shown as a modal dialog (showModal()), a popover, or in fullscreen. No CSS property puts something in the top layer; only these APIs do.
<dialog>
<button commandfor="signup" command="show-modal">Join the newsletter</button>
<dialog id="signup" closedby="any">
<form method="dialog">
<h2>Join the newsletter</h2>
<label>Email <input type="email" required></label>
<button>Subscribe</button>
<button commandfor="signup" command="close">Cancel</button>
</form>
</dialog>
- Modal, opened with
showModal()orcommand="show-modal": in the top layer, with a::backdrop; the rest of the page is inert (article 46); focus moves into the dialog; Escape closes it. - Non-modal, opened with
show()or theopenattribute: not in the top layer; by default it's absolutely positioned at its place in the page, centered horizontally and overlapping what follows, and the page stays usable. <form method="dialog">closes the dialog when submitted.closedbysets how it can be dismissed:any(also clicking outside, called light dismiss),closerequest(Escape or the platform's back gesture; the default for modal dialogs) ornone(only your own buttons or script; the default for non-modal dialogs). Your own close buttons andmethod="dialog"forms work with every value. It's in Chrome 134 and Firefox 141, and Safari 27.3.
The invoker commands, commandfor and command on a <button>, open and close dialogs and popovers without JavaScript: show-modal, close, request-close, show-popover, hide-popover and toggle-popover. They work in Chrome 135, Firefox 144 and Safari 26.2.
The default styles
Tested in Chrome, an open modal dialog gets position: fixed; inset: 0; margin: auto, which centers it, plus max-width and max-height of calc(100% - 38px), padding: 16px, a 3px solid black border, and white background with black text. Reset what you don't want:
dialog {
border: 0;
border-radius: 12px;
padding: 1.5rem;
inline-size: min(32rem, 100% - 2rem);
box-shadow: 0 16px 40px rgb(0 0 0 / 0.25);
color: inherit; /* the UA sets black, which breaks dark themes */
background: Canvas;
}
A closed dialog has display: none. Don't set display on the dialog selector itself, or a closed dialog becomes visible: style dialog[open] instead if you need flex or grid.
Popovers
The popover attribute turns any element into a popover: hidden until shown, then in the top layer.
<button popovertarget="filters">Filters</button>
<div id="filters" popover>…</div>
popover value | Behavior |
|---|---|
auto (the default) | light dismiss: closes on Escape or a click outside; opening one closes other auto popovers, except its ancestors |
manual | stays open until closed by a button or script; several can be open; for toasts |
hint | for tooltips: closes other hints but not auto popovers; Chrome 151, Firefox 153, Safari 27.3 |
Popovers aren't modal: the page stays interactive and focus doesn't move. Their default styles are like a dialog's: position: fixed; inset: 0; margin: auto centers them, with a border, padding, overflow: auto and a Canvas background. To place one next to its button, reset margin and use anchor positioning (article 59): popovers opened by a button anchor to it automatically.
Pseudo-classes and ::backdrop
| Selector | Matches |
|---|---|
:modal | a dialog shown with showModal(), or an element in fullscreen |
:popover-open | a popover that's showing |
:open | an open <dialog>, <details>, or <select> and input pickers; Chrome 133, Firefox 136, Safari 26.5 |
[open] | a dialog or <details> with the open attribute, the older way |
::backdrop | a box covering the viewport, just below each top-layer element |
dialog::backdrop {
background: rgb(43 33 24 / 0.55);
backdrop-filter: blur(3px); /* blur the page behind (article 98) */
}
::backdrop is created for modal dialogs, popovers and fullscreen elements, and inherits from its element, so custom properties set on the dialog work in its backdrop. Popovers have a transparent backdrop by default.

The first and third panels are the browser's defaults; only the middle one has author styles, including backdrop-filter on ::backdrop.
Entry and exit animations
A dialog or popover switches between display: none and visible, which normally can't be animated. Two features make it possible (article 108 covers them in depth):
@starting-stylegives the styles to transition from when the element first appears.transition-behavior: allow-discreteletsdisplayandoverlaytake part in a transition, so the element stays visible, and in the top layer, until the exit transition ends.
body { font: 0.9rem system-ui, sans-serif; margin: 1rem; display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: start; }
button { font: inherit; padding: 0.45rem 0.75rem; border: 1px solid #8a3b12; border-radius: 6px; background: #f4efe6; }
dialog {
border: 0; border-radius: 12px; padding: 1.25rem;
inline-size: min(20rem, 100% - 2rem);
box-shadow: 0 16px 40px rgb(0 0 0 / 0.25);
/* the exit state: also where the closing transition ends */
opacity: 0; translate: 0 1rem;
transition: opacity 0.25s, translate 0.25s, display 0.25s allow-discrete, overlay 0.25s allow-discrete;
}
dialog[open] { opacity: 1; translate: 0 0; }
@starting-style { dialog[open] { opacity: 0; translate: 0 1rem; } } /* where opening starts */
dialog h2 { margin: 0 0 0.5rem; font-size: 1.1rem; color: #8a3b12; }
dialog::backdrop {
background: rgb(43 33 24 / 0);
transition: background 0.25s, display 0.25s allow-discrete, overlay 0.25s allow-discrete;
}
dialog[open]::backdrop { background: rgb(43 33 24 / 0.5); }
@starting-style { dialog[open]::backdrop { background: rgb(43 33 24 / 0); } }
#toast {
margin: 0; inset: auto 1rem 1rem auto; /* bottom right instead of centered */
border: 0; border-radius: 8px; padding: 0.6rem 0.75rem;
background: #2f7d4f; color: white;
display: flex; gap: 0.75rem; align-items: center;
opacity: 0; translate: 0 0.5rem;
transition: opacity 0.2s, translate 0.2s, display 0.2s allow-discrete, overlay 0.2s allow-discrete;
}
#toast:not(:popover-open) { display: none; }
#toast:popover-open { opacity: 1; translate: 0 0; }
@starting-style { #toast:popover-open { opacity: 0; translate: 0 0.5rem; } }
#toast button { background: transparent; color: white; border: 0; padding: 0 0.25rem; font-size: 1.1rem; }
The pattern has three states: the closed styles (also the end of the exit transition), the open styles, and the starting styles in @starting-style. Listing display and overlay in the transition with allow-discrete keeps the element rendered, and in the top layer, while it fades out. overlay is Chrome-only; in other browsers the exit animation may be cut short, but nothing breaks. The toast sets display: flex only while open, since display on a closed popover would show it.
Respect reduced motion (article 112): keep the fade, drop the movement:
@media (prefers-reduced-motion: reduce) { /* after the rules above, so it wins */
dialog, dialog[open], #toast, #toast:popover-open { translate: none; }
@starting-style {
dialog[open], #toast:popover-open { translate: none; }
}
}
The override has to match the open-state selectors and the @starting-style rules too, or their more specific translate values still apply.
Accessibility
- A modal dialog moves focus inside, traps it there, makes the page inert, and returns focus to the button that opened it when it closes. Don't rebuild this with
<div>s. - Give dialogs a heading and label them with
aria-labelledby, and make sure there's a visible close button, not only Escape or light dismiss. - Popovers aren't modal, so don't use them for content that must be dealt with before continuing.
- Use
prefers-reduced-motionfor entry animations, and keep them short: 150 to 300ms.
What's current
<dialog>,showModal(),::backdropand:modal: all browsers.- The
popoverattribute: Baseline since January 2025;popover="hint": Chrome 151, Firefox 153, Safari 27.3. - Invoker commands (
commandfor,command): Chrome 135, Firefox 144, Safari 26.2, Baseline since December 2025. closedbyon dialogs: Chrome 134, Firefox 141, Safari 27.3.:open: Chrome 133, Firefox 136, Safari 26.5.@starting-styleandtransition-behavior: allow-discrete: all browsers; theoverlayproperty: Chrome only.
Common questions
Why does my dialog have a thick black border and white background?
Those are the browser's default styles. Override border, background and color.
Why is my closed dialog visible?
You set display on dialog, which overrides the default display: none of a closed dialog. Use dialog[open] for layout styles.
How do I put a popover next to its button instead of in the center?
Set margin: 0 and use position-area, since popovers opened by a button anchor to it (article 59).
Why does my exit animation not play?
Add display (and overlay) to the transition with allow-discrete, and give the closed state the end values. Without it, the element disappears immediately.
Should I use z-index for modals?
No. showModal() puts the dialog in the top layer, above everything, whatever z-index the page uses.
Summary
- The top layer is above all stacking contexts, isn't clipped by ancestors' overflow, and isn't trapped by their transforms.
<dialog>withshowModal()is modal, with a::backdropand an inert page; popovers (auto,manual,hint) are non-modal and light-dismissable.- Invoker buttons (
commandforandcommand, orpopovertarget) open and close them without JavaScript. - Reset the default border, background, color and centering margin as needed; never set
displayon a closed dialog or popover. - Style states with
:modal,:popover-open,:openand::backdrop. - Animate with
@starting-style, plusdisplayandoverlaytransitions withallow-discrete.
Practice questions
Write the HTML for a button that opens #help as a modal dialog, and a button inside the dialog that closes it, with no JavaScript.
Answer
<button commandfor="help" command="show-modal">Help</button>
<dialog id="help">
<p>…</p>
<button commandfor="help" command="close">Close</button>
</dialog>
Give a modal dialog's backdrop a semi-transparent dark color with a slight blur.
Answer
dialog::backdrop {
background: rgb(0 0 0 / 0.5);
backdrop-filter: blur(2px);
}
A dialog has dialog { display: grid; gap: 1rem; } and is visible on page load, before it's opened. Why, and what's the fix?
Answer
display: grid overrides the browser's display: none for closed dialogs. Put the layout on the open state: dialog[open] { display: grid; gap: 1rem; }.
A popover fades in, but disappears instantly when closed. What's missing?
Answer
display (and overlay) in the transition with allow-discrete, plus the faded-out values on the closed state, so the element stays rendered while it fades:
[popover] { opacity: 0; transition: opacity 0.2s, display 0.2s allow-discrete, overlay 0.2s allow-discrete; }
[popover]:popover-open { opacity: 1; }
@starting-style { [popover]:popover-open { opacity: 0; } }
Review quiz
Test yourself on Part 8 with the CSS Part 8 review quiz.
Previous: Anchor Positioning: Tethering Tooltips and Menus With anchor() and position-area
Next: Flexbox Basics: Flex Containers, Axes, flex-direction and flex-wrap

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.