Sub topic 7
Built-in animations
This chapter assumes you know basic HTML and CSS, can read Tailwind utility classes, and have a rough idea of how CSS @keyframes differ from transitions. A transition moves between two states when something changes. A keyframe animation runs on its own, without a trigger, and can repeat.
What Tailwind provides
Tailwind ships four animate-* utilities. Each one maps to a CSS animation with its keyframes already defined, so you only add the class.
| Utility | What it does | Typical use |
|---|---|---|
| `animate-spin` | Rotates 360 degrees every second, forever, at a constant speed | Loading spinners |
| `animate-ping` | Scales the element up to twice its size while fading it out, repeating | Notification dots, "live" indicators |
| `animate-pulse` | Fades opacity to 50% and back every two seconds | Skeleton placeholders |
| `animate-bounce` | Moves the element up and down with an easing curve | Scroll-down hints |
There is also animate-none, which removes any animation. It is useful for turning an animation off at a given breakpoint or state, for example animate-spin hover:animate-none.
A spinner
A spinner is a bordered circle with one colored edge, rotated continuously.
<div class="size-6 rounded-full border-2 border-slate-300 border-t-indigo-600 motion-safe:animate-spin"></div>The border-2 border-slate-300 classes draw a full gray ring. border-t-indigo-600 recolors only the top edge, which makes the rotation visible. animate-spin rotates the element, and a linear timing function keeps the speed constant.
The motion-safe: prefix applies the animation only when the user has not asked their system to reduce motion. The next chapter covers this in detail. For now, treat it as the default for any animation that is not essential.
A skeleton placeholder
animate-pulse suits content that is still loading. You draw gray shapes where the real content will appear, and the pulse signals that something is on its way.
<div class="flex w-64 gap-4 motion-safe:animate-pulse">
<div class="size-12 rounded-full bg-slate-200"></div>
<div class="flex-1 space-y-3 py-1">
<div class="h-3 rounded bg-slate-200"></div>
<div class="h-3 w-5/6 rounded bg-slate-200"></div>
</div>
</div>Put the animation on the parent, as above. Every child then fades together. If you put it on each child, they all start at the same moment anyway, so you gain nothing and repeat the class.
A ping indicator
animate-ping is usually used on a copy of the element. One layer ripples outward while a solid layer stays still on top.
<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>The outer span is positioned absolutely and animates. The inner span is the dot that stays visible. Without the stationary dot, the indicator would vanish at the end of every cycle, because ping ends fully transparent.
Common errors
- Animating an inline element.
transformhas no effect on plain inline elements such as a bare<span>. Addinline-block,blockorflexso spin and bounce work. - Expecting a hover trigger. Built-in animations run as soon as the class applies. To start one on hover, use a variant:
hover:animate-spin. - Using bounce or ping on large areas. These animations are noticeable and distracting on big elements. Keep them small.
- Forgetting reduced motion. An endless spinner or pulse is exactly what some users want to avoid. Use
motion-safe:or addmotion-reduce:animate-none.
Choosing an animation
- Use
animate-spinwhen the user waits on a process of unknown length. - Use
animate-pulsewhen you know the shape of what will load. - Use
animate-pingto draw the eye to a small status marker. - Use
animate-bouncesparingly, to hint at a direction.
Exercise
A teammate writes <span class="animate-spin">⟳</span> and the icon does not rotate. Name the likely cause and give a corrected version that also respects reduced motion.
Show answer
A bare <span> is an inline element, and CSS transforms do not apply to inline elements. Make it inline-block: <span class="inline-block motion-safe:animate-spin">⟳</span>. The motion-safe: prefix limits the animation to users who have not requested reduced motion.
Exercise
Why does a ping indicator need a second, non-animated element?
Show answer
animate-ping scales the element up and fades it to fully transparent on every cycle. Without a static dot beneath it, the indicator would disappear at the end of each cycle. The still element keeps a visible marker while the animated one produces the ripple.