Sub topic 10
Writing custom keyframes and animation utilities
This chapter assumes you know the built-in animate-* utilities, the transition-* family, and the basics of CSS @keyframes. It also assumes you can read a Tailwind config file or a CSS file with @theme. By the end, you will be able to define a new animation utility, use it in markup, tune it per element, and avoid the usual mistakes.
Why define your own animation
The built-in utilities cover four effects: animate-spin, animate-ping, animate-pulse and animate-bounce. Real interfaces need others, such as a shake on a failed form, a fade-in for a new list item, or a wiggle that draws attention to a button.
A custom animation has two parts:
- A
@keyframesrule. It describes what the element looks like at points along the timeline. - An animation declaration. It sets which keyframes to play, how long they take, the easing, the repeat count and the other timing options.
Tailwind generates an animate-* utility from these two parts. Once the utility exists, it works with every variant (hover:, focus:, motion-safe:, group-hover:, and so on).
Anatomy of an animation value
The animation value is the CSS animation shorthand:
animation: wiggle 0.4s ease-in-out 2 normal both;| Part | Example | Meaning |
|---|---|---|
| Name | `wiggle` | The `@keyframes` rule to play |
| Duration | `0.4s` | Time for one cycle |
| Timing function | `ease-in-out` | Easing applied across each cycle |
| Iteration count | `2` or `infinite` | Number of cycles |
| Direction | `alternate` | Whether alternate cycles run backwards |
| Fill mode | `both` | Whether the first or last keyframe style applies before or after the run |
Unlike transitions, there is no separate delay-* utility for animations in the shorthand. If you need a delay, put it in the value, after the duration, as in wiggle 0.4s 200ms ease-in-out. The first time value is the duration and the second is the delay.
Defining an animation in Tailwind v4
In Tailwind v4, configuration lives in CSS. You define a theme variable in the --animate-* namespace and put the matching @keyframes inside 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. --animate-wiggle creates animate-wiggle. Use it like any other utility:
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:animate-wiggle">
Save
</button>On hover, the button wiggles twice, then stops.
Several animations at once
Add more variables and keyframes to the same block. This example adds an entrance animation:
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.4s ease-in-out 2;
--animate-fade-in-up: fade-in-up 0.5s ease-out both;
@keyframes wiggle {
0%, 100% { transform: rotate(-3deg); }
50% { transform: rotate(3deg); }
}
@keyframes fade-in-up {
from {
opacity: 0;
transform: translateY(0.75rem);
}
to {
opacity: 1;
transform: translateY(0);
}
}
}The keyword both is the fill mode. It applies the from style before the animation starts, which matters if you also set a delay. It keeps the to style after the animation ends. Without it, an element with a delay would show its final look, then jump to the from state when the animation begins.
<ul class="space-y-2">
<li class="motion-safe:animate-fade-in-up">First item</li>
<li class="motion-safe:animate-fade-in-up">Second item</li>
</ul>Unused animations are not emitted
Tailwind v4 only writes theme variables to the final CSS when a utility uses them. The same rule applies to the @keyframes inside @theme. If a keyframe appears to be missing from your build, check whether any class uses it. If you need the CSS variable or keyframe in the output regardless (for example, because you reference it from handwritten CSS), use @theme static { ... }.
Replacing or removing the defaults
Define a variable with the same name as a built-in to override it. This example makes animate-spin slower:
@theme {
--animate-spin: spin 2s linear infinite;
}To remove every default animation and keep only your own, reset the whole namespace first:
@theme {
--animate-*: initial;
--animate-wiggle: wiggle 0.4s ease-in-out 2;
@keyframes wiggle {
0%, 100% { transform: rotate(-3deg); }
50% { transform: rotate(3deg); }
}
}Defining an animation in Tailwind v3
Tailwind v3 has no @theme. You add two entries to theme.extend in tailwind.config.js: keyframes and animation. The keys must match: the animation value refers to the keyframes name.
// 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',
},
},
},
};The utility is again animate-wiggle. Put the entries under extend. If you put them directly under theme, they replace all the built-in animations, and animate-spin stops working.
One-off animations with arbitrary values
If an animation is used in exactly one place, you may not want to add it to the theme. An arbitrary value writes the declaration in the class itself. Spaces become underscores:
<div class="animate-[wiggle_1s_ease-in-out_infinite]"></div>This only references the wiggle keyframes. It does not create them, so the keyframes must already exist in your theme or CSS. In v4 you can also point at a CSS variable with parentheses:
<div class="animate-(--my-animation)"></div>Arbitrary values suit experiments and unusual timing. For anything reused, a named theme entry is easier to read and to change in one place.
Tuning an animation per element
A named animation fixes its duration, easing and repeat count. When two elements need the same motion with different settings, there are three options:
- Define a second, differently named entry, such as
--animate-wiggle-slow. - Use an arbitrary value, such as
animate-[wiggle_1s_ease-in-out_infinite]. - Put the changing part in a CSS variable and read it in the keyframes.
The third option keeps one keyframes rule for many looks. This example lets each element choose its own rotation angle:
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.4s ease-in-out 2;
@keyframes wiggle {
0%, 100% { transform: rotate(calc(var(--wiggle-angle, 3deg) * -1)); }
50% { transform: rotate(var(--wiggle-angle, 3deg)); }
}
}<span class="inline-block hover:animate-wiggle">Default angle</span>
<span class="inline-block hover:animate-wiggle [--wiggle-angle:10deg]">Wide angle</span>The fallback value 3deg inside var() applies when the element does not set the variable. The second element sets --wiggle-angle with the arbitrary property syntax [--wiggle-angle:10deg].
Note the inline-block class. CSS transforms do not apply to non-replaced inline elements such as a plain <span>, so the wiggle would do nothing without it.
Animation or transition?
Both change appearance over time, but they suit different jobs.
| Need | Use |
|---|---|
| Move smoothly between two states caused by a state change (hover, focus, a class toggle) | A transition |
| Play a sequence with more than two steps | A keyframe animation |
| Loop without any trigger | A keyframe animation |
| Run once on page load without a state change | A keyframe animation (or `starting:` in v4) |
| Reverse smoothly when the state ends | A transition |
A transition reverses automatically when the trigger ends. A keyframe animation does not. If hover:animate-wiggle is removed mid-run, the animation stops and the element snaps back to its normal style.
Common errors
The utility does nothing. Check that the keyframes name in the animation value is spelled exactly like the @keyframes name. A mismatch produces no error, only no motion.
The animation works in v4 but not in v3, or the reverse. The syntax is not interchangeable. @theme blocks are ignored by v3, and tailwind.config.js entries do not drive v4 unless you load the file explicitly with @config.
The element jumps at the end. The animation ran with the default fill mode, so the element returned to its base style. Add forwards (or both) to the animation value if the end state should persist.
Delayed animations flash. With a delay and no backwards or both fill mode, the element shows its base style during the delay. For entrance effects that start from opacity: 0, use both.
A transform in the keyframes fights with transform utilities. In v3, scale-*, rotate-* and translate-* utilities all write to transform through CSS variables, and an animation that sets transform directly overrides them while it runs. In v4 these utilities set the individual scale, rotate and translate properties, which combine with a transform value instead of replacing it. When mixing them, check the result in the browser.
It ignores accessibility. A looping or large movement can cause discomfort for users who ask for less motion. Pair custom animations with motion-safe: (or cancel them with motion-reduce:animate-none), as shown in the chapter on reduced motion.
Exercises
Exercise
You add --animate-shake: shake 0.5s ease-in-out; inside @theme in Tailwind v4 but forget to write the @keyframes shake rule. You use class="animate-shake". What happens, and how do you fix it?
Show answer
The utility animate-shake is generated and the browser applies animation: shake 0.5s ease-in-out, but no keyframes named shake exist, so nothing visibly moves and no error is reported. Fix it by adding @keyframes shake { ... } inside the same @theme block, with steps such as 0%, 100% { transform: translateX(0); } and 25% { transform: translateX(-4px); }.
Exercise
Write the Tailwind v3 theme.extend entries for an animation called fade-in-up that lasts 0.5 seconds, uses ease-out, and keeps its final style after it ends. The element starts at opacity: 0 and translateY(0.75rem) and ends at opacity: 1 and translateY(0).
Show answer
Use keyframes 'fade-in-up': { from: { opacity: '0', transform: 'translateY(0.75rem)' }, to: { opacity: '1', transform: 'translateY(0)' } } and animation 'fade-in-up': 'fade-in-up 0.5s ease-out both'. Both sit under theme.extend, in keyframes and animation respectively. The fill mode both (or forwards) keeps the end style.
Exercise
A button has hover:animate-wiggle. When the pointer leaves the button halfway through the wiggle, the button snaps back to its resting position instead of easing back. Why, and what would you use if you wanted a smooth return?
Show answer
A keyframe animation stops as soon as the hover class stops applying, and the element returns to its base style immediately. A transition reverses smoothly when its trigger ends. For a smooth return, use a transition on a property such as transform (for example transition-transform hover:rotate-3) instead of a keyframe animation.