Sub topic 2
Choosing what to transition
This chapter assumes you know basic HTML and CSS, can read Tailwind utility classes, and know what a CSS transition does. By the end you will know which transition-* utility to put on an element, why that choice matters for correctness and speed, and how to handle properties that don't animate well.
What the transition utilities do
A state variant such as hover:bg-indigo-700 changes a property's value instantly. A transition tells the browser to move from the old value to the new one over time. The transition-* utilities set the CSS transition-property list: which properties the browser should animate when their values change.
Each utility also sets a default duration and easing, so one class is enough to see motion. Tailwind's default is 150ms with the easing cubic-bezier(0.4, 0, 0.2, 1). Later chapters cover changing both. This chapter covers the property list.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors hover:bg-indigo-700">
Save
</button>Here transition-colors limits animation to color-related properties. The background color fades, and nothing else does.
The built-in property utilities
| Utility | Properties it animates |
|---|---|
| `transition` | A default set: colors, `opacity`, `box-shadow`, `transform` and `filter` properties (v4 adds a few more) |
| `transition-colors` | `color`, `background-color`, `border-color`, `text-decoration-color`, `fill`, `stroke` (v4 adds `outline-color` and gradient stops) |
| `transition-opacity` | `opacity` |
| `transition-shadow` | `box-shadow` |
| `transition-transform` | `transform` (v4 also covers `translate`, `scale` and `rotate`) |
| `transition-all` | Every animatable property |
| `transition-none` | Nothing |
Pick the narrowest utility that covers the properties your variants change. If a hover changes only the background and text color, use transition-colors. If it changes only the shadow, use transition-shadow.
Put the transition on the base state
A transition applies when the element's computed value changes, and the browser uses the transition settings of the element's new state. If you write the transition only inside a variant, it runs on the way in but not on the way out.
<!-- Fades in on hover, but snaps back instantly when the pointer leaves -->
<button class="bg-indigo-600 hover:bg-indigo-700 hover:transition-colors">Save</button>
<!-- Animates in both directions -->
<button class="bg-indigo-600 transition-colors hover:bg-indigo-700">Save</button>In the first button, the non-hover state has no transition, so the return to bg-indigo-600 is immediate. Put transition-* on the element itself and let variants change only the values.
The variant-only form is occasionally useful on purpose. For example, you may want a fast snap back and a slow fade in. Otherwise, treat it as a bug.
The utility must match the property that changes
A transition list that doesn't include the changing property does nothing for that property. This is the most common mistake when you add a new effect to an existing component.
<!-- The color fades, but the scale jumps because transform is not in the list -->
<button class="bg-indigo-600 transition-colors hover:scale-105 hover:bg-indigo-700">
Save
</button>You have three options:
- Use
transition(the default set), which includes colors and transforms. - Use
transition-all, which includes everything. - List exactly the properties you need with an arbitrary value, as shown below.
<button class="bg-indigo-600 transition-[background-color,transform] hover:scale-105 hover:bg-indigo-700">
Save
</button>The arbitrary form transition-[...] takes a comma-separated list of CSS property names with no spaces. It sets the property list and keeps the default duration and easing.
Default `transition` versus `transition-all`
The default transition covers the properties most interactive elements change. Its property list is a fixed set, so it won't animate a width, a height or a margin.
<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>This card changes transform and box-shadow, and both are in the default set. The card lifts, grows and gains a larger shadow, all animated.
transition-all is tempting because it always works. It has three costs:
- It animates properties you didn't intend, such as a
widthchange caused by a responsive variant or a class swapped by JavaScript. Layout then shifts smoothly when you wanted it to be instant. - It can animate expensive properties. Animating
width,height,marginortopforces the browser to recompute layout on every frame. - It hides intent. A reader can't tell which properties are supposed to move.
Use transition-all for quick prototypes or when you truly change many unrelated properties. In shipped components, prefer a narrower utility.
Turning transitions off
transition-none sets the property list to none, so changes are instant. Its main use is overriding a transition set elsewhere, for example under a user preference or at a breakpoint:
<button class="bg-indigo-600 transition-colors hover:bg-indigo-700 motion-reduce:transition-none">
Save
</button>With motion-reduce:transition-none, users who ask their operating system for reduced motion see an instant color change. A later chapter covers reduced motion in full.
Properties that don't animate well
A transition needs a start value and an end value that the browser can interpolate, which means calculate intermediate steps between. Some properties can't be interpolated, and a transition on them has no visible effect.
height: auto. The browser can't compute steps between a number andauto, soh-0toh-autojumps. Animate between two fixed heights, or use the grid-rows technique below.display. Going fromhiddentoblockis a discrete change, not a range. Tailwind v4 offerstransition-discreteand thestartingvariant for this, covered in a later chapter.background-imagegradients. Changing a gradient swaps one image for another, so there is nothing to interpolate. Overlay two layers and animate the opacity of the top one. Tailwind v4 can transition gradient stops, but v3 can't.
The grid-rows technique animates a panel opening to its natural height without knowing that height. A grid track can transition between 0fr and 1fr.
<div class="group">
<h3 class="font-semibold">Details</h3>
<div class="grid grid-rows-[0fr] transition-[grid-template-rows] duration-300 group-hover:grid-rows-[1fr]">
<div class="overflow-hidden">
<p class="pt-2">This text expands to its natural height.</p>
</div>
</div>
</div>The group class sits on the outer element because a row collapsed to zero height can't be hovered. The inner overflow-hidden wrapper hides the content while the track is small. Browsers that predate animatable grid tracks show the panel without the animation, which is an acceptable fallback.
Several properties, one timing
A single Tailwind transition class applies one duration and one easing to every property in its list. If you want background-color to take 150ms and transform to take 400ms, the utilities can't express that directly. Your options are:
- Accept one shared timing. This is right for most components.
- Split the effect across two elements, each with its own
transition-*andduration-*. - Write a small custom CSS rule with
transition: background-color 150ms, transform 400ms;.
Reach for the second or third option only when the shared timing looks wrong.
Version differences to watch
| Area | Tailwind v3 | Tailwind v4 |
|---|---|---|
| `transition-transform` | Animates `transform` | Animates `transform`, `translate`, `scale` and `rotate` |
| Gradient color stops | Not covered by `transition-colors` | Covered by `transition-colors` |
| `transition-discrete` | Not available | Available |
The scale-*, rotate-* and translate-* utilities set the individual scale, rotate and translate CSS properties in v4, rather than writing into a single transform. If you upgrade and a custom transition list names only transform, those effects will stop animating. Add the individual property names, or switch to transition-transform.
Summary of rules
- Put the
transition-*class on the element's base state, not inside a variant. - Choose the narrowest utility that covers every property your variants change.
- Use
transition-[a,b]when the built-ins don't match exactly. - Avoid animating
height: auto,displayand gradient images directly. - Use
transition-noneto switch transitions off conditionally.
Exercise
A button has the classes bg-indigo-600 text-white transition-colors hover:bg-indigo-700 hover:scale-105. On hover the color fades, but the button grows instantly. Give two different fixes and say which you would prefer.
Show answer
The property list from transition-colors doesn't include the transform properties, so the scale is not animated. Fix 1: replace transition-colors with transition, whose default set includes transform (and scale in v4). Fix 2: use transition-[background-color,transform] to name exactly the two properties. Prefer fix 2 when you want a strict list, because transition also animates shadow, opacity and filters if they are added later. Fix 1 is simpler and fine for a small component. Avoid transition-all, which would also animate layout properties.
Exercise
You write <div class="h-0 overflow-hidden transition-[height] hover:h-auto"> to reveal content on hover, and nothing animates. Name two reasons it fails and one working alternative.
Show answer
First, height can't be interpolated between 0 and auto, so the browser jumps straight to the final value. Second, a zero-height element can't be hovered, so the hover: variant may never fire. A working alternative is the grid-rows technique: put a group class on a wrapper that stays visible, and make the panel a grid with grid-rows-[0fr] transition-[grid-template-rows] group-hover:grid-rows-[1fr], with an inner overflow-hidden child holding the content.