Sub topic 6
Making state changes smooth
Without a transition, a state change in CSS is instant. The background of a button jumps from indigo-600 to indigo-700 in a single frame. Tailwind's transition utilities tell the browser to animate between the two values instead. This chapter assumes you can already write hover: and focus-visible: variants from the earlier chapters. It covers which utility to pick, where to put it, and how to respect users who dislike motion.
What a transition does
A CSS transition watches a set of properties on an element. When one of those properties changes, for example because :hover started matching, the browser interpolates from the old value to the new one over a set time. Three settings control this:
- Which properties to animate (
transition-property). - How long it takes (
transition-duration). - How the speed changes over that time (
transition-timing-function).
Tailwind exposes each of these as utility classes. The base transition utility sets a default list of properties, a duration of 150ms, and an ease-in-out style curve. You can then override the duration and easing with separate classes.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition hover:bg-indigo-700">
Save
</button>Hovering this button now fades the background over about 150ms.
Put the transition on the base element
Place transition on the element's normal state, not on the hover: variant. This is the most common mistake in this area.
<!-- Fades in on hover, snaps back on mouse leave -->
<button class="bg-indigo-600 hover:bg-indigo-700 hover:transition-colors">Save</button>
<!-- Fades in and out -->
<button class="bg-indigo-600 transition-colors hover:bg-indigo-700">Save</button>The browser uses the transition settings of the element's destination state. In the first button, the hovered state has a transition and the resting state does not. Moving the pointer in animates. Moving it out lands on a state with no transition, so the color snaps back. In the second button, the transition is present in every state, so both directions animate.
Choosing a transition utility
The plain transition class covers many properties at once. A narrower utility limits the work the browser does and prevents unexpected animations.
| Utility | Animates |
|---|---|
| `transition` | A default set: colors, opacity, shadow, and transform-related properties |
| `transition-colors` | Text, background, border, outline, fill, and stroke colors |
| `transition-opacity` | `opacity` only |
| `transition-shadow` | `box-shadow` only |
| `transition-transform` | Transform properties such as translate, scale, and rotate |
| `transition-all` | Every property that changes |
| `transition-none` | Nothing |
Prefer the narrowest utility that covers your change. A button that only changes color should use transition-colors. transition-all is convenient, but it also animates properties you did not mean to animate. If a later change alters width or padding, that change will animate too, and layout properties are more expensive to animate than colors or opacity.
Exact property lists differ a little between Tailwind versions. Check the documentation for the version you use if you depend on a specific property. For example, whether transition-transform covers the separate translate and scale properties changed between v3 and v4.
Duration, easing, and delay
Add these classes next to a transition utility to tune the motion.
| Class | Effect |
|---|---|
| `duration-75`, `duration-150`, `duration-300`, `duration-500` | Length of the animation in milliseconds |
| `ease-linear`, `ease-in`, `ease-out`, `ease-in-out` | Speed curve |
| `delay-150`, `delay-300` | Wait before the animation starts |
Good defaults for interface feedback:
- Use 100 to 200ms for hover and focus color changes. Longer feels sluggish because the user already knows they hovered.
- Use 200 to 300ms for things that move, such as an arrow sliding or a panel fading in.
- Use
ease-outfor elements appearing or reacting to input, since it starts fast and settles gently.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 transition-colors duration-200 hover:bg-slate-50">
<h3 class="font-semibold text-slate-900 transition-colors group-hover:text-indigo-600">Read the guide</h3>
<span class="inline-block transition-transform duration-200 ease-out group-hover:translate-x-1">→</span>
</a>Each element that changes needs its own transition utility. The card, the heading, and the arrow all change, so each carries one. A transition on the parent does not animate its children. Transitions are not inherited in the way that matters here, because each element animates its own properties.
Transitions with group and peer
A child that reacts to a parent's state still declares its own transition. The group-hover: variant changes the child's styles, and the child's transition-* class makes that change gradual. The arrow in the card above shows this. The same holds for peer-* variants.
<input type="email" class="peer rounded border px-3 py-2 invalid:border-red-500" placeholder="[email protected]" />
<p class="text-sm text-red-600 opacity-0 transition-opacity duration-200 peer-invalid:opacity-100">
Enter a valid email address.
</p>This version differs from the earlier peer example in one respect. It hides the message with opacity-0 instead of invisible. The visibility property is not in Tailwind's transition lists, so toggling invisible and visible switches instantly even when a transition class is present. Use opacity for fades. If the hidden element must also be ignored by the pointer, add pointer-events-none to the hidden state, since an invisible-but-present element at opacity-0 still receives clicks.
Hiding with opacity-0 also leaves the element in the accessibility tree. Screen readers will still read it. For content that should be hidden from assistive technology until it is relevant, such as an error message, make sure that the way you reveal it is also announced properly. Do not rely on opacity alone.
Focus styles and transitions
Transitions work on focus the same way they work on hover. Be careful with the focus indicator itself, though. An outline that fades in over 300ms is visible later than one that appears at once, and a keyboard user tabbing quickly may see nothing on some elements for a moment. If you animate the focus ring, keep it fast.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-150 hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 active:bg-indigo-800">
Save
</button>Here transition-colors includes outline-color, so a color change on the outline animates. The outline's width and offset appear immediately, so the indicator is never delayed.
Respecting reduced motion
Some users set an operating system preference to reduce motion, because movement can cause discomfort or nausea. Tailwind exposes this preference as two variants:
motion-safe:applies a utility only when the user has not asked for reduced motion.motion-reduce:applies a utility only when the user has asked for it.
Color fades are generally acceptable. Movement such as sliding, scaling, and rotating is what to guard.
<a href="#" class="group inline-flex items-center gap-1 text-indigo-600">
Continue
<span class="inline-block motion-safe:transition-transform motion-safe:group-hover:translate-x-1">→</span>
</a>With reduced motion on, the arrow neither has a transition nor moves. The link keeps working and its other styles stay unchanged. An alternative is to keep the movement and remove only the animation with motion-reduce:transition-none. Choose based on whether the movement itself matters.
Common errors
- Transition only on the variant. The animation plays in one direction. Move the
transition-*class to the base element. - Transition on the parent only. The children snap. Add a transition class to every element whose style changes.
transition-allon a component that changes size. Unrelated properties animate. Usetransition-colorsortransition-opacity.- Animating
invisibleorhidden. Visibility and display toggles do not fade. Animateopacity. - Long durations on feedback. A 500ms hover color change feels laggy. Keep it under 200ms.
- Forgetting motion preferences. Guard movement with
motion-safe:ormotion-reduce:.
Exercise
A card has the classes bg-white hover:bg-slate-100 hover:transition-colors duration-300. The background fades to gray when the pointer enters but snaps back to white when it leaves. Why does this happen, and how do you fix it so both directions fade?
Show answer
The transition-colors setting exists only in the hovered state, and the browser uses the destination state's transition settings. When the pointer leaves, the destination is the resting state, which has no transition property, so the change is instant. Move the utility to the base element: bg-white transition-colors duration-300 hover:bg-slate-100. The transition is then present in both states, so entering and leaving both animate.
Exercise
You want a "Delete" button in a table row to appear when the row is hovered, using group on the row. You write invisible group-hover:visible transition-opacity on the button. It appears instantly with no fade. What do you change?
Show answer
visibility is not animated by transition-opacity, so toggling invisible and visible switches at once. Replace them with opacity classes: opacity-0 transition-opacity group-hover:opacity-100. Optionally add pointer-events-none group-hover:pointer-events-auto so the hidden button cannot be clicked, and consider focus-visible:opacity-100 so keyboard users can reach it.