scroll() CSS Function
The scroll() function creates an anonymous scroll progress timeline, so an animation follows the scroll position of a scroll container.
Description
The scroll() CSS function creates an anonymous scroll progress timeline for scroll-driven animations. It is used as a value of the animation-timeline property and links an animation's progress to the scroll position of a scroll container, instead of to time. At the top of the scroller the animation is at 0%, and when the scroller reaches the end the animation is at 100%. Scrolling back reverses it.
The function takes two optional arguments, in any order. The scroller says which scroll container to track: nearest (the default) uses the closest ancestor that scrolls, root uses the document's viewport, and self uses the element itself. The axis says which scroll direction to track: block (the default), inline, x or y. Because the timeline is anonymous, you do not need to name anything, which makes scroll() the quickest way to build effects like reading progress bars.
/* Reading progress bar fixed to the top of the page */
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: #2563eb;
transform-origin: left;
animation: grow linear;
animation-timeline: scroll(root block);
}
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
Set the animation's duration to auto (the default when only a timeline is used) and use linear timing so the effect follows the scroll position exactly. For effects tied to an element entering the viewport, use view() instead.
Interactive Demo
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Scroll down (and sideways) inside this box → → → → → → →
Syntax
scroll() = scroll( [ <scroller> || <axis> ]? )
<scroller> = root | nearest | self
<axis> = block | inline | x | y
Values
- <scroller>Optional. Which scroll container provides the timeline: nearest (closest scrolling ancestor, the default), root (the document viewport) or self (the element itself).
- <axis>Optional. The scroll direction to track: block (default), inline, x or y.
Example
Browser Support
The following information will show you the current browser support for the CSS scroll() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





