Sub topic 9
Animating popovers and dialogs
This chapter assumes you know CSS transitions, the display property, and the basics of the popover attribute and the <dialog> element. It also assumes you have seen @starting-style and transition-behavior: allow-discrete. Here we combine them to animate elements that live in the top layer.
Why these elements need extra care
Popovers and modal dialogs are promoted to the top layer when they open. The top layer is a browser-managed stack that renders above the rest of the page, regardless of z-index or ancestor overflow.
Two things change when such an element opens or closes:
- Its
displayflips betweennoneand its normal value. - Its top-layer membership is added or removed.
Without extra rules, the browser removes the element from the top layer the moment it closes. The exit transition then plays underneath other content, or not at all. The overlay property fixes this. It reports whether the element is in the top layer, and it can be transitioned with allow-discrete so that removal waits until the exit finishes.
Animating a popover
Each rule below has a job:
- The base
[popover]rule holds the closed (exit) values and the transition. :popover-openholds the open values.@starting-styleholds the values the popover animates from when it opens.
[popover] {
opacity: 0;
transform: scale(0.95);
transition:
opacity 0.3s,
transform 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
transform: none;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: scale(0.95);
}
}Because transition is on the base rule, it applies in both directions. On open, the element starts from the @starting-style values and moves to the open values. On close, it returns to the base values. display and overlay stay in place until the 0.3s elapses, so the popover remains visible and on top while it fades.
A minimal HTML pairing:
<button popovertarget="menu">Open menu</button>
<div id="menu" popover>Menu content</div>Animating a dialog and its backdrop
A modal dialog opened with showModal() also enters the top layer. It has a ::backdrop pseudo-element that is a separate top-layer item, so it needs its own transition and its own starting style.
dialog {
opacity: 0;
transition:
opacity 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open] {
opacity: 1;
}
@starting-style {
dialog[open] {
opacity: 0;
}
}
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0.5);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
}
}<button id="open">Open dialog</button>
<dialog id="dlg">
<p>Hello from a dialog.</p>
<button id="close">Close</button>
</dialog>
<script>
const dlg = document.getElementById('dlg');
document.getElementById('open').addEventListener('click', () => dlg.showModal());
document.getElementById('close').addEventListener('click', () => dlg.close());
</script>The dialog and backdrop use the same duration so they finish together. If you transition only the dialog, the backdrop pops in and out abruptly.
Respect reduced motion
Users who request reduced motion should not get large movement. Shortening the duration to almost nothing keeps the final states and the display flip working.
@media (prefers-reduced-motion: reduce) {
.card,
.panel,
[popover],
dialog,
dialog::backdrop {
transition-duration: 0.01ms;
}
}Common errors
| Symptom | Likely cause |
|---|---|
| Popover fades in but vanishes instantly on close | `display` and `overlay` are missing from `transition`, or lack `allow-discrete` |
| Dialog fades but the backdrop pops | `::backdrop` has no transition or no starting style |
| Nothing animates on open | The `@starting-style` selector is less specific than, or placed before, the rule it should override |
| Exit works but entry jumps | No `@starting-style` for the open state |
Exercise
A popover fades in correctly, but when closed it disappears at once with no fade-out. Its transition lists only opacity 0.3s and transform 0.3s. What do you add, and why?
Show answer
Add overlay 0.3s allow-discrete and display 0.3s allow-discrete to the transition list. Without them, the browser sets display: none and removes the element from the top layer as soon as it closes. With them, both changes wait 0.3s, so the opacity and transform exit transition stays visible.
Exercise
Why does a modal dialog need a separate @starting-style rule for dialog[open]::backdrop?
Show answer
The backdrop is its own top-layer element with its own styles. Styles on dialog do not transition it, so it needs its own starting values and its own transition declaration.