Sub topic 12
Animating elements as they appear
This chapter assumes you know Tailwind utility classes, state variants such as hover:, and the basics of CSS transitions. It uses Tailwind v4 features.
The problem with entering elements
A CSS transition needs two values: a start and an end. When an element is first rendered, or switches from display: none to visible, the browser has no earlier style to start from. The element appears in its final state at once, and no transition runs.
Two things cause this:
- A newly inserted element has no "before" style, so there is nothing to animate from.
displayis a discrete property. It flips betweennoneandblockwith no values in between, so transitions ignore it by default.
Tailwind v4 provides two utilities for these cases: the starting: variant and transition-discrete.
The starting variant
The starting: variant maps to the CSS @starting-style rule. It sets the style an element has on its first render. The browser then transitions from that style to the normal one.
Here is the pattern from the topic's example, a popover menu:
<div popover id="menu" class="opacity-0 transition-opacity transition-discrete duration-200 open:opacity-100 starting:open:opacity-0">
Menu content
</div>Each class has a job:
opacity-0is the closed state.open:opacity-100is the open state. Theopen:variant matches an open popover, dialog or<details>element.starting:open:opacity-0is the style the element has on the frame when it opens. Without it, the element would appear at full opacity and skip the fade.transition-opacity duration-200sets what is animated and for how long.transition-discretelets the transition handle discrete properties such asdisplay.
Why transition-discrete matters
A popover is hidden with display: none while closed. When it closes, display would switch to none immediately and cut the fade-out short. transition-discrete sets transition-behavior: allow-discrete. The browser then delays the display flip until the other transitions finish, so the exit fade stays visible.
You need it for popovers, dialogs and any element toggled with hidden or display: none. For an element that only changes opacity and stays in the layout, you can leave it out.
Adding movement
You can combine starting: with transforms. This version slides a panel up as it fades in:
<div popover id="panel" class="translate-y-2 opacity-0 transition transition-discrete duration-300 ease-out open:translate-y-0 open:opacity-100 starting:open:translate-y-2 starting:open:opacity-0">
Panel content
</div>The starting style repeats the closed values. Because the closed and starting styles match, the element enters from where it exits to, which keeps both directions consistent.
Common errors
- No fade-in happens. The
starting:class is missing, or it does not repeat the same property as the end state. Check that you changed the property you are transitioning. - The element disappears instantly on close. Add
transition-discrete. Without it,display: noneapplies at once. - Nothing animates in an older browser.
@starting-styleandtransition-behaviorare newer CSS features. Unsupported browsers show the final state with no animation, which is a safe fallback. Check browser support before relying on the effect. - The
starting:variant is not recognized. It exists only in Tailwind v4. In v3, use JavaScript to add a class after the first paint, or use a keyframe animation instead. - The animation ignores motion preferences. Wrap the motion in
motion-safe:or reset it withmotion-reduce:, as covered in the reduced motion chapter.
When to use a keyframe animation instead
Use starting: with transitions when you want a two-state change tied to an element opening and closing. Use a custom keyframe animation, covered in an earlier chapter, when the entrance needs several steps or must run on page load for an element that is always in the DOM.
Exercise
A dialog fades in correctly when it opens, but it vanishes instantly when it closes, with no fade-out. It has opacity-0 transition-opacity duration-200 open:opacity-100 starting:open:opacity-0. Which utility is missing, and why does it fix the problem?
Show answer
transition-discrete. Closing the dialog sets display: none, which is a discrete property, so without transition-behavior: allow-discrete the browser applies it immediately and hides the element before the opacity fade finishes. With transition-discrete, the browser delays the display change until the transitions complete.
Exercise
Change the popover example so the menu also scales up from 95% as it opens. Which classes do you add for the closed, open and starting states?
Show answer
Add scale-95 for the closed state, open:scale-100 for the open state, and starting:open:scale-95 for the starting style. Also change transition-opacity to transition so that transform changes are animated as well as opacity.