Keyframes and animation controls
Definition and purpose
CSS animations define named keyframe sequences that can run without a state change. Keyframes describe sampled values along a timeline; the browser interpolates between them according to timing and animation rules.
A paused halfway sample
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @keyframes fade { from { opacity: 0.2; } to { opacity: 1; } } #a { animation: fade 2s linear -1s paused; } </style> </head> <body> <div id="a">Status</div> </body></html>The negative one-second delay samples halfway through the two-second animation.
Syntax and mental model
@keyframes names the sequence; animation-name, duration, delay, iteration-count, direction, fill-mode and play-state configure its lifecycle. Fill forwards retains the final relevant keyframe after the active interval, without rewriting the base declaration. Paused state freezes timeline progress. A negative delay lets us inspect a deterministic interior sample.
Explicit lifecycle
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } @keyframes pulse { from { opacity: 0.5; } to { opacity: 1; } } #a { animation: pulse 1s linear 2 alternate both paused; } </style> </head> <body> <div id="a">Status</div> </body></html>The second iteration reverses the direction; both fill modes apply outside the active interval.
Common mistakes
Infinite decorative motion can distract and may require a pause or stop control. Do not depend on an animation to communicate essential status.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Transitions and timing. Next lesson: Multiple animations and interpolation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Keyframes and animation controls — knowledge check
Five questions on keyframes and animation controls.
What names a CSS keyframe sequence?
What does animation-play-state: paused do?
What does alternate do?
Does forwards fill rewrite the base CSS?
What is midpoint of a 4s linear animation with -2s delay at pause?