Sub topic 10
Writing custom keyframes and animation utilities
This chapter assumes you know the built-in animate-* utilities, how a CSS @keyframes rule works, and where your Tailwind setup lives. That is the CSS file with @import "tailwindcss" in v4, or tailwind.config.js in v3.
The built-in animations are spin, ping, pulse and bounce. When you need something else, such as a wiggle, a fade-in or a shake, you define your own keyframes and register an animation utility for them.
The two parts of a custom animation
A custom animation has two pieces:
- Keyframes describe what changes. They name a sequence of styles at percentages of the timeline.
- An animation value is the CSS
animationshorthand. It sets the keyframes name, duration, easing and iteration count.
Tailwind joins them under one name, and that name becomes the animate-* utility.
Tailwind v4: define it in CSS
In v4 you configure the theme in CSS. A variable in the --animate-* namespace creates an animate-* utility, and the keyframes go in the same @theme block.
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.4s ease-in-out 2;
@keyframes wiggle {
0%, 100% { transform: rotate(-3deg); }
50% { transform: rotate(3deg); }
}
}The variable name after --animate- becomes the utility name, so this gives you animate-wiggle. The value is a normal animation shorthand: the keyframes name wiggle, a 0.4 second duration, ease-in-out easing, and two iterations. Keeping the @keyframes inside @theme ties it to the variable, so Tailwind emits the keyframes whenever you use the utility.
Tailwind v3: define it in the config
In v3 the same utility comes from two entries under theme.extend: one for keyframes and one for animation.
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
keyframes: {
wiggle: {
'0%, 100%': { transform: 'rotate(-3deg)' },
'50%': { transform: 'rotate(3deg)' },
},
},
animation: {
wiggle: 'wiggle 0.4s ease-in-out 2',
},
},
},
};Put both under extend. Writing them directly under theme replaces the built-in spin, ping, pulse and bounce animations.
Using the utility
Apply it like any other animation, and wrap it in motion-safe: so users who ask for reduced motion do not get it.
<button class="rounded bg-indigo-600 px-4 py-2 text-white motion-safe:hover:animate-wiggle">
Notifications
</button>Variants stack from left to right. This button wiggles only on hover, and only when the user has not requested reduced motion.
One-off animations with arbitrary values
For a single use you can skip the theme entry. An arbitrary value such as animate-[wiggle_1s_ease-in-out_infinite] writes the shorthand inline, with underscores standing in for spaces. The keyframes named in it must still exist, so you still define @keyframes wiggle somewhere. Prefer a theme entry when you reuse the animation.
Common errors
| Symptom | Likely cause |
|---|---|
| `animate-wiggle` does nothing | The keyframes name does not match the name in the animation value. |
| Built-in `animate-spin` stopped working (v3) | `keyframes` or `animation` was placed outside `extend`. |
| The animation runs once and stops | The value has no iteration count. Add `infinite` or a number. |
| The utility is not generated (v4) | The variable is not in the `--animate-*` namespace. |
Exercise
In Tailwind v4, write the @theme block that creates an animate-fade-in utility lasting 300 milliseconds, running once with ease-out. It should go from opacity: 0 to opacity: 1.
Show answer
@theme {
--animate-fade-in: fade-in 0.3s ease-out 1;
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
}
Use it as motion-safe:animate-fade-in. The name after --animate- becomes the utility name, and the first word of the value must match the @keyframes name.