Animating a dialog in and out with transition-behavior and @starting-style
A dialog that fades in and out sounds like a 0.35s transition on opacity. For years it took JavaScript instead: insert the element, force a reflow, add a class, wait for the exit to finish, then remove it. The blocker is display. A closed dialog is display: none, and display is a discrete property: it has no in-between values, it flips from one keyword to another. Transitions animate continuous values like opacity and transform, so display sat out and the dialog snapped. Two CSS features close the gap. transition-behavior: allow-discrete lets discrete properties take part in a transition, and @starting-style supplies the first frame an entering element animates from.
three states, not two
An element going from display: none to visible has no previous style to transition from, and browsers skip transitions on first style updates for exactly that reason. So an entry and exit animation manages three style states: the starting style (the first frame), the open style, and the default closed style. The starting style applies on entry only; on exit the element transitions back to its default closed style.
The flip timing is the clever part. Discrete properties normally flip halfway through a transition. display is special-cased: going from none to a visible value flips at 0% of the duration, and going back to none flips at 100%. The element is on screen for the full 0.35s in both directions, which is what makes the fade visible. Without allow-discrete, display flips instantly, the element leaves the screen before opacity moves, and the exit animation exists only on paper.
the two pieces
Piece one is allow-discrete, listed per property in the transition shorthand. It is the opt-in that lets a discrete property join the transition at all. Piece two is @starting-style, which declares the values the open dialog transitions from on its first frame. It carries the same specificity as the open rule, so it must come after that rule in the stylesheet; placed before it, the open styles win and there is nothing to animate from.
dialog {
opacity: 0;
transform: translateY(10px) scale(0.97);
transition:
opacity 0.35s ease,
transform 0.35s ease,
overlay 0.35s allow-discrete,
display 0.35s allow-discrete;
}
dialog[open] {
opacity: 1;
transform: translateY(0) scale(1);
}
@starting-style {
dialog[open] {
opacity: 0;
transform: translateY(10px) scale(0.97);
}
}The backdrop is the same shape, one more rule. Its background-color is continuous, so it interpolates normally; display and overlay ride along with allow-discrete so the backdrop stays for the full fade.
dialog::backdrop {
background-color: rgb(0 0 0 / 0);
transition:
background-color 0.35s ease,
overlay 0.35s allow-discrete,
display 0.35s allow-discrete;
}
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 45%);
}
@starting-style {
dialog[open]::backdrop {
background-color: rgb(0 0 0 / 0);
}
}the top layer adds one more property
A modal dialog lives in the top layer, and on close the browser removes it from there. overlay is the property that tracks that removal. It is set by the browser only, you cannot style it, but you can list it in the transition with allow-discrete, which defers the removal until the transition ends. Without it, the dialog leaves the top layer at once: the backdrop vanishes immediately and positioned page content can paint over the panel while it is still fading. With it, panel and backdrop finish their 0.35s together.
Same dialog markup both times. The animated one adds transition-behavior: allow-discrete and an @starting-style block. In Firefox the entry animates and the exit is instant, because display does not transition there yet.
support, honestly
Entry animations work in all three engines: @starting-style and transition-behavior are Baseline 2024 (Chrome 117, Firefox 129, Safari 17.5). Exits across display: none are the gap. Chrome 117 and Safari 18 transition display; Firefox does not yet, so a Firefox exit is instant while the entry still animates. overlay in transitions is Chromium-only today. Every gap degrades to an instant change, not a broken dialog. The progressive-enhancement pattern from MDN is to declare the transition twice, once without allow-discrete and once with it, so older engines keep the opacity and transform transitions and skip only the discrete part:
dialog {
transition: opacity 0.35s ease, transform 0.35s ease;
transition:
opacity 0.35s ease,
transform 0.35s ease,
overlay 0.35s allow-discrete,
display 0.35s allow-discrete;
}The pattern to keep: declare the three states and let the browser own the flip timing. The entry gets a first frame from @starting-style, the exit stays visible because display flips at 100%, and the top layer waits because overlay is in the list. No timers, no reflow tricks, no removal bookkeeping in JavaScript. If your dialog has an exit path this misses (a form submit close, a route change mid-animation), let me know where this flow can improve.