Sub topic 12
Animating elements as they appear
This chapter assumes you know how to apply a transition-* utility with a state variant such as hover:, and that you are using Tailwind CSS v4. The starting: variant and transition-discrete do not exist in v3, so the chapter ends with a keyframe fallback that works there too.
Why entering elements do not transition
A CSS transition runs when the browser sees a property change from one computed value to another between two style updates. A hover works because the element already has a style (opacity: 1), and hovering gives it a new one.
An element that is newly inserted into the page, or that goes from display: none to visible, has no earlier computed style. The browser has no "from" value, so it draws the final state immediately. Adding transition-opacity duration-300 to such an element changes nothing on screen.
The same limit applies in reverse. display is a discrete property, meaning it has no in-between values. By default it flips at once, so a fade-out is cut short when the element disappears.
CSS addresses both problems with two features, which Tailwind v4 exposes as utilities:
| CSS feature | Tailwind v4 utility | What it solves |
|---|---|---|
| `@starting-style` | `starting:` variant | Supplies a "from" style for an element's first render |
| `transition-behavior: allow-discrete` | `transition-discrete` | Lets discrete properties such as `display` take part in a transition |
The starting variant
starting: wraps a utility in @starting-style. The style inside applies only on the element's first style update. After that, the normal styles take over and the transition runs from one to the other.
<div class="opacity-100 transition-opacity duration-300 starting:opacity-0">
I fade in when I am first rendered.
</div>Read this as: "normally the element is fully opaque. On its first frame, pretend it was transparent." The browser transitions from 0 to 1 over 300 milliseconds.
Several points follow from how this works:
- It runs on the first render of the page too, not only on elements added later by JavaScript.
- It applies once per element. Toggling a class later does not replay it, unless the element goes back to
display: noneor is removed and re-inserted. - Each element needs its own
starting:classes. Children do not inherit the effect. - You still need a
transition-*utility and a duration.starting:only supplies the "from" value.
You can combine it with transforms to get a slide and fade:
<div class="translate-y-0 opacity-100 transition duration-300 ease-out starting:translate-y-2 starting:opacity-0">
Notification
</div>The plain transition utility covers opacity and transform-related properties, so both parts animate together.
Staggering a list
Because each element has its own starting style, you can offset them with delays:
<ul class="space-y-2">
<li class="opacity-100 transition-opacity duration-300 starting:opacity-0">First</li>
<li class="opacity-100 transition-opacity delay-100 duration-300 starting:opacity-0">Second</li>
<li class="opacity-100 transition-opacity delay-200 duration-300 starting:opacity-0">Third</li>
</ul>During its delay, each item sits at its starting style (transparent), so nothing flashes before its turn.
Popovers, dialogs and the open variant
The open: variant matches an element that is open. In v4 that covers <details open>, <dialog open>, and popovers that match :popover-open. Combined with starting:, you can animate these native elements without JavaScript.
<button popovertarget="menu" class="rounded bg-slate-900 px-4 py-2 text-white">Menu</button>
<div popover id="menu" class="opacity-0 transition-opacity transition-discrete duration-200 open:opacity-100 starting:open:opacity-0">
Menu content
</div>A closed popover is display: none. When it opens, the browser makes it visible and sets opacity: 1 through open:opacity-100. The starting:open:opacity-0 rule gives it a "from" value of 0, so it fades in over 200 milliseconds.
Read the stacked variant from left to right. starting:open:opacity-0 means "in the starting style, when open, use opacity 0."
Animating the exit with transition-discrete
Fading out is harder. When the popover closes, its display goes to none. Two things must hold for the fade to be visible:
displaymust be in the transitioned properties, so the browser holds the element visible until the transition ends.- The transition must be allowed to run on discrete properties, which is what
transition-discretesets.
transition-opacity lists only opacity, so it does not cover display. For the exit, use a utility that includes it. transition-all does, and so does an arbitrary value such as transition-[opacity,display].
Here is a complete page that fades a banner in on load and out when you press a button. It uses a data-closed attribute as the state:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-8">
<div id="banner" class="rounded bg-emerald-100 p-4 opacity-100 transition-all transition-discrete duration-300 starting:opacity-0 data-closed:hidden data-closed:opacity-0">
Settings saved.
</div>
<button id="toggle" class="mt-4 rounded bg-slate-900 px-4 py-2 text-white">Toggle banner</button>
<script>
const banner = document.getElementById("banner");
document.getElementById("toggle").addEventListener("click", () => {
banner.toggleAttribute("data-closed");
});
</script>
</body>
</html>Here is what happens on each step:
- On page load, the banner starts at
opacity: 0fromstarting:opacity-0and fades to 1. - Clicking the button adds
data-closed. That setsopacity: 0anddisplay: none. Becausedisplayis transitioned and discrete transitions are allowed, the browser keeps the banner displayed for 300 milliseconds while opacity falls, then hides it. - Clicking again removes
data-closed. The element becomes visible again, andstarting:opacity-0supplies the "from" value, so it fades back in.
Browser support and fallback behavior
@starting-style and transition-behavior are newer than basic transitions. Current versions of Chrome, Edge, Safari and Firefox support them, but older browsers do not. The failure mode is gentle: the browser ignores the starting style, and the element simply appears without animation. The content stays usable.
That makes these utilities a good fit for progressive enhancement, as long as nothing depends on the animation finishing. Do not use transitionend events as your only signal that an element has become visible.
A keyframe alternative that also works in Tailwind v3
Keyframe animations do not need a "from" style from the browser. They define their own start. That is why they run on a newly inserted element in any browser, and why they are the usual choice in v3.
In v4, define the animation in your CSS:
@import "tailwindcss";
@theme {
--animate-fade-in: fade-in 0.3s ease-out both;
@keyframes fade-in {
from { opacity: 0; transform: translateY(0.5rem); }
to { opacity: 1; transform: translateY(0); }
}
}Then use animate-fade-in on the element:
<div class="motion-safe:animate-fade-in rounded border p-4">Appears with a gentle rise.</div>The both fill mode keeps the element at the first keyframe during any delay, and at the last keyframe afterward. The motion-safe: prefix applies the animation only when the user has not asked for reduced motion, as covered earlier in the tutorial.
The trade-offs between the two approaches:
| Question | `starting:` + transitions | Keyframe animation |
|---|---|---|
| Needs Tailwind v4 | Yes | No |
| Old-browser behavior | Element appears with no animation | Works everywhere |
| Can animate the exit | Yes, with `transition-discrete` | Not on its own, because `display: none` ends the animation |
| Can be interrupted halfway | Yes, it reverses smoothly | No, it restarts or jumps |
| Style lives in | Utility classes on the element | `@theme` plus `@keyframes` |
For a one-time entrance, both work well. For something users toggle repeatedly, transitions handle interruption better: if someone closes a menu mid-fade, the opacity heads back from where it was instead of jumping.
Common mistakes
- Using
starting:without a transition. The element still appears instantly. Addtransition-*and a duration. - Expecting an exit fade with
transition-opacity. It does not includedisplay. Usetransition-allortransition-[opacity,display]withtransition-discrete. - Forgetting the normal value.
starting:opacity-0only works if the element has a different normal opacity. Withoutopacity-100or a default, both values are the same and nothing animates. - Applying
starting:to a parent and expecting children to follow. Every animated element needs its own classes. - Ignoring reduced motion. Add
motion-reduce:transition-noneto entrance transitions, or usemotion-safe:on thestarting:classes.
Exercise
A developer writes <div class="opacity-100 transition-opacity duration-500"> and inserts it into the page with JavaScript, expecting a fade in. Nothing animates. What single class would make it fade in on Tailwind v4, and why does the original not work?
Show answer
Add starting:opacity-0. A newly inserted element has no earlier computed style, so the browser has no "from" value and draws it at full opacity at once. The starting: variant wraps the style in @starting-style, which gives the browser a before-change style of opacity 0 for the first render, and the existing transition-opacity duration-500 then animates from 0 to 1.
Exercise
You use class="transition-opacity transition-discrete duration-300 data-closed:hidden data-closed:opacity-0" on a panel. Adding data-closed makes the panel vanish instantly instead of fading out. What do you change, and why?
Show answer
Replace transition-opacity with transition-all (or transition-[opacity,display]). transition-opacity transitions only opacity, so display: none is applied immediately and hides the element. Once display is among the transitioned properties and transition-discrete allows discrete transitions, the browser keeps the panel displayed until the 300 millisecond transition ends.
Exercise
Name two reasons you might choose a keyframe animate-* utility over starting: for an entrance effect.
Show answer
Keyframes work in browsers that do not support @starting-style, and they work in Tailwind v3, which has no starting: variant. Keyframes also define their own start state, so they need no help from the browser to find a "from" value.