Sub topic 11
Animating entry and exit
This chapter assumes you can already create a popover with the popover attribute and style it with :popover-open. It also assumes you know basic CSS transitions (transition, opacity, transform). By the end you will be able to fade and slide a popover in and out, animate its backdrop, and handle browsers that cannot do it.
Why popovers resist simple transitions
A closed popover is not just invisible. The browser gives it display: none and removes it from the top layer. When it opens, the browser switches display to block and adds the element to the top layer in a single step.
That causes two problems for CSS transitions:
- Entry. A transition needs a "before" value to start from. A newly displayed element has no previous computed style, so it appears at its final style with nothing to animate.
- Exit.
displayflips tononethe moment the popover closes. The element vanishes before an opacity transition can run. The popover also leaves the top layer immediately, so even if it stayed visible it would drop beneath other content.
Modern CSS has three features that fix these problems:
| Feature | Problem it solves |
|---|---|
| `@starting-style` | Supplies the "from" values for the first frame after the element is displayed (entry). |
| `transition-behavior: allow-discrete` on `display` | Lets `display` take part in a transition, so the element stays rendered until the exit finishes. |
| `overlay` with `allow-discrete` | Keeps the element in the top layer until the exit finishes. |
The open and closed states
Think of a popover as having four phases, not two.
In the Starting phase the browser applies the @starting-style values. In the Closing phase the popover is no longer open, but allow-discrete keeps it displayed while the transition runs.
A complete fade-and-slide example
The pattern has three parts. The base rule describes the closed look and declares the transitions. The :popover-open rule describes the open look. The @starting-style block describes where the entry animation starts.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Animated popover</title>
<style>
[popover] {
padding: 1rem;
border: 1px solid #888;
border-radius: 8px;
/* Closed state: this is also the end point of the exit animation. */
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;
}
/* Entry: where the animation starts on the first frame. */
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: translateY(-8px);
}
}
</style>
</head>
<body>
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>
</body>
</html>Here is what happens when you click the button:
- The popover opens and
displaychanges fromnonetoblock. - On the first frame,
@starting-stylemakes the popover transparent and shifted up by 8px. - The browser sees that the computed style differs from the starting style and runs the transitions to
opacity: 1andtransform: none.
When you press Escape or click outside:
- The popover closes, and the base rule's
opacity: 0andtransformbecome the target. display: noneand the removal from the top layer are held back for 0.2s byallow-discrete.- After the transition ends, the popover is hidden and leaves the top layer.
Order and specificity of `@starting-style`
@starting-style adds no specificity. The rule inside it has the same specificity as [popover]:popover-open. If the normal :popover-open rule appears later in your stylesheet, it wins and the starting values are ignored. Put the @starting-style block after the :popover-open rule, as shown above.
You can also nest it inside the rule:
[popover]:popover-open {
opacity: 1;
transform: none;
@starting-style {
opacity: 0;
transform: translateY(-8px);
}
}Nesting keeps the entry values next to the open values, which makes them harder to forget when you edit one of them.
Why each part of the `transition` list is there
You can write the discrete transitions in two ways. The shorthand names the discrete properties in the list:
[popover] {
transition:
opacity 0.2s,
display 0.2s allow-discrete,
overlay 0.2s allow-discrete;
}The longhand sets transition-behavior once for everything:
[popover] {
transition-property: opacity, display, overlay;
transition-duration: 0.2s;
transition-behavior: allow-discrete;
}Keep the display and overlay durations equal to the longest of your visual transitions. If opacity takes 0.3s and display takes 0.1s, the popover disappears while it is still fading.
A discrete property cannot interpolate. For display, the browser flips the value at the start of the transition when going from none to a visible value, and at the end when going to none. That is what keeps the element rendered for the whole exit.
Animating the backdrop
A popover can have a ::backdrop layer, which you met in the styling chapter. The backdrop is a separate pseudo-element, so it needs its own transition and its own starting style.
[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.3);
}
@starting-style {
[popover]:popover-open::backdrop {
background: rgb(0 0 0 / 0);
}
}The backdrop does not inherit transition settings from the popover. If you forget to declare them on ::backdrop, the popover will fade while the backdrop pops in and out.
Entry-only animation with keyframes
If you only care about the entry, @keyframes is a simpler option. An animation runs when the element becomes displayed, so no @starting-style is needed.
[popover]:popover-open {
animation: pop-in 0.2s ease-out;
}
@keyframes pop-in {
from {
opacity: 0;
transform: scale(0.95);
}
}This does not help with exit. The element is hidden as soon as it closes, so there is nothing left to animate. Use transitions with allow-discrete when you need both directions.
Respecting reduced motion
Some users set an operating-system preference to reduce motion. Honor it by shortening or removing the movement. Keep the fade, because opacity changes are generally comfortable.
@media (prefers-reduced-motion: reduce) {
[popover] {
transform: none;
transition:
opacity 0.1s,
overlay 0.1s allow-discrete,
display 0.1s allow-discrete;
}
@starting-style {
[popover]:popover-open {
transform: none;
}
}
}Common errors and how to spot them
- The popover animates out but not in. The
@starting-styleblock is missing, or an earlier rule overrides it. Check the order of your rules. - The popover appears instantly and disappears instantly. The browser may not support
allow-discreteor@starting-style. See the fallback section below. - The popover fades out but jumps behind other content.
overlayis missing from the transition list, so the popover leaves the top layer too early. - The popover vanishes before the fade completes. The
displayduration is shorter than theopacityduration. - The backdrop does not animate.
::backdrophas no transition of its own. - Transforms jump on exit. The base rule has no
transformvalue, so there is no end point to animate toward. Set the closed values on the base rule.
Events during animation
The toggle and beforetoggle events from the previous chapter describe the state change, not the end of the animation. When a popover closes, beforetoggle reports newState as closed right away, even though the popover stays visible for the length of the exit transition. If you need to run code after the exit has finished, listen for transitionend on the popover. Filter on event.propertyName so you react to one property rather than all of them.
const panel = document.getElementById('panel');
panel.addEventListener('transitionend', (event) => {
if (event.target === panel && event.propertyName === 'opacity' && !panel.matches(':popover-open')) {
console.log('exit animation finished');
}
});Fallback behavior
Browsers that do not support @starting-style or allow-discrete ignore those parts of your CSS and show and hide the popover instantly. The popover still works. This makes the animation a progressive enhancement, so you do not need to hide it behind a script check.
There is one case to watch. If a browser supports @starting-style but not the overlay property, entry animates and exit still shows instantly or briefly drops behind other content. Test in the browsers you support, and do not make the animation carry any information the user needs.
Summary
- A popover switches
displayand top layer membership at once, which blocks ordinary transitions. @starting-styleprovides the entry start values.displayandoverlaywithallow-discretekeep the popover rendered and in the top layer during exit.::backdropneeds its own transition and starting style.- Unsupported browsers show and hide the popover without animation, which is an acceptable fallback.
Exercise
A popover fades in correctly but disappears instantly when closed. Its CSS has transition: opacity 0.2s, transform 0.2s; and a correct @starting-style block. What is missing, and why does it fix the exit?
Show answer
The transition list has no display 0.2s allow-discrete or overlay 0.2s allow-discrete. Without them, display switches to none the moment the popover closes, so the element is hidden before the opacity and transform transitions can play. Adding both entries keeps the popover displayed and in the top layer until the 0.2s transition finishes.
Exercise
You add a ::backdrop rule with background: rgb(0 0 0 / 0.3) inside [popover]:popover-open::backdrop, and the popover animates, but the backdrop appears and disappears instantly. Name two changes that make the backdrop animate.
Show answer
First, add a transition (including background, overlay, and display with allow-discrete) to the [popover]::backdrop rule, because the backdrop does not use the popover's transitions. Second, add a @starting-style rule for [popover]:popover-open::backdrop that sets a transparent background so the entry has a starting value.