Skip to content

Lesson 39 of 52Tailwind in practice

Tailwind Transitions and Animation

Add motion with Tailwind: transition, duration and easing utilities, transforms, built-in and custom animations, enter effects with starting:, and reduced motion.

19 min read0/5 questions

Good motion is mostly invisible. A button that darkens over 150 milliseconds instead of snapping, a card that lifts a few pixels when you point at it, a spinner that tells you something is happening: none of these are decoration, they are feedback. You learned how transitions, transforms and keyframes work in plain CSS in the animations lesson. This lesson shows how Tailwind turns all of it into short classes, how to add your own animations to the theme, how to animate elements as they appear with starting:, and how to keep everything fast and kind to people who prefer less motion.

The transition utilities

A transition needs three things: which properties to smooth, how long it takes, and how the speed changes along the way (the easing). Tailwind gives each its own class.

ClassWhat it sets
transitionThe common properties: colours, opacity, shadows, transforms, filters. 150ms with a gentle ease by default
transition-colorsOnly colour properties: text, background, border, outline, fill, stroke
transition-opacityOnly opacity
transition-transformOnly movement, scaling and rotation
transition-shadowOnly box-shadow
transition-allEvery property that can animate
transition-noneNo transition
duration-300transition-duration: 300ms. Any number works in version 4
ease-out, ease-in, ease-in-out, ease-linearThe timing function
delay-150Wait 150ms before starting

Two habits follow from the plain CSS lesson. First, put the transition classes on the element without a variant, so the change animates in both directions; hover:transition would fade in and snap back. Second, prefer a specific class like transition-colors over transition-all, so you animate only what you meant to, and a layout change elsewhere does not start sliding around.

HTML + Tailwind
<div class="flex flex-wrap gap-3 p-2"> <button class="rounded-lg bg-indigo-600 px-4 py-2 text-white transition-colors duration-75 hover:bg-indigo-400">75ms</button> <button class="rounded-lg bg-indigo-600 px-4 py-2 text-white transition-colors duration-300 hover:bg-indigo-400">300ms</button> <button class="rounded-lg bg-indigo-600 px-4 py-2 text-white transition-colors duration-700 ease-in hover:bg-indigo-400">700ms ease-in</button></div><a href="#" class="mt-4 block max-w-xs rounded-xl border border-slate-200 bg-white p-5 transition duration-200 ease-out hover:-translate-y-1 hover:shadow-lg hover:ring-2 hover:ring-indigo-200 focus-visible:-translate-y-1 focus-visible:ring-2 focus-visible:ring-indigo-500 focus-visible:outline-hidden"> <p class="text-sm font-semibold text-indigo-600">Guide</p> <h3 class="mt-1 text-lg font-bold">Batch cooking for busy weeks</h3> <p class="mt-1 text-sm text-slate-500">Hover or Tab to this card.</p></a>

Preview

Move the pointer across the three buttons. 75ms feels instant but smooth; 700ms feels sluggish for something as small as a button. Most interface transitions sit between 100ms and 300ms. The card shows the classic hover lift: hover:-translate-y-1 raises it 4px (the minus sign makes it negative, so upwards), while hover:shadow-lg and hover:ring-2 make it feel closer. Notice the same lift is repeated on focus-visible:, so keyboard users get the same feedback. Try hover:scale-[1.02] instead of the lift.

A few rules of thumb for picking values. Small things change fast: colour on a button or link, 100ms to 150ms. Medium things, like a card lifting or a menu opening, take 200ms to 300ms. Big movements across the screen, such as a drawer sliding in, can take up to about 400ms. Use ease-out for things entering or reacting to the user, because they start quickly and settle gently, and ease-in for things leaving. Shadows and rings are a good way to suggest depth on hover, but keep them subtle, and remember that ring-* draws with box-shadow, so it animates with transition or transition-shadow.

Predict the outputHTML

How long, in milliseconds, does the colour change take on hover? Type a number.

<button class="transition-colors delay-100 duration-200 hover:bg-sky-500">
Need a nudge? Hints walk you through it.

Transforms

Transforms move, resize or rotate an element without affecting the layout around it. Neighbours stay where they are, which is exactly why transforms are cheap to animate.

  • Translate: translate-x-4, -translate-y-1, translate-y-full, -translate-x-1/2. Uses the spacing scale, fractions or arbitrary values.
  • Scale: scale-95, scale-105, scale-x-0. The number is a percentage.
  • Rotate: rotate-3, -rotate-6, rotate-90, rotate-[17deg].
  • Origin: origin-left, origin-top-right set the point the element scales or rotates around.

