Scroll and view timelines
Definition and purpose
A scroll-driven animation maps progress to a scroll offset or to the position of a subject within a scrollport, rather than elapsed seconds. Use it for optional progress indicators, not for essential content visibility.
Scroll progress
<!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; } .scroll { height: 100px; overflow: auto; } .space { height: 400px; } #a { height: 20px; } @keyframes progress { from { transform: scaleX(0); } to { transform: scaleX(1); } } #a { transform-origin: left; animation: progress linear both; animation-timeline: scroll(nearest block); } </style> </head> <body> <div class="scroll"> <div id="a">Marker</div> <div class="space"></div> </div> </body></html>Scroll the small container to grow the marker. The default nearest/block arguments serialize as scroll().
Syntax and mental model
scroll(nearest block) tracks the nearest scroller along its block axis; view() tracks the subject crossing its nearest scrollport. A named range such as entry can restrict the attachment interval. Set animation-timeline after the animation shorthand, which resets it. A scroller with no scrollable distance gives no useful scroll progress. Keep a static fallback outside @supports and disable optional motion under prefers-reduced-motion.
View entry range
<!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; } .scroll { height: 100px; overflow: auto; } .space { height: 400px; } #a { height: 20px; } @keyframes progress { from { transform: scaleX(0); } to { transform: scaleX(1); } } #a { animation: progress linear both; animation-timeline: view(); animation-range: entry 0% entry 100%; } </style> </head> <body> <div class="scroll"> <div id="a">Marker</div> <div class="space"></div> </div> </body></html>The entry phase, rather than the whole crossing, drives progress.
Common mistakes
Do not assume entry means the entire passage through the scrollport. Use cover for the full crossing. A time-based fallback may move unexpectedly, so guard the entire animation.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Reduced motion and motion paths, Anchor positioning. Next lesson: Starting styles and discrete transitions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scroll and view timelines — knowledge check
Five questions on scroll and view timelines.
What drives a scroll progress timeline?
What does view() track?
Where should animation-timeline follow?
Which range describes the entire crossing?
What is a safe unsupported-browser fallback?