liveBook · Manning

Keyframes at-rule

As explained in Tiny CSS Projects

A CSS keyframe is a point in an animation sequence that specifies styles at a particular progress position.

A loading indicator can show that an action is still underway. With CSS, the @keyframes at-rule defines the important style changes in an animation; the browser fills in the changes between them. A loader can use this to make SVG rectangles shrink and grow while nearby text tells people what is happening. section 3.0 section 3.4.1

Write @keyframes followed by an animation name and a block of steps. Each step gives a progress position and the styles to apply there: 0% is the start, 50% is halfway, and 100% is the end. The keywords from and to can mark the start and end instead. Set animation-name on an element to connect it to the named keyframes. section 3.4.1

Make a rectangle squeeze and expand

@keyframes doScale { from { transform: scaleY(1); } 50% { transform: scaleY(0.2); } to { transform: scaleY(1); } } rect { animation-name: doScale; } The rectangle begins at full height, shrinks halfway through, then returns to full height. Set animation-duration to control how long one run takes, animation-iteration-count to control how many times it runs, and different animation-delay values on the rectangles to stagger their movement. section 3.4.1 section 3.4

Listing 3.6 Example animation
@keyframes changeColor {                                #1
  0% { background: blue }                               #1
  50% { background: yellow }                            #1
  100% { background: red }                              #1
}                                                       #1
@keyframes changeBorderRadius {                         #2
  from { border-radius: 0 }                             #2
  50% { border-radius: 50% }                            #2
  to { border-radius: 0 }                               #2
}                                                       #2
div {
  animation-name: changeColor, changeBorderRadius;      #3
  animation-duration: 3s;                               #4
  animation-iteration-count: 10;                        #5
}

Compare two named animations: one changes background color at 0%, 50%, and 100%; the other changes border radius using from, 50%, and to. A comma-separated animation-name value applies both. section 3.4.1

Keyframes define the changing styles, while animation properties control when and how often those changes run. For motion that respects user preferences, animations can also be styled conditionally with a prefers-reduced-motion media query. section 3.4 section 3.7

How Tiny CSS Projects covers it

How deep it goes
Explains named @keyframes syntax, percentage steps, browser in-betweening, and animation-name, then works through color, shape, and scaling examples.
Examples
Shows background-color and border-radius animations on a div, then vertical scaling that squeezes and expands an SVG rectangle.
What it assumes
Introduces at-rules and keyframe syntax before applying them to SVG rectangles; assumes no prior animation terminology.
What you can do after
Build it from scratch with CSS by defining named keyframes, applying them to SVG rectangles, and staggering their start times.
How it connects
Links @keyframes to CSS animations, animation-name, animation-duration, animation-iteration-count, animation-delay, SVG, and prefers-reduced-motion.

Also explained in

Compare how 3 books explain keyframes at-rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.