In version 4 these use the CSS translate, scale and rotate properties, so you can combine them freely without a separate transform class. Delays let you stagger several items so they move one after another.

HTML + Tailwind
<div class="group flex max-w-sm cursor-pointer items-center gap-4 rounded-xl border border-slate-200 p-4"> <div class="grid size-12 shrink-0 place-items-center rounded-lg bg-amber-300 font-bold transition-transform duration-300 group-hover:rotate-12 group-hover:scale-110"> Aa </div> <div class="flex-1"> <p class="font-semibold">Hover this whole card</p> <div class="mt-2 flex gap-1"> <span class="h-2 w-6 origin-left scale-x-50 rounded bg-indigo-400 transition-transform duration-300 group-hover:scale-x-100"></span> <span class="h-2 w-6 origin-left scale-x-50 rounded bg-indigo-400 transition-transform delay-75 duration-300 group-hover:scale-x-100"></span> <span class="h-2 w-6 origin-left scale-x-50 rounded bg-indigo-400 transition-transform delay-150 duration-300 group-hover:scale-x-100"></span> </div> </div> <span class="text-xl transition-transform group-hover:translate-x-1" aria-hidden="true">&gt;</span></div>

Preview

The tile rotates and grows, the bars grow from their left edge one after another, and the arrow nudges right. Change origin-left to origin-right on the bars, or remove the delays to see how much the stagger adds.

Built-in animations

Transitions need a trigger. Animations run on their own. Tailwind ships four, each a keyframe animation:

ClassWhat it doesTypical use
animate-spinRotates 360 degrees, forever, at constant speedLoading spinners
animate-pingScales up and fades out, like a rippleNotification dots, "live" badges
animate-pulseFades gently in and outSkeleton loaders
animate-bounceBounces up and down"Scroll down" hints

A skeleton loader is a grey outline of the content that is coming, shown while it loads. It feels faster than a blank screen or a lone spinner because the page already has its shape.

HTML + Tailwind
<div class="flex items-center gap-6 p-2"> <svg class="size-6 animate-spin text-indigo-600 motion-reduce:animate-none" viewBox="0 0 24 24" fill="none" aria-hidden="true"> <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" class="opacity-25"></circle> <path d="M22 12a10 10 0 0 0-10-10" stroke="currentColor" stroke-width="4"></path> </svg> <span class="relative flex size-3"> <span class="absolute inline-flex size-full animate-ping rounded-full bg-emerald-400 opacity-75 motion-reduce:hidden"></span> <span class="relative inline-flex size-3 rounded-full bg-emerald-500"></span> </span> <span class="animate-bounce motion-reduce:animate-none" aria-hidden="true">v</span></div><div class="mt-4 max-w-sm rounded-xl border border-slate-200 p-4" aria-busy="true"> <span class="sr-only">Loading recipe</span> <div class="flex animate-pulse gap-4 motion-reduce:animate-none"> <div class="size-14 rounded-lg bg-slate-200"></div> <div class="flex-1 space-y-2 py-1"> <div class="h-3 w-3/4 rounded bg-slate-200"></div> <div class="h-3 w-1/2 rounded bg-slate-200"></div> <div class="h-3 w-5/6 rounded bg-slate-200"></div> </div> </div></div>

Preview

Each animation has a motion-reduce: fallback. The ping ripple is hidden completely for reduced motion while the solid dot stays, so the meaning ("online") survives. The skeleton is marked aria-busy="true" and carries a sr-only label, because screen readers cannot see grey bars. Change w-3/4 to w-full to reshape the skeleton to match your real content.

Match the pairs0/4 matched

Match each built-in animation to its best use.

Choose an item, then its match on the other side.

Custom animations in @theme

For anything else, add your own. In version 4 an animation is a theme variable in the --animate-* namespace, and its @keyframes can live right inside the same @theme block. That gives you an animate-wiggle class that works with variants like any other.

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --animate-wiggle: wiggle 0.4s ease-in-out 2; --animate-rise: rise 0.5s ease-out both; --ease-snappy: cubic-bezier(0.2, 0.9, 0.3, 1.3); @keyframes wiggle { 0%, 100% { transform: rotate(0deg); } 25% { transform: rotate(-6deg); } 75% { transform: rotate(6deg); } } @keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } } }</style><div class="space-y-4 p-2"> <button class="rounded-lg bg-rose-600 px-4 py-2 font-semibold text-white hover:animate-wiggle motion-reduce:hover:animate-none"> Hover to wiggle </button> <ul class="space-y-2"> <li class="motion-safe:animate-rise rounded-lg bg-slate-100 p-3">Breakfast</li> <li class="motion-safe:animate-rise rounded-lg bg-slate-100 p-3 [animation-delay:120ms]">Lunch</li> <li class="motion-safe:animate-rise rounded-lg bg-slate-100 p-3 [animation-delay:240ms]">Dinner</li> </ul> <button class="rounded-lg border border-slate-300 px-4 py-2 transition duration-300 ease-snappy hover:scale-110">Snappy ease</button></div>

