Chapter 05 of 11Part 2 · CSS

CSS keyframe animation: @keyframes and the animation property

How @keyframes and the animation property work, from fill mode to loops, and when to choose them over a transition.

10 minBy Zuzo StudioUpdated

11 min

In this chapter you will

  • write an @keyframes rule and attach it with the animation shorthand
  • choose a fill mode, an iteration count and a direction on purpose
  • add a looping scroll cue to your hero that knows when to stop

Before you startThis builds on CSS transitions: hover, focus and entry.

Check your understanding

3 questions

Q1A heading has animation: rise 0.6s ease-out 0.4s, and rise starts at opacity: 0. The heading flashes fully visible, vanishes, then fades in. What fixes it?

Show the answer

Add animation-fill-mode: backwards Backwards applies the first keyframe during the delay, so the heading waits in its starting state instead of showing its normal style for 0.4 seconds.

Q2What does animation: pulse 2s 1s infinite do?

Show the answer

Plays pulse for 2 seconds after a 1 second wait, forever The first time value is the duration and the second is the delay, and infinite is the iteration count.

Q3A button should lift slightly on hover and settle back smoothly when the pointer leaves, even halfway through. Which tool fits best?

Show the answer

A transition on transform A transition is triggered by the state change and reverses from wherever it is when the state flips back, which is exactly what hover feedback needs.

Key takeaways

  1. A keyframe animation is two parts: a named @keyframes rule that lists the states, and an animation property that says how to play them.
  2. In the animation shorthand, the first time value is the duration and the second is the delay.
  3. Leave out the to keyframe and the animation ends on the element's normal styles, which keeps entrances honest and easy to change.
  4. animation-fill-mode: backwards stops the flash before a delayed entrance; forwards holds the last keyframe and can block later style changes.
  5. Use transitions for state changes that should reverse, and keyframes for motion that runs, repeats or has more than two steps.
Up next · Chapter 06 · 10 min Transform, opacity and compositor only animation Why transform and opacity animate smoothly when other properties stutter, and how animation affects responsiveness and loading.

02FAQ

Questions on this chapter

Something else on your mind? Email the studio and a designer, not a salesperson, replies within a day.

What is the difference between a CSS transition and a CSS animation?

A transition animates between two states when something changes, such as a hover or a new class, and reverses when it changes back. A CSS animation uses @keyframes, can have any number of steps, and runs by itself, once, several times or in a loop.

How do I stop a CSS animation from resetting at the end?

Add animation-fill-mode: forwards so the element keeps the values of the last keyframe. A cleaner fix is often to make the final keyframe match the element's normal styles, for example by leaving out the to keyframe, so there is nothing to reset.