Learn CSS Animations

@keyframes name { ... } writes the stages of a movement (from/to or percentages), and the animation property attaches them to an element. The three most…

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;
}

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:

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

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?

🔬 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.

What you will learn