Sub topic 6
Making state changes smooth
This chapter assumes you know basic CSS transitions, can write Tailwind utilities for color and spacing, and have a project where Tailwind is already set up. It also assumes you have used hover: and focus-visible: variants. Here you add motion to those state changes.
Why state changes snap by default
When a variant such as hover:bg-indigo-700 applies, the browser changes the property in a single frame. The color jumps from one value to the next. A CSS transition tells the browser to interpolate between the old and new values over a duration.
In Tailwind, transitions are utilities, not variants. You put them on the element's base state, so they run in both directions: entering the state and leaving it.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors hover:bg-indigo-700">
Save
</button>transition-colors sets which properties animate, along with a default duration and timing function. Hover the button and the background fades. Move the pointer away and it fades back.
The transition utilities
Each utility limits the animation to a set of properties. Choosing a narrow set avoids animating things you did not intend to.
| Utility | Properties animated |
|---|---|
| `transition-colors` | Colors, backgrounds, borders, text decoration, fill, stroke |
| `transition-opacity` | Opacity |
| `transition-shadow` | Box shadow |
| `transition-transform` | Transform (scale, rotate, translate) |
| `transition` | A common set of properties, including colors, opacity, shadow, and transform |
| `transition-all` | Every animatable property |
| `transition-none` | Nothing |
Prefer the narrowest utility that covers your change. transition-all also animates properties you did not mean to animate, such as width changes caused by layout, and it can cost more to render.
Controlling duration, easing, and delay
Three more utilities adjust the timing.
duration-150,duration-300and so on set the length in milliseconds.ease-in,ease-out,ease-in-outandease-linearset the timing function.delay-75,delay-150and so on wait before starting.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 hover:bg-slate-50">
<h3 class="font-semibold text-slate-900 transition-colors duration-200 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>The arrow uses transition-transform because only its transform changes. The heading uses transition-colors because only its color changes. Both sit on the child element that changes, not on the parent that is hovered.
Where to place the transition
Put the transition utility on the element whose style changes. If a parent is hovered and a child moves, the child needs the transition. This is a common source of "my group hover snaps" bugs.
You can also put a different duration on the entering state. A transition declared inside a variant applies only while that state is active, so it controls the way in, while the base state controls the way out.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-300 hover:bg-indigo-700 hover:duration-75">
Fast in, slow out
</button>Respecting reduced motion
Some users set an operating system preference to reduce motion. Tailwind exposes it through the motion-safe: and motion-reduce: variants.
<button class="rounded bg-indigo-600 px-4 py-2 text-white motion-safe:transition-transform motion-safe:hover:scale-105 hover:bg-indigo-700">
Upgrade
</button>Here the scale animation applies only when the user has not asked for reduced motion. The color change still happens, but without the movement.
Common errors
- Putting
transition-colorsonly onhover:so the fade works in but snaps out. - Using
transition-colorswhen the change is atransform, which does nothing. - Placing the transition on the parent instead of the child that changes.
- Using
transition-alleverywhere and animating properties you did not intend to.
Exercise
A card uses group, and its arrow has group-hover:translate-x-1. The arrow jumps instantly instead of sliding. Which utility fixes it, and which element gets it?
Show answer
Add transition-transform (optionally with duration-200) to the arrow element. The arrow's transform is what changes, so the transition belongs on the arrow, not on the parent card. The arrow also needs to be inline-block or block, because transforms do not apply to non-replaced inline elements.