Sub topic 11
Defining animations and keyframes in a theme
This chapter assumes you know CSS custom properties, the @keyframes rule, and how to import Tailwind into a CSS file with @import "tailwindcss";. You should also know how @theme namespaces map variable names to utility classes.
The --animate-* namespace
Tailwind v4 reads theme variables in the --animate-* namespace and turns each one into an animate-* utility. The variable value is a full animation shorthand: name, duration, timing function, and so on.
The @keyframes rule that the animation refers to can live inside the same @theme block.
@import "tailwindcss";
@theme {
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}Now animate-fade-in is a utility class:
<div class="animate-fade-in">Hello</div>The utility sets animation: var(--animate-fade-in). Tailwind emits the @keyframes rule next to the theme variables, so the animation name resolves.
How the pieces fit
Why keyframes belong in the theme
You can write @keyframes outside @theme. It still works as plain CSS, but Tailwind then treats it as ordinary output and may not tie it to the theme. Keeping the keyframes inside @theme has two benefits:
- The animation and its keyframes are defined in one place.
- Tailwind includes the keyframes in the output only when the matching utility is used, so unused animations do not add CSS.
Adding a second animation
You can define several animations in one block. Each variable name sets the utility name.
@import "tailwindcss";
@theme {
--animate-wiggle: wiggle 0.6s ease-in-out infinite;
--animate-slide-up: slide-up 0.4s cubic-bezier(0.3, 0, 0, 1) both;
@keyframes wiggle {
0%, 100% {
transform: rotate(-3deg);
}
50% {
transform: rotate(3deg);
}
}
@keyframes slide-up {
from {
transform: translateY(1rem);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
}This gives you animate-wiggle and animate-slide-up. Because these are theme variables, variants work too, for example hover:animate-wiggle or md:animate-slide-up.
Overriding a default animation
Tailwind ships with animations such as animate-spin and animate-pulse. To change one, redefine its variable. The name must match, and your value replaces the default.
@import "tailwindcss";
@theme {
--animate-spin: spin 2s linear infinite;
}To remove all the defaults and keep only your own, reset the namespace first:
@import "tailwindcss";
@theme {
--animate-*: initial;
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}Common errors
- Name mismatch. The first word of the
--animate-*value must equal the@keyframesname. A typo gives an animation that silently does nothing. - Wrong namespace.
--fade-in: ...creates no utility. The variable must start with--animate-. - Missing duration. An
animationshorthand without a duration defaults to0s, so nothing visibly plays.
Exercise
You define --animate-bounce-in: bounce-in 0.5s ease-out; in @theme but the element with class="animate-bounce-in" does not move. The @keyframes rule is named bounceIn. What is wrong, and how do you fix it?
Show answer
The animation name in the variable (bounce-in) does not match the keyframes name (bounceIn), so the browser finds no keyframes. Rename the @keyframes rule to bounce-in, or change the variable to use bounceIn. The two names must be identical.
Exercise
Write a @theme block that removes all default animations and defines only animate-fade-in, which fades from opacity 0 to 1 over 0.3 seconds.
Show answer
@theme { --animate-*: initial; --animate-fade-in: fade-in 0.3s ease-out; @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } } }