Sub topic 3
Animating Popovers and Dialogs
Assume you know CSS selectors, the cascade, transitions, display: none, and basic JavaScript. A popover or dialog enters the top layer, a browser-managed layer above ordinary page content. The overlay property keeps that element in the top layer until its exit transition finishes.
Animating popovers and dialogs
Set the closed values on the base rule and the visible values on the open state. Include both display and overlay as discrete transitions.
<button popovertarget="help">Help</button>
<div id="help" popover>Useful information</div>
<dialog id="settings">
<p>Settings</p>
<button onclick="settings.close()">Close</button>
</dialog>
<button onclick="settings.showModal()">Settings</button>
<style>
[popover],
dialog {
opacity: 0;
transform: scale(.95);
transition:
opacity .3s ease,
transform .3s ease,
display .3s allow-discrete,
overlay .3s allow-discrete;
}
[popover]:popover-open,
dialog[open] {
opacity: 1;
transform: none;
}
@starting-style {
[popover]:popover-open,
dialog[open] {
opacity: 0;
transform: scale(.95);
}
}
</style>The starting style supplies the before-change values when the browser first renders the open state. The discrete transitions delay the display and overlay changes until the visual exit completes.
flowchart TD
A[Closed] -->|open| B[Starting style]
B --> C[Visible in top layer]
C -->|close| D[Exit transition]
D --> Acaption: Popover and dialog entry and exit states alt: A flow from closed, through starting and visible states, back through an exit transition to closed
Animating the `::backdrop`
A modal dialog can style its backdrop separately. Give it a transparent starting and closed color, then transition to a translucent color.
dialog::backdrop {
background: rgb(0 0 0 / 0);
transition:
background .3s ease,
display .3s allow-discrete,
overlay .3s allow-discrete;
}
dialog[open]::backdrop {
background: rgb(0 0 0 / .5);
}
@starting-style {
dialog[open]::backdrop {
background: rgb(0 0 0 / 0);
}
}When to use `@keyframes` instead
Use @starting-style when an existing transition describes a simple change between two states. Use @keyframes when the animation needs several stages, a controlled duration independent of state changes, or repeated motion. Transitions are usually easier to reverse when a user opens and closes an element quickly.
Reduced motion and graceful fallback
Respect the user's system preference and provide a fallback for browsers that do not support discrete transitions.
@media (prefers-reduced-motion: reduce) {
[popover],
dialog,
dialog::backdrop {
transition-duration: .01ms;
}
}
@supports not (transition-behavior: allow-discrete) {
[popover],
dialog {
transition: none;
}
}Pitfalls and debugging in DevTools
- Check that the open selector has enough specificity and appears after conflicting rules.
- Confirm that
@starting-styleis in the cascade. Nested rules are convenient, but a later rule can override them. - Verify that
displayandoverlayare listed intransition. - Inspect the element while it is open. DevTools can show its matched rules and pseudo-element backdrop.
- Test rapid open and close actions, keyboard focus, and reduced-motion mode.
Exercise
Why must a popover transition overlay as well as opacity?
Show answer
overlay keeps the popover in the top layer until the opacity and transform exit transitions finish.