CSS Entry and Exit Animations with @starting-style

CSS Entry and Exit Animations with @starting-style

Two structural gaps kept mount and unmount animations in JavaScript. For elements hidden rather than removed, @starting-style and allow-discrete close both, retiring transitionend listeners and timing constants.

Elements that appear and disappear needed JavaScript for two structural reasons: an element rendered for the first time had no previous style to transition from, and display: none applied instantly so exit animations never got to run. @starting-style supplies the before-first-render state, and in browsers that support transitioning display (not yet Firefox), transition-behavior: allow-discrete makes display (and, in Chromium-based browsers, overlay) wait for the transition to finish. For elements that stay in the DOM and are hidden rather than removed, together they retire the enter/leave utility, the transitionend bookkeeping, and the timing constants duplicated between your stylesheet and your JavaScript.

Why appearing and disappearing needed JavaScript

A CSS transition needs two values to interpolate between. That is a trivial requirement until you notice that an element being rendered for the first time has only one — its final style. There is no previous computed value, so there is nothing to transition from, and the element simply appears at its destination. Toasts popped into existence. Dropdowns arrived fully formed.

The workaround was to render the element in its "from" state, force the browser to acknowledge that state, and only then add the class that produced the "to" state. Forcing the acknowledgment meant reading a layout property to flush styles, or waiting two animation frames because one was unreliable. That is where the double requestAnimationFrame in your codebase came from, and it was never anything but a way to convince the engine that a first render had happened.

The exit direction had a different structural problem. display is a discrete property: it flips from block to none at once, with no intermediate values. So an element removed by setting display: none — or removed from the DOM entirely — vanished before any fade could play. The workaround was to run the animation first and delete afterward: add the leaving class, listen for transitionend, then unmount. That listener had to handle the case where several properties transitioned and fired several events, and the case where the transition was interrupted and never fired at all, which is why so many implementations quietly fell back to a setTimeout matching a duration written down in two places.

Both gaps are now closed in CSS for elements that stay in the DOM and are hidden with display: none (or, for top-layer elements, leave the top layer), and the two mechanisms are separate: one supplies the missing starting state, the other holds the discrete flip until the animation is done. Removing an element from the DOM still cancels any running transitions on it, so a component that unmounts by removing the element has to keep it in the DOM and hide it with display: none instead, or keep JavaScript for that step.

Entry: @starting-style

@starting-style declares the style an element transitions from on the frame it is first rendered — which covers both insertion into the DOM and a flip out of display: none, the two cases JavaScript handled with different hacks.

css
.toast {
  opacity: 1;
  translate: 0 0;
  transition: opacity 200ms ease, translate 200ms ease;

  @starting-style {
    opacity: 0;
    translate: 0 1rem;
  }
}

The transition lives on the element's normal state, as always. The nested at-rule supplies the origin. Authoring it nested inside the selector keeps the two halves adjacent, which matters because the rule and its starting styles are easy to separate during a refactor and meaningless apart.

One placement detail bites people: @starting-style carries the same specificity as the rule it applies to, so when written as a standalone block it must come after the original rule or the normal styles win and nothing animates (MDN, accessed July 2026). Nesting the block after the element's own declarations keeps that order without a separate block, which is a good reason to prefer it; declarations written after a nested @starting-style still come later in the cascade and override it.

Exit: transition-behavior: allow-discrete

The exit side needs the discrete flip to wait. transition-behavior: allow-discrete lets discretely animated properties transition instead of changing immediately. Ordinary discrete values flip at 50% of the transition, but display and overlay are interpolated specially: the non-none value (for overlay, auto) is used for the entire transition, in either direction, with most easing functions; a step easing such as step-start can move the flip.

css
.dropdown {
  display: none;
  opacity: 0;
  scale: 0.96;

  transition-property: opacity, scale, display;
  transition-duration: 150ms;
  transition-behavior: allow-discrete;
}

.dropdown.is-open {
  display: block;
  opacity: 1;
  scale: 1;

  @starting-style {
    opacity: 0;
    scale: 0.96;
  }
}

Three things have to be true together, and omitting any one of them produces a silent no-op: the visual properties are transitioned, display is listed among the transitioned properties, and transition-behavior is set to allow-discrete.

The timing the engine applies is exactly what the old listener was trying to emulate. Going to a hidden state, display flips at 100% of the duration, so the element stays rendered for the whole animation. Coming from a hidden state it flips at 0%, so the element is visible before the motion starts (MDN, accessed July 2026).

