All notes

Animating in from display none with @starting-style

Why a toast with a transition still pops in, and how @starting-style and allow-discrete fix it without requestAnimationFrame.

By 3 min readCSS, Animation

If your toast or popover pops in even though it has a transition, it's probably coming from display: none.

Turn Use @starting-style off in the demo and press the button. The toast appears at full opacity in one frame and vanishes the same way. Turn it back on and it slides in and fades out.

Changes saved
Same toast, same display toggle. Only the CSS differs.

Why the transition doesn't run

A transition goes from one computed value to another. An element with display: none isn't rendered, so it has no computed style to start from. When it switches to display: flex, its first style is already the final one: opacity: 1. There's nothing to animate from.

Going the other way fails too. display isn't a value you can tween, so it flips to none immediately, and the fade-out never gets a chance to be seen.

The old fix

The usual workaround was JavaScript, in two steps:

toast.classList.add("is-mounted") // display: flex, opacity: 0
requestAnimationFrame(() => {
  toast.classList.add("is-open") // opacity: 1, now it can transition
})

and a transitionend listener on the way out, to set display: none only after the fade.

@starting-style

@starting-style lets you write the values an element starts from on its first style update. The browser uses them as the "before" values, then transitions to the real ones:

toast.css
.toast {
  display: none;
  opacity: 0;
  translate: 0 24px;
  transition:
    opacity 350ms ease,
    translate 350ms ease,
    display 350ms allow-discrete;
}
 
.toast.is-open {
  display: flex;
  opacity: 1;
  translate: 0 0;
}
 
@starting-style {
  .toast.is-open {
    opacity: 0;
    translate: 0 24px;
  }
}

The exit: allow-discrete

display 350ms allow-discrete handles the way out. Discrete properties normally flip right away. With allow-discrete, display waits until the end of its duration before it changes to none, so the element stays rendered while opacity fades. On the way in, it switches to flex at the start, so nothing is delayed.

Put it after the normal rule

This one catches people. @starting-style doesn't add specificity. If the block has the same selector as your normal rule and comes before it, the normal rule wins the cascade, the starting values are thrown away, and nothing animates. Keep it after the rule it modifies, as in the example above.

In Tailwind v4 you don't have to think about it: the starting: variant is sorted after the plain utilities in the generated CSS, so the order is right whatever order you write the classes in.

Support

@starting-style and transition-behavior: allow-discrete work in Chrome 117, Firefox 129 and Safari 17.5, so in all three major browsers since August 2024. web.dev's write-up has a version for <dialog>, where you'd also transition overlay so the dialog stays in the top layer while it fades out.

In an older browser the toast still shows and hides; it just pops, the way it did before.

Need this on your site?

I build interfaces like this for agencies, startups and SaaS teams.

Let's talk