Sub topic 7
Built-in animations
This chapter assumes you know basic HTML and CSS, can read Tailwind utility classes, and understand the difference between a CSS transition and a keyframe animation. It covers the four animation utilities that ship with Tailwind: animate-spin, animate-ping, animate-pulse and animate-bounce.
Transitions versus animations
A transition moves an element between two states when something triggers it, such as a hover. An animation runs a @keyframes sequence on its own, with no state change needed. It can loop forever.
That difference decides which tool you reach for.
| Need | Use |
|---|---|
| Respond to hover, focus or a class change | `transition-*` utilities |
| Show continuous activity, like loading | `animate-*` utilities |
| Play a multi-step sequence | `animate-*` with custom keyframes |
The built-in utilities cover the continuous cases that come up most often in interfaces. Each one sets the animation shorthand property and ships with a matching @keyframes rule that Tailwind adds to your CSS when you use the class.
The four built-in utilities
| Class | What it does | Typical use |
|---|---|---|
| `animate-spin` | Rotates 360 degrees every 1 second, linear, infinite | Loading spinners |
| `animate-ping` | Scales to 2x while fading to transparent, every 1 second, infinite | Notification dots, "live" indicators |
| `animate-pulse` | Fades opacity to 50% and back every 2 seconds, infinite | Skeleton placeholders |
| `animate-bounce` | Moves up and down every 1 second, infinite | Scroll-down hints |
There is also animate-none, which removes any animation. You use it to switch an animation off at a breakpoint or in a state.
animate-spin
animate-spin rotates an element continuously. It uses a linear timing function, so the speed stays constant. An eased rotation would visibly slow down and speed up on every turn.
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin"></div>The element is a circle with a thin border. Only the top border has a different color, so the rotation is visible. If the element were a plain, uniformly colored circle, you would see nothing change. The motion-safe: prefix applies the animation only when the user has not asked for reduced motion. Later in this chapter you will see why that matters.
You can also spin an icon inside a button. Put the class on the SVG, not on the button, or the whole button rotates.
<button class="inline-flex items-center gap-2 rounded bg-indigo-600 px-4 py-2 text-white" disabled>
<svg class="size-4 motion-safe:animate-spin" 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="M4 12a8 8 0 018-8" stroke="currentColor" stroke-width="4" class="opacity-75"></path>
</svg>
Saving...
</button>The text "Saving..." stays visible even if the spinner is turned off by a reduced motion setting. That is deliberate: the text carries the meaning, and the spinner is decoration.
animate-ping
animate-ping grows an element to twice its size while it fades out, then restarts. On its own it looks like an expanding ripple. It is almost always layered over a static element of the same shape, so the ripple appears to radiate from a solid dot.
<span class="relative flex size-3">
<span class="absolute inline-flex size-full rounded-full bg-sky-400 opacity-75 motion-safe:animate-ping"></span>
<span class="relative inline-flex size-3 rounded-full bg-sky-500"></span>
</span>Here is how the layers work:
- The outer
spanisrelative, which makes it the positioning reference for its children. - The first inner
spanisabsoluteand fills the parent withsize-full. It is the one that pings. - The second inner
spanisrelative, so it stays on top in normal flow, and it never animates.
If you remove absolute from the pinging layer, it pushes the dot sideways as it grows, because it takes up space in the layout. Positioning it absolutely takes it out of the flow, so the ripple does not disturb neighbors.
animate-pulse
animate-pulse fades an element between full opacity and 50% opacity on a two-second cycle, with an ease-in-out curve. It is the standard way to build skeleton screens, which are gray placeholder shapes shown while content loads.
<div class="w-full max-w-sm rounded-lg border border-slate-200 p-4">
<div class="motion-safe:animate-pulse flex gap-4">
<div class="size-10 rounded-full bg-slate-200"></div>
<div class="flex-1 space-y-3 py-1">
<div class="h-2 rounded bg-slate-200"></div>
<div class="h-2 w-5/6 rounded bg-slate-200"></div>
</div>
</div>
</div>The animate-pulse class sits on the wrapper, not on each gray bar. The whole group fades in sync, which looks calmer than several bars pulsing independently. Only the wrapper needs the class because opacity applies to the element and everything inside it.
animate-bounce
animate-bounce moves an element up and down with a bounce-like curve. It is useful for drawing attention to a single control, such as a "scroll down" arrow.
<button class="size-10 rounded-full bg-white p-2 shadow ring-1 ring-slate-900/5 motion-safe:animate-bounce" aria-label="Scroll down">
<svg class="size-6 text-indigo-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 14l-7 7m0 0l-7-7m7 7V3" />
</svg>
</button>Use it sparingly. A bouncing element pulls the eye away from everything else on the page, so a page with several of them is tiring to read.
Seeing the keyframes
Each utility maps to a keyframe rule. In Tailwind v4, these are defined as theme variables, so you can read and override them. The values below match the defaults.
| Utility | Animation value |
|---|---|
| `animate-spin` | `spin 1s linear infinite` |
| `animate-ping` | `ping 1s cubic-bezier(0, 0, 0.2, 1) infinite` |
| `animate-pulse` | `pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite` |
| `animate-bounce` | `bounce 1s infinite` |
The ping keyframes run to transform: scale(2); opacity: 0 at 75% and 100%, so the ripple finishes at the 75% mark and then waits briefly before repeating. The bounce keyframes alternate between two timing functions, which is why the motion looks like gravity: it accelerates on the way down and decelerates on the way up.
Rather than a diagram of keyframes, the simpler mental model is a timeline for one cycle of animate-ping.
Applying animations conditionally
Animation classes accept variants like any other utility. That lets you turn an animation on only in a given state.
<button
class="group rounded bg-indigo-600 px-4 py-2 text-white disabled:opacity-60"
disabled
>
<span class="inline-block size-4 rounded-full border-2 border-white/40 border-t-white group-disabled:motion-safe:animate-spin"></span>
</button>Variants stack from left to right and each one must be true for the class to apply. In group-disabled:motion-safe:animate-spin, the spinner runs only when the parent group is disabled and the user allows motion.
A common real-world pattern drives the animation from application state, such as a loading flag in a framework, by toggling the class:
<div class="size-5 rounded-full border-2 border-slate-300 border-t-indigo-600 animate-spin motion-reduce:animate-none"></div>Here motion-reduce:animate-none is the opposite way to write the same guard. The spinner runs by default and is removed for users who prefer reduced motion. Choose motion-safe: when the animation is an extra, and motion-reduce:animate-none when the default should be animated and you want an explicit opt-out.
Common errors
- Nothing seems to move. The element may have no visible features. A solid circle that spins looks static. Give it an uneven border, an icon or an image.
- Spinner wobbles or drifts.
animate-spinrotates around the element's center. If the element is not square, the rotation looks lopsided. Use equal width and height, such assize-6. - Ping shifts the layout. The pinging layer must be
absolute, inside arelativeparent. - Skeleton blocks vanish.
animate-pulseonly changes opacity. Placeholders need a background color such asbg-slate-200, or there is nothing to fade. - Animation is on forever. All four utilities use
infinite. If a loading state ends, remove the class when the content arrives. Do not just hide the element, because a hidden element withdisplay: nonestops animating, but one hidden withopacity-0keeps running and wastes work. - Animation conflicts with a transform.
animate-spinandanimate-bounceanimatetransformthrough keyframes. A staticrotate-45or-translate-y-1on the same element can be overridden or look wrong while the animation runs. Put the static transform on a parent or child element instead. In Tailwind v4, the separaterotate,scaleandtranslateproperties that the transform utilities use can combine with an animation that usestransform, but do not rely on that when you support older browsers.
Accessibility
Continuous motion can cause real problems for some users, including dizziness and nausea for people with vestibular disorders. Operating systems expose a "reduce motion" setting, and browsers report it through the prefers-reduced-motion media query. Tailwind turns this into two variants:
motion-safe:applies its utility only when the user has not asked for reduced motion.motion-reduce:applies its utility only when the user has asked for reduced motion.
Spinners and pulses that signal "something is happening" still need an alternative for those users. A static label, such as "Loading...", is enough. Always pair a decorative spinner with text or an aria-label, and mark the spinner graphic aria-hidden="true" when text already describes the state.
For a loading state that also needs to be announced to screen readers, add a status role:
<div role="status" class="flex items-center gap-2">
<div class="size-5 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin" aria-hidden="true"></div>
<span>Loading...</span>
</div>The next chapter covers reduced motion in more detail. For now, treat motion-safe: as the default prefix for any looping animation.
Choosing between them
- Use
animate-spinwhen the user waits for an operation of unknown length. - Use
animate-pulsewhen you know the shape of the content that is coming. - Use
animate-pingto signal new or live activity on a small element. - Use
animate-bouncefor a single, rare call to attention.
If none fits, you can define your own keyframes. That is covered in a later chapter.
Exercise
You build a "live" status badge as a single green dot with animate-ping on it. On the page, the dot grows and pushes the text next to it sideways. What two changes fix it, and what does the final structure look like?
Show answer
Put the pinging dot on its own element with absolute, inside a relative parent, so it leaves the normal layout flow. Then add a second, static dot on top as a relative element so a solid dot remains visible. The structure is a relative flex size-3 wrapper holding an absolute inline-flex size-full rounded-full bg-green-400 opacity-75 motion-safe:animate-ping span and a relative inline-flex size-3 rounded-full bg-green-500 span.
Exercise
A skeleton loader uses animate-pulse on three gray bars, each with its own class, and the result looks jittery. Where should the class go instead, and why does a user with reduced motion enabled need something else on screen?
Show answer
Move animate-pulse (as motion-safe:animate-pulse) to the wrapper that contains the bars, so they fade together. A user with reduced motion enabled sees static bars, so the page should also have a text or ARIA indication that content is loading, such as a role="status" element with the text "Loading...".