Sub topic 11
Defining animations and keyframes in a theme
This chapter assumes you can write a basic @theme block, know that variable names in a namespace such as --color-* produce utilities, and have Tailwind v4 imported with @import "tailwindcss";. Here you will apply the same idea to animations. A variable in the --animate-* namespace becomes an animate-* utility, and the @keyframes rule it uses can live in the same block.
The --animate-* namespace
Each namespace maps to a family of utilities. The --animate-* namespace maps to the animation property. The default theme already ships four of these variables: --animate-spin, --animate-ping, --animate-pulse and --animate-bounce. That is why animate-spin works without any setup.
An animation variable holds a value for the CSS animation shorthand: a keyframes name, a duration, a timing function, and optionally a delay, iteration count and so on.
@import "tailwindcss";
@theme {
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}Tailwind now generates animate-fade-in. You use it like any other utility:
<div class="animate-fade-in rounded bg-white p-6 shadow">
This panel fades in when it is rendered.
</div>What Tailwind generates
The utility sets animation to the theme variable. The keyframes are emitted next to it. The flow looks like this:
The generated CSS has roughly this shape:
.animate-fade-in {
animation: var(--animate-fade-in);
}
:root {
--animate-fade-in: fade-in 0.3s ease-out;
}
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}The exact order and wrapping layers can differ between versions. What matters is that the utility, the variable and the keyframes travel together, and that all three disappear if nothing uses the class.
Why keyframes belong inside @theme
You can write @keyframes anywhere in your CSS. A rule outside @theme is always emitted, even if no element uses it. A rule inside @theme is treated like a theme variable. It is emitted only when something uses it, so unused animations add no bytes to your stylesheet.
Putting the keyframes next to the variable has a second benefit. The animation is defined in one place, and you can see its name, timing and steps together.
A multi-step animation
Keyframes can use percentages, and a single @theme block can define several animations. This example adds a wiggle and a slide-up.
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.6s ease-in-out infinite;
--animate-slide-up: slide-up 0.4s ease-out both;
@keyframes wiggle {
0%,
100% {
transform: rotate(-3deg);
}
50% {
transform: rotate(3deg);
}
}
@keyframes slide-up {
from {
opacity: 0;
transform: translateY(1rem);
}
to {
opacity: 1;
transform: translateY(0);
}
}
}The both fill mode in --animate-slide-up keeps the first frame applied during any delay and the last frame applied afterward. Without it, the element would show its normal state until the animation starts.
Because these are ordinary utilities, variants apply to them:
<button class="hover:animate-wiggle rounded bg-indigo-600 px-4 py-2 text-white">
Hover me
</button>
<p class="motion-safe:animate-slide-up">
Only animates for users who have not asked for reduced motion.
</p>Use motion-safe: or motion-reduce: for any animation that moves things around. Users with vestibular disorders often enable a reduced-motion setting, and these variants respect it.
Reusing other theme variables
An animation value can reference other theme variables with var(). The easing token from an earlier chapter works well here:
@import "tailwindcss";
@theme {
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
--animate-pop: pop 250ms var(--ease-fluid);
@keyframes pop {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
}If you later change --ease-fluid, animate-pop follows. The same reference works for duration tokens you define yourself, for example --duration-quick under a custom name, but note that Tailwind has no built-in namespace for that. A custom name only gives you a variable you can read with var(). It does not create a utility.
Extending versus replacing the defaults
Defining --animate-fade-in adds to the default animations. Existing ones such as animate-spin keep working. To change a default, redefine its variable:
@theme {
--animate-spin: spin 2s linear infinite;
}To remove every default animation and keep only your own, reset the namespace with initial first:
@import "tailwindcss";
@theme {
--animate-*: initial;
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}After this, animate-spin, animate-ping, animate-pulse and animate-bounce no longer exist. Tailwind's animate-none utility, which sets animation: none, is not tied to a theme variable, so it keeps working.
Using a theme animation in custom CSS
Animation variables are ordinary CSS variables, so your own rules can read them:
.toast {
animation: var(--animate-slide-up);
}The var() reference makes Tailwind keep the variable, and the keyframes it depends on, in the output. This works even when no HTML file uses the animate-slide-up class.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The class `animate-fade-in` does nothing | The variable name is wrong, such as `--animation-fade-in` | Use the `--animate-` prefix exactly |
| The animation runs but nothing moves | The name after the variable's colon does not match the `@keyframes` name | Make both names identical |
| Keyframes missing from the output | Nothing uses the animation, so Tailwind dropped it | Use the class, reference the variable with `var()`, or use `@theme static` |
| A default animation still exists after a reset | The reset line came after your definitions, or you reset the wrong namespace | Put `--animate-*: initial;` first in the block |
| The element flashes its end state before animating | A delay is set and the fill mode is missing | Add `both` or `backwards` to the animation value |
Choosing where to put keyframes
- Use
@keyframesinside@themewhen the animation is a design token, meaning it has a utility name and teams reuse it. - Use a plain
@keyframesrule outside@themewhen the animation is private to one component's custom CSS and you always want it emitted. - Use
@theme staticwhen you build class names dynamically, such asanimate-${name}, so Tailwind cannot detect that they are used.
Exercise
You add --animate-shake: shake 0.5s ease-in-out; and a matching @keyframes shake rule inside @theme. The class animate-shake is applied in your HTML, but the element does not move, and the built CSS contains no shake keyframes. Name two things to check.
Show answer
First, confirm the build actually scans the file that contains the class, and that the class name is written out in full rather than assembled from string pieces, because unused theme data is dropped. Second, confirm that the name in --animate-shake (shake) exactly matches the @keyframes name and that the keyframes are written directly inside @theme. If the class is built dynamically, use @theme static so the variable and keyframes are always emitted.
Exercise
Write a @theme block that removes all default animations and defines one called animate-pop that scales an element from 0.9 to 1 over 200ms with an ease-out timing.
Show answer
@theme { --animate-*: initial; --animate-pop: pop 200ms ease-out; @keyframes pop { from { transform: scale(0.9); } to { transform: scale(1); } } }