Preview

The list items rise in one after another when the preview loads; edit any character to replay them. both in --animate-rise keeps the first keyframe applied during the delay, so the later items do not flash before they start. The delay-* utilities only set transition-delay, so the stagger uses arbitrary properties like [animation-delay:120ms]. The third button uses a custom easing token: --ease-snappy creates ease-snappy, and its last number above 1 makes it overshoot slightly. Try making wiggle run infinite times instead of 2, and you will see why looping motion should be rare.

Quick check
You want an animate-float class that gently moves an element up and down. Where does it go in Tailwind v4?
Need a nudge? A hint rules out a wrong answer.

Enter animations with starting:

There is one moment a normal transition cannot catch: when an element first appears. It has no previous style to transition from, so it just pops in. The CSS @starting-style rule fixes that by declaring the style an element starts from on its first frame, and Tailwind exposes it as the starting: variant.

A toast appearing with starting:
ScriptBrowserToastappend the toastuse starting: stylesapply normal stylestransition runs

JavaScript adds a new element to the page.

1/4
HTML + Tailwind
<button id="add" class="rounded-lg bg-slate-900 px-4 py-2 text-white transition-colors hover:bg-slate-700">Add to basket</button><div id="toasts" class="mt-4 flex max-w-xs flex-col gap-2" aria-live="polite"></div><script> let count = 0; document.getElementById("add").addEventListener("click", () => { count += 1; const toast = document.createElement("p"); toast.className = "rounded-lg bg-emerald-600 px-4 py-3 text-sm text-white shadow-md transition duration-300 ease-out starting:translate-y-2 starting:opacity-0 motion-reduce:transition-none"; toast.textContent = "Added item " + count + " to your basket"; document.getElementById("toasts").prepend(toast); });</script>

Preview

Click the button a few times and each message slides up and fades in. Remove the two starting: classes and they just appear. @starting-style is supported in current versions of all major browsers, but it arrived during 2024, so check Can I Use if you support older ones; browsers without it simply show the element with no animation, which is a safe fallback. For elements that toggle display (a dialog or a popover), Tailwind's docs pair starting:open: with transition-discrete so the hidden state can animate too; read the starting and transition-behavior sections there before you build one. Also note the class list lives in the script as a full string, so a real Tailwind build can still find it.

Performance and reduced motion

Two rules keep motion smooth and respectful.

Animate transform and opacity. As the plain CSS lesson showed, these can skip layout and paint. In Tailwind terms: reach for translate-*, scale-*, rotate-* and opacity-*, not w-*, h-*, top-* or m-*. Colour and shadow transitions are fine on small elements. will-change-transform hints that an element will move, but add it only when you have measured a problem.

Respect prefers-reduced-motion. Some people get dizzy or nauseous from movement and switch it off in their system settings. motion-safe: applies only when they have not, and motion-reduce: applies only when they have.

Anatomy of a motion-friendly class list
  1. motion-reduce:transition-noneguard: respect the user setting
  2. hover:-translate-y-1 hover:shadow-lgtarget: where it ends up
  3. transition duration-200 ease-outbase: how it moves

The base layer sits on the element with no variant, so the change animates both ways.

1/3

Choose between the two styles by what is safer to forget. motion-safe:animate-rise is opt-in: motion only exists when it is allowed. motion-reduce:animate-none is opt-out: handy when adding to existing classes. To test, open Chrome devtools, find the Rendering panel in the command menu, and emulate prefers-reduced-motion: reduce. The page must still make complete sense with every animation off.

Put it in order5 steps

Put these steps for adding a hover lift to a card in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Quick check
A dropdown menu stutters as it opens. Its classes are transition-all duration-300 h-0 open:h-64. What is the best change?
Need a nudge? A hint rules out a wrong answer.
Key terms
transition
A smooth change between two values of a property, triggered by a state change like hover.
duration
How long a transition takes, set with classes like duration-200.
easing
How speed changes during a transition, such as ease-out (fast start, slow finish).
transform
Moving, scaling or rotating an element without changing the layout around it.
skeleton loader
Grey placeholder shapes that show the layout of content while it loads.
starting: variant
Tailwind's wrapper for @starting-style, the styles an element animates from when it first appears.
prefers-reduced-motion
A user setting, read by motion-safe: and motion-reduce:, that asks for less animation.