Interruption is where the declarative version quietly pulls ahead. A menu closed and immediately reopened, or a toast dismissed while it is still arriving, used to be the case that broke the listener-based implementation: the event never fired, the cleanup never ran, and the element was left either invisible with no way back or stuck rendered forever. Transitions handle reversal natively — the engine retargets from wherever the property currently is, so the motion turns around from its current position rather than restarting or stranding. You get that behavior without writing anything, and it avoids the interruption failures described above.

Elements in the top layer need one more property in the list. overlay is the discrete property that governs top-layer membership, and without it a dialog or popover leaves the top layer the moment it closes — dropping behind other content mid-fade. Adding overlay to the transitioned properties keeps it up there until the animation finishes. As of November 2026 overlay ships only in Chromium engines, and other engines ignore it, so check how a top-layer exit looks there. That is a mechanical detail about animation; how a native overlay traps focus, dismisses, and reaches the top layer in the first place is a separate subject with its own coverage.

Composing real components

Three compositions show how the pieces fit together in real components.

The toast is the plain case above: enter with a small translate and a fade, leave by reversing it. The dropdown is the same shape with a transform origin and a shorter duration. The dialog is the one that used to be genuinely unpleasant, because its backdrop is a separate pseudo-element that also has to fade, and both halves have to survive the close:

css
dialog {
  opacity: 0;
  scale: 0.97;
  transition-property: opacity, scale, display, overlay;
  transition-duration: 200ms;
  transition-behavior: allow-discrete;
}

dialog[open] {
  opacity: 1;
  scale: 1;

  @starting-style {
    opacity: 0;
    scale: 0.97;
  }
}

dialog::backdrop {
  background: rgb(0 0 0 / 0%);
  transition-property: background, display, overlay;
  transition-duration: 200ms;
  transition-behavior: allow-discrete;
}

dialog[open]::backdrop {
  background: rgb(0 0 0 / 40%);
}

@starting-style {
  dialog[open]::backdrop {
    background: rgb(0 0 0 / 0%);
  }
}

The backdrop's starting style is written as a standalone block after the rule rather than nested. The CSS Nesting specification originally wrapped declarations inside a nested at-rule in an & rule, which cannot represent pseudo-elements; the nested declarations rule that lets them match ::backdrop shipped in Chrome and Edge 130, Firefox 132, and Safari 18.2, so the standalone block also covers earlier browsers that support @starting-style.

Transitions handle two-state moves, which is what entering and leaving usually are. Reach for keyframe animations when the motion has genuine intermediate states — an attention-getting bounce, a staged reveal — and be aware that the two mechanisms have different relationships with the discrete-property machinery, so mixing them in one component is a place to test rather than assume.

The deletion inventory, once a component is converted to stay in the DOM and hide with display: none instead of unmounting: the enter/leave utility or transition-group wrapper, the transitionend listener and its interruption handling, the setTimeout-before-unmount fallback, the double requestAnimationFrame, and — the one that can drift without any error — the duration constant that existed in both the stylesheet and the script and had to be kept in agreement by hand.

Support and the reduce-motion contract

Both mechanisms are recorded in the web-features data (@starting-style, transition-behavior) as Baseline Newly available since August 2024 (accessed November 2026), meaning the at-rule and the property are supported in current versions of all the core browsers, with a support history of a little over two years. Transitioning display itself is a separate web-features entry, display animation, and it is not Baseline: as of November 2026 it is supported in Chrome and Edge 117 and later and in Safari 18 and later, but not in Firefox (desktop or Android), where display: none still applies immediately on exit and the exit animation does not play. Verify against Baseline before committing, since this paragraph ages faster than the technique.

Degradation is unusually kind here. A browser that does not understand these rules ignores them, and the element appears and disappears instantly — which is precisely what it did before anyone added the animation. Treat that as a designed state rather than a defect, confirm the interface still reads correctly without the motion, and no gate is needed at all.

Motion preference is a stylesheet concern rather than a scripting one, and it belongs in the same rules:

css
@media (prefers-reduced-motion: reduce) {
  .toast,
  .dropdown,
  dialog,
  dialog::backdrop {
    transition-duration: 1ms;
  }
}

Collapsing the duration rather than removing the transition keeps the discrete-property behavior intact, so the element is still hidden correctly — it simply arrives and leaves without the movement. How far to go in honoring the preference, and which motions are worth keeping at reduced amplitude, is a motion-UX question with its own literature.

The honest line: this replaces the enter and leave animation layer for elements that stay in the DOM, not an animation library wholesale. Choreographed sequences across several elements with staggered offsets, and gesture-driven motion that must be interruptible and reversible mid-flight — a sheet that follows a drag and settles based on velocity — remain places where JavaScript animation tooling earns its weight. What has changed is that the ordinary show-and-hide case no longer needs any of it.

Project Inquiry