Full answer
Two things make entry and exit animations hard.
- When an element goes from
display: noneto shown, it has no earlier style, so a transition has nothing to start from. It just appears. - When it goes back to
display: none, the element disappears at once, so an exit transition never plays.
Newer CSS has features for both.
@starting-styledefines the style an element has on its first frame, before the first style update. Putopacity: 0there, and the transition runs from 0 to the normal value when the element is first shown.transition-behavior: allow-discretelets properties that cannot be calculated in between, such asdisplayandoverlay, take part in transitions. With it,displayflips at the end of the exit transition, not at the start, so the fade-out is visible.overlaydoes the same for the top layer, which keeps a popover or dialog on screen until the transition ends. A shorthandtransition: opacity 0.3s, display 0.3s allow-discrete, overlay 0.3s allow-discretecovers all three.
For a popover, the rules are applied to :popover-open for the open state, the base rule for the closed state, and @starting-style for the first frame.
These are newer features. Check the support table. Without support, the popover still opens and closes, only without animation. That is a good fallback because the content and behaviour stay the same.
Also respect reduced motion: a short fade is usually fine, but remove movement and long durations for people who ask for less motion.
On click, the popover fades from invisible to visible over 0.4 seconds. When it is closed, it fades out first and is then removed, so the exit is visible. If the browser does not support the features, it simply appears and disappears.
The check for this example was done in one browser only, so read the support table before shipping.
<style>
#pop {
opacity: 0;
transition: opacity 0.4s, display 0.4s allow-discrete, overlay 0.4s allow-discrete;
}
#pop:popover-open { opacity: 1; }
@starting-style {
#pop:popover-open { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) { #pop { transition: none; } }
</style>
<button id="open" popovertarget="pop" type="button">Open</button>
<div id="pop" popover>Hello from the popover</div>Checked in Chrome 156 only, by reading the opacity and display values during the open and close transitions. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Entering from display: none has no start style without @starting-style.
- Leaving display: none cuts the exit unless allow-discrete is used.
- overlay keeps top layer elements visible until the end of the transition.
- The fallback is an instant show and hide.
- Add a reduced motion rule.
Common mistakes
- Expecting a transition to play when going from display: none, with no @starting-style.
- Forgetting allow-discrete for display, so the element vanishes before the fade-out plays.
- Not adding a reduced motion rule.
Follow-ups
- Why is overlay in the transition list for a popover or dialog?
- What is the fallback in a browser without these features?
Tips
- Explain the two problems: no starting style on entry, and display: none cuts the exit.
- Name the two tools: @starting-style and transition-behavior: allow-discrete.