Sub topic 2
Choosing what to transition
This chapter assumes you know basic HTML and CSS, you can read Tailwind utility classes, and you have a rough idea of how CSS transition works. By the end you should be able to pick the right transition-* utility for an element and explain why a transition did or did not run.
Why a transition needs a property
A state variant such as hover:bg-indigo-700 changes a style when the state applies. Without a transition, the browser applies that change instantly. A transition tells the browser to animate between the old and new values of specific CSS properties.
In Tailwind, the transition-* utilities set two things: the list of properties to animate (transition-property) and a default duration and easing. If you add a hover style but no transition utility, nothing animates.
The transition utilities
| Utility | Properties animated | Use it for |
|---|---|---|
| `transition` | A default set of color, shadow, opacity, transform and related properties | General-purpose hover effects |
| `transition-colors` | Colors: background, border, text, fill, stroke and similar | Buttons and links that change color |
| `transition-opacity` | `opacity` | Fades |
| `transition-shadow` | `box-shadow` | Elevation changes |
| `transition-transform` | `transform`, `translate`, `scale`, `rotate` | Movement, scaling and rotation |
| `transition-all` | Every animatable property | Quick prototypes |
| `transition-none` | Nothing | Turning a transition off |
The exact default list behind transition differs a little between Tailwind versions, so check the docs for yours. It includes color properties, opacity, box-shadow and transforms.
Narrow transitions: transition-colors
Start with the narrowest utility that covers your change. A button that only changes color needs transition-colors:
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-200 ease-out hover:bg-indigo-700">
Save
</button>The hover variant changes the background color. transition-colors makes that change animate, duration-200 sets it to 200 milliseconds, and ease-out sets how the speed changes. Duration and easing get their own chapters, so treat them as given here.
Broader transitions: transition
When several properties change together, use transition. The card below changes position, size and shadow on hover:
<div class="rounded-lg bg-white p-6 shadow transition duration-300 ease-out hover:-translate-y-1 hover:scale-105 hover:shadow-lg">
Card content
</div>The transition utility animates the default set, which includes box-shadow and transform. So the card lifts, grows and gains a larger shadow, all in one smooth motion.
Matching the utility to the change
The most common error is a mismatch between the transition utility and the property that changes. Consider this element:
<div class="bg-slate-200 transition-opacity duration-200 hover:bg-slate-300">
Hover me
</div>The hover changes the background color, but only opacity is set to transition. The color jumps instantly. The fix is transition-colors.
Check these points when a transition does not run:
- Find which CSS property your variant changes.
- Confirm the transition utility covers that property.
- Confirm the property can be animated. Properties with discrete values, such as
display, do not interpolate by default. - Confirm a duration is set. Tailwind's
transition-*utilities include a default duration, but aduration-0ortransition-noneon the same element overrides it.
transition-all and its costs
transition-all animates every property that changes, so it always "works". That convenience has costs:
- It can animate properties you did not intend, such as
widthorpaddingchanged by a responsive variant. - It can animate layout properties, which are more expensive for the browser than colors or transforms.
- It makes later changes harder to reason about, because any new style on the element may start animating.
Prefer a specific utility when you know what changes. Keep transition-all for prototypes.
How the pieces connect
Exercises
Exercise
A link has the classes text-slate-600 transition-opacity duration-200 hover:text-indigo-600. The color changes instantly on hover. Why, and which utility fixes it?
Show answer
The hover changes color, but transition-opacity only animates opacity. Replace it with transition-colors, which covers text color.
Exercise
A card has transition-colors duration-300 hover:bg-slate-100 hover:shadow-lg. The background fades but the shadow jumps. What is the narrowest change that animates both?
Show answer
Replace transition-colors with transition, whose default property list includes both colors and box-shadow. Adding transition-shadow alone would not work, because only one transition-* property utility applies at a time. transition-all would also work but covers more than needed.