A transition can only change an element from state A to state B, and it needs a trigger such as hover. An animation can run by itself, have many stages, and repeat forever, like a box that keeps moving up and down. It takes two steps: write the stages of the movement in @keyframes, then attach those stages to an element with the animation property.
Write the stages, then attach them
This example makes a box move up and down forever:
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-40px); }
}
.box {
animation: bounce 1s infinite;
}
@keyframes bounce { ... }creates an animation namedbounce. You choose the name.0%,50%and100%are the stages. The percentages are measured along one cycle, which is one run of the animation from start to end.0%is the start, and100%is the end.0%, 100%means those two stages have the same style: the box is at its starting position.50% { transform: translateY(-40px); }means: halfway through, the box is 40px higher.animation: bounce 1s infiniteon.boxattaches that animation to the box. It says: the namebounce, one cycle of 1 second, repeated forever (infinite).
The result: the box goes up and comes back down every second, again and again. The browser fills in the movement between the stages by itself.
from and to for two stages
When there are only two stages, a start and an end, you can write from and to:
@keyframes appear {
from { opacity: 0; }
to { opacity: 1; }
}
from is the same as 0%, and to is the same as 100%. opacity sets how visible an element is: 1 is fully visible, and 0 is not visible at all. So the appear animation makes the element slowly become visible.
The animation properties one by one
animation is a shorthand. animation: slide 2s infinite is the same as these three lines:
.box {
animation-name: slide;
animation-duration: 2s;
animation-iteration-count: infinite;
}
Here is what those three properties do, and two more that are often used:
animation-name: the name of the@keyframesto use (hereslide). It must be spelled exactly the same.animation-duration: how long one cycle takes, with the units(seconds) orms(milliseconds).animation-iteration-count: the number of cycles. It is a number (such as3for three cycles), orinfiniteto repeat forever.animation-timing-function: how the animation speeds up and slows down, the same as in a transition (ease,linear).animation-direction: alternate: the second cycle, the fourth and so on run backward, so the movement goes back and forth.
After the animation ends
An animation that does not repeat forever will finish. When it does, the styles from @keyframes are removed, and the element goes back to its own styles. So the element seems to jump back to its starting position. Add animation-fill-mode: forwards to keep the element at the last stage.
⚠️ Animations that won't start, or won't stop
- Names that don't match.
@keyframes slidewithanimation-name: sldedoes not run at all, because the two names are different. - Forgetting the duration. The default is
0s, so the animation is over before you can see it. - A duration without a unit.
animation-duration: 2is not valid. Write2s. - Endless animations everywhere. Movement that never stops tires the eyes. Use it only where it helps, and respect people who turn on the reduce motion setting on their device. CSS reads that setting through
prefers-reduced-motion.
This rule turns off the bounce animation on .box for people who turn on the reduce motion setting. @media makes the rules inside it apply only in certain conditions (covered in the media queries lesson):
@media (prefers-reduced-motion: reduce) {
.box { animation: none; }
}
🎯 Transition or animation?
- One change from A to B, caused by hover, a click or focus: use
transition. - Several stages, such as going up and then coming back down: use
animationwith@keyframes. - Runs forever with no trigger, such as a spinning loading icon: use
animationwithinfinite.
🔬 Try it yourself
Pick the keyframes, the duration and the number of cycles, and watch the box move.
TL;DR: @keyframes name { ... } writes the stages of a movement (from/to or percentages), and the animation property attaches them to an element. The three most important properties: animation-name (spelled exactly like the keyframes' name), animation-duration (the length of one cycle, which is one run of the animation, with s) and animation-iteration-count (the number of cycles: a number, or infinite to repeat forever). The shorthand for all three is animation: slide 2s infinite. When the animation ends, the element goes back to its own styles, unless it has animation-fill-mode: forwards.
⚡ Write the stages, then attach them
Write:
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-40px); }
}
.box {
animation: bounce 1s infinite;
}
Meaning: @keyframes bounce writes the stages of the movement. The percentages are measured along one cycle, which is one run of the animation from start to end. At 0% (the start) and 100% (the end), the box is at its starting position. At 50%, translateY(-40px) moves the box 40px up. animation: bounce 1s infinite attaches that movement to .box: one cycle of 1 second, repeated forever (infinite).
⚡ Name, duration, repeats
Write:
.box {
animation-name: slide;
animation-duration: 2s;
animation-iteration-count: infinite;
}
Guess first: if animation-duration is left out, what do you see?
No movement. The default duration is 0s, so the animation is over before you can see it.
Meaning: animation-name is the name of the @keyframes to use (here slide), and it must be spelled exactly the same. animation-duration is how long one cycle takes, with the unit s (seconds) or ms (milliseconds). animation-iteration-count: infinite repeats the animation forever. The shorthand for all three is animation: slide 2s infinite.
⚡ After the animation ends
Guess first: an animation that doesn't repeat has finished. Does the element stay at the last stage?
No. The styles from @keyframes are removed, so the element jumps back to its own styles.
Meaning: Add animation-fill-mode: forwards to keep the element at the last stage.