Favorites

scroll() CSS Function

scroll()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The scroll() function creates an anonymous scroll progress timeline, so an animation follows the scroll position of a scroll container.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

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 → → → → → → →

Demo Code
HTML Code
<div class="demo-scroller">
	<div class="demo-element"></div>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	<p>Scroll down (and sideways) inside this box &rarr; &rarr; &rarr; &rarr; &rarr; &rarr; &rarr;</p>
	</div>
CSS Code
@keyframes demo-grow {
	from { transform: scaleX(0); }
	to { transform: scaleX(1); }
}
.demo-scroller {
	height: 160px;
	width: 270px;
	overflow: auto;
	border: 1px solid #c5c5c5;
	background: #fafafa;
}
.demo-element {
	position: sticky;
	top: 0;
	left: 0;
	width: 270px;
	height: 12px;
	background: #f72585;
	transform-origin: left;
	animation: demo-grow linear both;
}
.demo-scroller p {
	margin: 0;
	padding: 8px 12px;
	white-space: nowrap;
}
.demo-element {
	animation-timeline: scroll();
}

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

<div class="wrap">
<h1>CSS scroll() examples</h1>
<div class="scroller">
<div class="progress"></div>
<div class="content">
<p>Scroll inside this box. The bar at the top fills up as you go.</p>
<p>The bar uses animation-timeline: scroll(nearest block), so its animation progress follows the scroll position of this box rather than time.</p>
<p>Scroll-driven animations run off the main thread in supporting browsers, which keeps them smooth.</p>
<p>Keep scrolling to reach the end of the content.</p>
<p>Almost there.</p>
<p>At the bottom of the box, the bar is completely full.</p>
</div>
</div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* scroll() examples */
.scroller {
height: 220px;
overflow-y: auto;
border-radius: 12px;
background: #fff;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.progress {
position: sticky;
top: 0;
height: 6px;
background: linear-gradient(90deg, #7c3aed, #06b6d4);
transform-origin: left center;
animation: grow linear both;
/* track the nearest scrolling ancestor, which is .scroller */
animation-timeline: scroll(nearest block);
}

.content {
padding: 16px;
line-height: 1.7;
}

@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
Open in editor

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.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.