CSS Keyframes — Web Animation

@keyframes name { 0% { ... } 50% { ... } 100% { ... } } writes the script of an animation: a list of keyframes, the element's state at each point in time…

The transition in the previous lesson needs a trigger: the value has to change first, for example through a new class or :hover. A transition also only knows two states, start and end. A toast that rises by itself as soon as it appears, or an arrow that bounces three times, needs another tool: @keyframes. @keyframes is the script of an animation. It holds a list of keyframes (the element's state at one point in time), from 0% (the start) to 100% (the end). The animation property plays that script on one element. The animation runs by itself as soon as its CSS rule applies, and may have many keyframes.

It works like a dance choreography. The moves are written once with counts ("on count 1 the arms go up, on count 4 they come down"), then each dancer performs it with their own tempo, number of repeats and direction. @keyframes is the choreography, and the percentages inside it are the counts. The element is the dancer: through animation, each element picks its own duration, number of repeats and direction.

Writing @keyframes and animation

Here is the script for the toast "Booking sent", which rises from the bottom of the screen:

@keyframes toast-in {
  from { opacity: 0; transform: translateY(120%); } /* same as 0% */
  to   { opacity: 1; transform: none; }             /* same as 100% */
}
.toast.show {
  animation: toast-in 200ms ease-out forwards;
}
Longhand On the toast Default
animation-name toast-in none
animation-duration 200ms 0s
animation-timing-function ease-out ease
animation-delay 0s 0s
animation-iteration-count 1 (a number, or infinite) 1
animation-direction normal normal
animation-fill-mode forwards none
animation-play-state running running

In the exercise, you build @keyframes and an animation value as one-line strings. There, each keyframe is called a step. Each step becomes percent% { property: value; }. All the steps are joined with spaces, then wrapped in @keyframes name { ... }:

Input Result
fadeIn, step 0 { opacity: 0 } and 100 { opacity: 1 } @keyframes fadeIn { 0% { opacity: 0; } 100% { opacity: 1; } }
pop, step 0 { transform: "scale(1)", opacity: 0.5 } @keyframes pop { 0% { transform: scale(1); opacity: 0.5; } }
fadeIn, 0.5s, ease-out, 1 fadeIn 0.5s ease-out 1
spin, 1s, linear, infinite spin 1s linear infinite

One step may hold several properties. Endless repetition is written with the word infinite, not a number.

Points in the middle: 0%, 50%, 100%

A script may have keyframes at any point in time, not only at the start and the end. An example is the small "See more photos" arrow in the Terrace Room gallery. It bounces three times, then rests:

@keyframes bounce {
  0%, 100% { transform: translateY(0); }   /* start and end in place */
  50%      { transform: translateY(-30px); } /* middle: 30 px up */
}
.hint { animation: bounce 600ms ease-in-out 3; }

fill-mode: what stays after it ends

Before reading on, take a guess. The base style of the toast "Booking sent" (its style without the animation) is opacity: 0, so the toast is hidden. When it appears, the toast gets animation: toast-in 200ms ease-out, and toast-in ends at opacity: 1. After 200 ms, what is its opacity?

An animation only owns the element's values while the animation runs. Before it starts and after it ends, the element goes back to its base style, unless animation-fill-mode says otherwise:

fill-mode During the delay After it ends
none base style base style
forwards base style the last keyframe's values
backwards the first keyframe's values base style
both the first keyframe's values the last keyframe's values

The toast's base style is opacity: 0. Without forwards, the toast rises and is visible for 200 ms, then jumps straight back to opacity: 0: gone again. So its opacity is 0, not 1. We tested it in Chromium 147, WebKit 26.4 and Firefox 146: with none the opacity ended at 0, with forwards at 1. Another example: @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } with animation: fadeIn 2s forwards. After it ends, the opacity is 1, the value in to.

The whole toast: in, wait, out

The toast on Mrs. Sri's page comes in over 200 ms, shows for 5 seconds with a countdown bar, then leaves over 150 ms. The bar stops while the mouse pointer is over the toast, so people have time to read it. Here is the CSS, where .bar is the countdown bar:

.toast { opacity: 0; transform: translateY(120%); }        /* base: hidden below */
.toast.show { animation: toast-in 200ms ease-out forwards; }
.toast.hide { animation: toast-out 150ms ease-in forwards; }
.toast.show .bar, .toast.hide .bar {                       /* same name: the bar doesn't restart on the way out */
  transform-origin: left;
  animation: countdown 5s linear forwards;
}
.toast:hover .bar { animation-play-state: paused; }       /* the bar stops while it is read */

@keyframes toast-in  { to { opacity: 1; transform: none; } }
@keyframes toast-out { from { opacity: 1; transform: none; } to { opacity: 0; transform: translateY(120%); } }
@keyframes countdown { to { transform: scaleX(0); } }
@keyframes fade-in   { to { opacity: 1; } }
@keyframes fade-out  { from { opacity: 1; } to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .toast { transform: none; }
  .toast.show { animation: fade-in 150ms linear forwards; }
  .toast.hide { animation: fade-out 150ms linear forwards; }
}

Here toast-in has only a to keyframe, so it starts from the base style of .toast: hidden below. The bar keeps the same animation name under show and hide, so it doesn't restart when the class changes.

The JavaScript below waits for the event animationend, which arrives when an animation finishes:

toast.addEventListener('animationend', (e) => {
  if (e.animationName === 'countdown') toast.className = 'toast hide'; // 5 seconds are up
  if (e.animationName === 'toast-out') toast.hidden = true;            // it has gone down
});

What happens, in order:

Time What happens Event
0 ms class show: the toast rises, the bar starts shrinking animationstart toast-in, animationstart countdown
200 ms the toast arrives, stays visible thanks to forwards animationend toast-in
5,000 ms the bar runs out animationend countdown, the class becomes hide
5,150 ms the toast has gone down animationend toast-out, hidden = true

Repeats, direction and events

⚠️ Why doesn't the animation follow its script?

TL;DR: @keyframes name { 0% { ... } 50% { ... } 100% { ... } } writes the script of an animation: a list of keyframes, the element's state at each point in time (from = 0%, to = 100%). One keyframe may hold many properties, and 0%, 100% may share one block. animation: name duration easing repeats plays that script on an element (fadeIn 0.5s ease-out 1, spin 1s linear infinite). The first time value is the duration, the second the delay. Easing applies per segment between two keyframes. fill-mode sets the values before and after the animation: none goes back to the base style (the style without the animation), forwards holds the last keyframe (fadeIn 2s forwards ends at opacity 1), backwards uses the first keyframe during the delay, both does both. play-state: paused stops the animation's time. Events: animationstart, animationiteration between two rounds, animationend (never for infinite, endless repeats), and animationcancel when the element becomes display: none. To replay: remove the class that sets the animation, force a reflow (the browser calculates layout again), then add the class again.

⚡ The script and its player

Write:

@keyframes toast-in {
  from { opacity: 0; transform: translateY(120%); } /* same as 0% */
  to   { opacity: 1; transform: none; }             /* same as 100% */
}
.toast.show {
  animation: toast-in 200ms ease-out forwards;
}

Meaning: @keyframes toast-in is the script of the toast's animation: a list of keyframes (the element's state at given points in time) from 0% to 100%. animation plays that script on the toast that has the class show. The animation runs by itself as soon as that rule applies. The first time value is the duration, the second the delay.

⚡ Points in the middle

Write:

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-30px); }
}
.hint { animation: bounce 600ms ease-in-out 3; }

Meaning: The hint arrow (.hint) rises 30 px in the middle of the animation (50%), then comes back down. 0%, 100% share one block, so the arrow starts and ends in the same place. The easing applies to each segment between two keyframes, and 3 means the script plays three times, then stops.

⚡ After it ends

Guess first: a toast with a base style (its style without the animation) of opacity: 0 plays toast-in 200ms ease-out, which ends at opacity: 1. After 200 ms, what is its opacity?

0, not 1. An animation only owns the element's values while the animation runs. After that the toast goes back to its base style, so the toast is gone again.

Meaning: animation-fill-mode: forwards holds the last keyframe's values after the animation ends.

⚡ Replaying it

Guess first: the toast already has the class show. If show is added again, does the animation play again?

No: the class is already there, so nothing changes. Remove the class, make the browser register that change (for example by reading el.offsetWidth, the width of the element el), then add the class again.

Meaning: An infinite animation never sends animationend, the event for a finished animation. So don't wait for that event. To stop the animation, remove it.

What you will learn