Transitions & Animations — Tailwind CSS

transition-* classes smooth a change from one state to another, for example a button's colour when it's hovered. The choices: transition-colors for colours…

The hover button from the last lesson jumps directly from blue to dark blue the moment the cursor arrives. Nothing is wrong, but it feels stiff. A transition smooths a change from one state to another, for example from blue to dark blue. With transition-colors duration-300, the change is spread over 300 milliseconds (ms), so the colour changes gently.

It's like a bedroom light: a normal switch makes it bright right away, while a light with a dimmer gets brighter little by little until it's full. A transition is that dimmer: the button's colour changes little by little instead of jumping. There is also animation (animate-*): movement that runs by itself and repeats forever, like a fan that keeps spinning without anyone touching it.

Transition: from A to B, smoothly

This is the same button as in the Lab:

<button class="bg-blue-500 hover:bg-blue-700 hover:scale-125 transition-colors duration-300 ease-in-out text-white px-4 py-2 rounded">
  Hover Me
</button>
Class CSS Tailwind v4 makes
transition-colors transition-property: color, background-color, border-color, ...
duration-300 transition-duration: 300ms;
ease-in-out transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);

Every transition-* class also brings a default duration of 150ms and a default rhythm that's the same as ease-in-out. So transition-colors on its own is already smooth, and duration-* or ease-* only need writing when you want a different value.

Which properties get smoothed

Class What it smooths
transition the ones used most: colours, opacity, shadows, transforms (move, rotate, size), filters
transition-colors colours only: background, text, border
transition-opacity opacity only
transition-shadow shadows only
transition-transform transforms only: move, rotate, size
transition-all every property that can be animated

So on that button, only the colour is smooth. transition-colors doesn't cover size, so scale-125 jumps directly to its new size. Want both smooth? Use transition or transition-all.

transition-all looks practical, but it also smooths changes you didn't mean to, like a width or a padding. Pick the narrowest class that covers everything that changes.

Duration and rhythm

The number in duration-* is milliseconds: duration-75, duration-150, duration-300, duration-500, duration-1000, and so on. In v4 any number works, like duration-250. For a button hover, 150 to 300ms feels right. 1,000ms (1 second) already feels slow.

Class How it feels
ease-linear the same speed from start to end
ease-in starts slow, speeds up at the end
ease-out starts fast, slows down at the end
ease-in-out slow, fast, then slow again

Animations that run by themselves: animate-*

A transition needs a trigger, like a hover. An animation runs by itself as soon as its class is on, and repeats forever.

Class The movement Usually used for
animate-spin one full turn every 1 second a loading icon
animate-ping grows to 2 times its size while fading out a notification dot
animate-pulse fades to half and back, every 2 seconds a skeleton (grey boxes that stand in for content that hasn't loaded)
animate-bounce bounces up and down a "scroll down" arrow

The spinner (a turning loading icon) from the exercise:

<div id="spinner" class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"></div>

⚠️ Motion that still jumps, or too much of it

TL;DR: transition-* classes smooth a change from one state to another, for example a button's colour when it's hovered. The choices: transition-colors for colours, transition-transform for moving, rotating and sizing, transition for the most common ones, transition-all for everything. duration-300 means 300 milliseconds (with no duration-*, 150ms), and ease-* sets the rhythm (how the speed changes). Write the transition class with no hover: prefix, so it's smooth when the cursor arrives and when it leaves. animate-* runs by itself with no trigger and repeats: animate-spin for loading, animate-pulse for skeletons (stand-ins for content that hasn't loaded), animate-ping and animate-bounce to draw attention.

⚡ From A to B, smoothly

Write:

<button class="bg-blue-500 hover:bg-blue-700 hover:scale-125 transition-colors duration-300 ease-in-out text-white px-4 py-2 rounded">
  Hover Me
</button>

Meaning: On hover, the background changes from bg-blue-500 to hover:bg-blue-700. transition-colors smooths that colour change, duration-300 makes it last 300 milliseconds, and ease-in-out sets the rhythm: slow, fast, then slow again.

⚡ Pick what gets smoothed

Guess first: hover:scale-125 makes that button 125% of its size. Does the button grow slowly too?

No, the size jumps right away. transition-colors only smooths colours. Want both smooth? Use transition.

Meaning: Pick the transition class that covers everything that changes, and nothing more. Write it with no hover: prefix, so it's smooth when the cursor arrives and when it leaves.

⚡ Animation that runs by itself

Write:

<div id="spinner" class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"></div>

Meaning: animate-spin turns a full circle every second, forever. border-t-transparent leaves a gap in the circle, and that gap is what makes the turning visible.

In the exercise, the checker looks for fully rounded corners, so rounded-full has to be there.

What you will learn