Sub topic 11
Animating entry and exit
This chapter assumes you can write a popover with popovertarget, style it with :popover-open, and understand that open popovers live in the top layer. You should also know basic CSS transitions.
Why popover animation needs extra CSS
A closed popover has display: none. An open one is display: block and sits in the top layer. Ordinary transitions cannot animate either change, for two reasons:
- An element that has just changed from
display: nonehas no earlier style to transition from. - When the popover closes, the browser removes it from rendering and from the top layer immediately. The exit has no time to play.
Three CSS features fix this:
| Feature | What it does |
|---|---|
| `@starting-style` | Supplies the "from" values for the first frame after the popover appears. |
| `transition-behavior: allow-discrete` | Lets properties such as `display` switch at the end of a transition instead of at the start. |
| `overlay` transition | Keeps the element in the top layer until the exit transition finishes. |
A complete fade and slide
[popover] {
opacity: 0;
transform: translateY(-8px);
transition:
opacity 0.2s,
transform 0.2s,
overlay 0.2s allow-discrete,
display 0.2s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
transform: none;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: translateY(-8px);
}
}The rules work in three parts:
- The base rule describes the closed state: transparent and shifted up. It also lists the transitions.
- The
:popover-openrule describes the open state. - The
@starting-styleblock gives the values the popover has on its first frame, so it animates in instead of appearing at once.
On close, the browser removes :popover-open. The element returns to the base values and fades out. Because display and overlay use allow-discrete, the popover stays displayed and in the top layer for the 0.2 seconds the fade takes.
Animating the backdrop
A popover's ::backdrop is a separate box, so it needs its own transition. Use the same pattern:
[popover]::backdrop {
background: rgb(0 0 0 / 0);
transition:
background 0.2s,
overlay 0.2s allow-discrete,
display 0.2s allow-discrete;
}
[popover]:popover-open::backdrop {
background: rgb(0 0 0 / 0.2);
}
@starting-style {
[popover]:popover-open::backdrop {
background: rgb(0 0 0 / 0);
}
}Common errors
- No entry animation. The
@starting-styleblock is missing, or its selector does not match the open state. Its selector must be at least as specific as the open rule. - Entry works, exit snaps shut. The
displayoroverlaytransition is missing. Check that both haveallow-discreteand a duration. - Animation seems to run backwards on close. The closed values in the base rule are the ones the popover fades to. Set them to what you want the exit to look like.
- Nothing animates in an older browser.
@starting-styleandallow-discreteare newer than the popover attribute. The popover still works, and it opens and closes without animation. This is a safe fallback.
Reduced motion
@media (prefers-reduced-motion: reduce) {
[popover],
[popover]::backdrop {
transition-duration: 0.01s;
}
}The short duration keeps the display and overlay transitions valid, so the popover still opens and closes correctly.
Exercise
A popover fades in correctly but disappears instantly when closed. Its CSS lists transition: opacity 0.2s; and has a correct @starting-style block. What should you add, and why?
Show answer
Add overlay 0.2s allow-discrete and display 0.2s allow-discrete to the transition list. Without them, the browser sets display: none and removes the popover from the top layer as soon as it closes, so the opacity fade never gets to play.
Exercise
Why does the open-state rule need a matching @starting-style block, instead of relying on the base rule's opacity: 0?
Show answer
When a popover goes from display: none to shown, it has no previous computed style to transition from. @starting-style provides those first-frame values, so the transition from opacity 0 to 1 can run.