Sub topic 9
Animating popovers and dialogs
This chapter applies @starting-style, transition-behavior: allow-discrete, display and overlay to the two built-in top-layer elements: popovers and dialogs. It assumes you can already write a transition with transition-property, a duration and a timing function. It also assumes you know that display: none stops an element from transitioning, and that you have seen the popover attribute and the <dialog> element.
Why these elements are harder than a plain panel
A plain show/hide panel has two obstacles. It has no before-change style on first render, and it jumps to display: none the moment you set it. Popovers and dialogs add a third.
When a popover or dialog opens, the browser puts it in the top layer. The top layer is a browser-managed stacking context that sits above everything else on the page, regardless of z-index or overflow on ancestors. When the element closes, the browser removes it from the top layer immediately. If the element is still fading out at that point, it drops back into normal page flow in mid-animation. It may then be clipped by an ancestor or covered by other content.
The overlay property fixes this. It is a read-only-from-the-author's-view property that reports whether the element is currently in the top layer (auto) or not (none). You can't set it. You can only transition it with allow-discrete, so the browser holds the element in the top layer until the exit transition finishes.
You need three pieces for each element:
- A closed-state style (the exit target).
- An open-state style, plus a
@starting-stylecopy of the closed values (the entry origin). - A
transitionthat includesdisplayandoverlaywithallow-discrete.
How the lifecycle plays out
The diagram shows which property flips when. The visual properties (opacity, transform) animate smoothly. display and overlay are discrete, so each one switches at a single moment. With allow-discrete, the switch is placed so the element stays visible for the whole animation.
For a switch from none to a visible value, allow-discrete makes the flip happen at the start of the transition, so the element is visible from frame one. For a switch to none, the flip happens at the end. The same rule applies to overlay, with auto as the "visible" value.
Animating a popover
Here is the popover example from the plan. It uses the three pieces above.
[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);
}
}Here is what each part does.
- The base rule
[popover]describes the closed state and also declares the transition. The browser's own stylesheet already gives a closed popoverdisplay: none, so you don't write that yourself. :popover-openmatches only while the popover is showing. Its values are the entry destination and the exit origin.- The
@starting-styleblock supplies the before-change style on the frame when the popover first appears. Without it,displaygoes fromnonetoblockand the popover would appear at full opacity with no transition. overlayanddisplayare listed withallow-discrete. Both durations match the visual ones.
Put the transition on the base rule
The transition that runs is the one declared on the after-change style, meaning the style the element is moving to. When the popover closes, the after-change style is the base [popover] rule. If you declared transition only inside :popover-open, the entry would animate but the exit would snap shut, because the closed style has no transition list. Declaring it on the base rule covers both directions.
A complete page you can run
This page is self-contained. Click the button to open the popover. Press Escape or click outside it to close it. Both ways of closing animate, because they go through the same state change.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated popover</title>
<style>
[popover] {
padding: 1rem 1.5rem;
border: 1px solid #ccc;
border-radius: 0.5rem;
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);
}
}
@media (prefers-reduced-motion: reduce) {
[popover] {
transition-duration: 0.01ms;
}
}
</style>
</head>
<body>
<button popovertarget="tip">Show tip</button>
<div id="tip" popover>Popovers can fade and scale.</div>
</body>
</html>Because the browser handles opening and closing, you write no JavaScript for the animation. Light dismiss (clicking outside), the Escape key and hidePopover() all trigger the same exit transition.
Animating a modal dialog
A dialog follows the same recipe. The [open] attribute plays the role that :popover-open plays for popovers. The browser's stylesheet sets dialog:not([open]) to display: none.
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;
}
}Here is a runnable page for it. The script only calls showModal() and close(). The animation is entirely CSS.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated dialog</title>
<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;
}
}
</style>
</head>
<body>
<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>
</body>
</html>The Escape key closes a modal dialog, and that exit animates too. Selecting dialog[open] also covers non-modal dialogs opened with show(). If you want to style only modal ones, use dialog:modal.
The backdrop in one paragraph
A modal dialog draws a ::backdrop behind it. It is a separate box, so it needs its own transition and its own @starting-style, otherwise the dimmed background would pop in while the dialog fades. The plan's backdrop example does this with background-color plus display and overlay. A later chapter covers it in detail.
Matching the durations
You can give each property a different duration, but the discrete properties must last at least as long as the longest visual one. Compare these two lines:
/* Risky: display flips at 0.2s, cutting the 0.5s fade short */
transition: opacity 0.5s, display 0.2s allow-discrete;
/* Safe: display waits until the fade is done */
transition: opacity 0.5s, display 0.5s allow-discrete;On exit, display flips to none at the end of its own duration. If that is earlier than the end of the opacity fade, the element vanishes mid-fade. A convenient habit is to keep one duration for all four properties, as the examples do.
Specificity and order of the starting style
@starting-style does not add specificity. A rule inside it has the same specificity as the equivalent rule outside it. The cascade still decides which value wins, and the starting style loses if a later or more specific rule sets the same property.
Two practical consequences follow.
- Put the
@starting-styleblock after the open-state rule, as in the examples. If it comes earlier, the open-state rule can override its values, and the entry starts from the final values. - Match the selector. A starting style for
[popover]won't beat a main rule written as[popover]:popover-open, because the main rule is more specific. Repeat the same selector inside@starting-style.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Entry works, exit snaps shut | `transition` declared only on the open-state rule | Move the `transition` to the base rule |
| Neither direction animates | `display` and `overlay` missing, or `allow-discrete` missing | List both with `allow-discrete` |
| Entry has no fade | `@starting-style` is overridden by a more specific or later rule | Match the selector and place it last |
| Exit is clipped or hidden behind other content | `overlay` is not transitioning | Add `overlay` with `allow-discrete` and the same duration |
| Element vanishes mid-fade | `display` duration shorter than the visual duration | Make the discrete durations at least as long |
| Animation still plays for users who asked for less motion | No `prefers-reduced-motion` rule | Shorten durations in a media query |
Two more details about timing are worth knowing.
- State flips first, animation follows.
:popover-openand[open]change at the moment of the call. Afterdlg.close(),dlg.openis alreadyfalseand focus has already returned to the opener, while the dialog is still visibly fading. If your code needs to wait for the exit to end, listen fortransitionendand checkevent.propertyName. Several properties fire the event, so pick one, such asopacity. overlayhas narrower support than the rest.@starting-styleandallow-discreteare widely available in current browsers.overlayis newer, so check a current compatibility table before relying on it. Where it is missing, the popover or dialog still opens and closes correctly. Only the exit may look less polished, because the element leaves the top layer at once.
Respecting reduced motion
Entry and exit animations are non-essential motion, so give users who request less motion a way out. The plan's rule keeps the same final states and the same display flip, and shrinks only the time:
@media (prefers-reduced-motion: reduce) {
.card,
.panel,
[popover],
dialog,
dialog::backdrop {
transition-duration: 0.01ms;
}
}Use 0.01ms and not 0s. A duration of exactly zero can skip the transition machinery entirely, and a tiny non-zero value keeps display and overlay behaving as in the animated case.
Exercise
A teammate's dialog fades in correctly, but when it closes it vanishes instantly. Their CSS has transition: opacity 0.3s, overlay 0.3s allow-discrete, display 0.3s allow-discrete; inside dialog[open] { ... } and nothing on the plain dialog rule. What is wrong, and how do you fix it?
Show answer
On close, the [open] attribute is removed, so the browser uses the plain dialog rule as the after-change style and finds no transition there. Move the transition declaration to the dialog rule, which also covers the entry. The dialog[open] rule should keep only the destination values.
Exercise
You set transition: opacity 0.6s, display 0.2s allow-discrete; on a popover. What do users see on exit, and what change gives a full fade?
Show answer
Opacity fades for 0.6s, but display flips to none after 0.2s, so the popover disappears while it is still partly visible. Set the display duration to 0.6s. Also add overlay with 0.6s and allow-discrete so the popover stays in the top layer for the whole fade.