liveBook · Manning

Animation property

As explained in CSS in Depth, Second Edition

The CSS animation property is a shorthand property for configuring one or more animations on an element.

The CSS animation property is shorthand for applying a named @keyframes animation to an element. It can set the animation name, duration, timing function, and iteration count in one declaration. section 17.1 section 17.7

Animating a box

Define over-and-back with keyframes at 0%, 50%, and 100%. Then apply it with .box { animation: over-and-back 1.5s linear 3; }. A 100px green box changes color and position, moves right, returns to its starting position, and stops after three repetitions. section 17.1

Listing 17.3 Applying the animation to the box
.box {
  width: 100px;               #1
  height: 100px;              #1
  background-color: green;
  animation: over-and-back 1.5s linear 3;      #2
}

Inspect the compact declaration and connect each value to the animation’s name, duration, timing, and repetition behavior. section 17.1

Use the property with these principles:

  • Pair it with a matching @keyframes name; keyframes define the changing states, while animation applies them. section 17.1
  • Leave a property out of an intermediate keyframe when smooth interpolation between surrounding values is desired. section 17.1
  • Use animation-fill-mode when the first or last animation frame should remain applied before or after playback. section 17.7

How CSS in Depth, Second Edition covers it

How deep it goes
Defines four shorthand components and notes fill mode, but does not enumerate every animation subproperty.
Examples
Uses a 100px green box that changes color and position, runs 1.5 seconds, and repeats three times.
What it assumes
Assumes an element and stylesheet, then establishes @keyframes as the animation definition.
What you can do after
Apply a named @keyframes animation with shorthand values for duration, timing, and iteration count.
How it connects
Connects @keyframes, animation-name, animation-duration, animation-timing-function, animation-iteration-count, animation-fill-mode, and JavaScript.

Also explained in

Compare how 2 books explain animation property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.