Effects that react to scrolling are everywhere: reading progress bars, headers that shrink, images that fade in as they enter the screen. For years the usual way to build them was a scroll event listener or an Intersection Observer, plus some JavaScript to work out how far along the user was and set a style.
That approach works, but it runs on the main thread. If the page is busy, the animation stutters, and every frame of scrolling costs you JavaScript time that could be spent responding to the user.
CSS scroll-driven animations let you link a normal CSS animation to the scroll position instead of to time. You write @keyframes as usual, then tell the browser that the animation's progress should follow a scroll container or an element's position in the viewport. The browser does the rest, and for properties like transform and opacity it can run the animation off the main thread.
This article is for developers who already know CSS animations and want to replace scroll-linked JavaScript with something simpler and smoother.
The core idea: a different timeline
#Every CSS animation runs against a timeline. By default that is the document timeline, which moves forward with time. A scroll-driven animation swaps in a different timeline, set with the animation-timeline property.
There are two kinds:
- A scroll progress timeline follows the scroll position of a scroll container, from the top (0%) to the bottom (100%). You create one with
scroll(). - A view progress timeline follows an element as it moves through the visible area of its scroll container, from the moment it starts to enter to the moment it fully leaves. You create one with
view().
Because progress now comes from scrolling, the animation's duration no longer means anything. You still need to give the animation a name and keyframes, and it is good practice to set animation-duration: auto or leave the duration out.
A reading progress bar with scroll()
#This is the classic first example. A bar at the top of the page grows from nothing to full width as you scroll from top to bottom.
_10<div class="progress" aria-hidden="true"></div>_10<main>_10 <!-- long article content -->_10</main>
_14.progress {_14 position: fixed;_14 inset: 0 0 auto 0;_14 height: 4px;_14 background: var(--accent);_14 transform-origin: left;_14 animation: grow linear both;_14 animation-timeline: scroll(root block);_14}_14_14@keyframes grow {_14 from { transform: scaleX(0); }_14 to { transform: scaleX(1); }_14}
scroll(root block) means "use the root scroller (the page) along the block axis (vertical in English)". Both arguments are optional:
- The scroller can be
nearest(the default, the closest ancestor that scrolls),rootorself. - The axis can be
block(the default),inline,xory.
The linear timing function matters here. With the default ease, the bar would grow faster in the middle of the page than at the ends, which looks odd for a progress indicator.
Note that animation-timeline must come after the animation shorthand. The shorthand resets animation-timeline to auto, so if you write them the other way round your timeline is silently thrown away. This is the most common reason a first attempt does nothing.
Reveal on scroll with view()
#A view timeline tracks one element's journey through the scrollport. That makes it ideal for fade-ins and slide-ins.
_16.reveal {_16 animation: fade-up linear both;_16 animation-timeline: view();_16 animation-range: entry 0% entry 100%;_16}_16_16@keyframes fade-up {_16 from {_16 opacity: 0;_16 transform: translateY(2rem);_16 }_16 to {_16 opacity: 1;_16 transform: none;_16 }_16}
Each element with the .reveal class gets its own timeline based on its own position, so you do not need a separate observer per element.
Controlling when it runs with animation-range
Without animation-range, a view timeline runs across the whole journey: from the first pixel entering at the bottom to the last pixel leaving at the top. That is usually too long for a reveal. animation-range lets you pick a slice of that journey using named ranges:
entryis the part where the element is entering the scrollport.exitis the part where it is leaving.containis the part where it is fully inside the scrollport (or fully covering it, if it is taller).coveris the full journey, which is the default.
You can combine a named range with a percentage, so entry 0% entry 100% means "start when the element begins to enter and finish when it has fully entered". You can also use lengths, such as entry 20% cover 50%.
You can even use the range names inside your keyframes to build an element that fades in on entry and out on exit in one animation:
_11.card {_11 animation: in-and-out linear both;_11 animation-timeline: view();_11}_11_11@keyframes in-and-out {_11 entry 0% { opacity: 0; scale: 0.9; }_11 entry 100% { opacity: 1; scale: 1; }_11 exit 0% { opacity: 1; scale: 1; }_11 exit 100% { opacity: 0; scale: 0.9; }_11}
Named timelines
#scroll() and view() are anonymous timelines: they find the right scroller based on where the animated element sits in the DOM. Sometimes the element you want to animate is not inside the scroller you care about. For that, you can name a timeline on one element and use it on another.
_10.gallery {_10 overflow-x: auto;_10 scroll-timeline: --gallery inline;_10}_10_10.gallery-indicator {_10 animation: grow linear both;_10 animation-timeline: --gallery;_10}
The name must start with two dashes, like a custom property. scroll-timeline creates a scroll progress timeline and view-timeline creates a view progress timeline.
There is a catch. By default, a named timeline is only visible to the element that defines it and its descendants. If the indicator is a sibling of the gallery rather than a child, it cannot see the name. Fix that with timeline-scope on a shared ancestor:
_10.gallery-wrapper {_10 timeline-scope: --gallery;_10}
A shrinking header
#Here is a more realistic example: a sticky header that gets shorter and gains a shadow over the first 200 pixels of scrolling.
_14.site-header {_14 position: sticky;_14 top: 0;_14 animation: shrink linear both;_14 animation-timeline: scroll(root);_14 animation-range: 0 200px;_14}_14_14@keyframes shrink {_14 to {_14 padding-block: 0.5rem;_14 box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);_14 }_14}
The animation-range: 0 200px stops the effect after 200 pixels, so the header does not keep changing for the length of the page. Because there is no from keyframe, the browser uses the element's normal styles as the start.
Notice that this one animates padding-block and box-shadow. Those cannot run on the compositor, so they still cost main-thread work. It is fine for a single header, but see the performance section before doing this to a hundred elements.
Using it from JavaScript
#The same timelines are available in the Web Animations API, which is useful when the keyframes are dynamic.
_10const bar = document.querySelector('.progress');_10_10bar.animate(_10 { transform: ['scaleX(0)', 'scaleX(1)'] },_10 {_10 fill: 'both',_10 easing: 'linear',_10 timeline: new ScrollTimeline({ source: document.documentElement }),_10 }_10);
There is also a ViewTimeline constructor that takes a subject element. In JavaScript you set the range with rangeStart and rangeEnd in the options object.
Performance
#The main performance win is that the browser knows ahead of time how the animation relates to scrolling. When you animate compositor-friendly properties, mainly transform, opacity and some filter values, the browser can update them on the compositor thread as you scroll. A slow main thread does not make the effect lag behind.
Compare that to a scroll listener, which has to run JavaScript, read the scroll position and write a style on every scroll event. Even with requestAnimationFrame, the effect can only update as fast as the main thread allows, and it adds work during the very moments the user is interacting with the page.
To get the benefit:
- Stick to
transformandopacitywhere you can. Animatingwidth,height,top,paddingorbox-shadowworks but needs layout or paint on the main thread every frame. - Use
scaleandtranslate(ortransform: scaleX()) for growing bars rather than animatingwidth. - Do not add
will-changeto everything. The browser promotes elements as needed for these animations.
Respecting reduced motion
#Scroll-linked motion can make some people feel unwell, and large parallax effects are the worst offenders. Wrap decorative effects in a reduced-motion check:
_10@media (prefers-reduced-motion: no-preference) {_10 .reveal {_10 animation: fade-up linear both;_10 animation-timeline: view();_10 animation-range: entry 0% entry 100%;_10 }_10}
With this pattern, people who ask for reduced motion simply see the content in its final state. Make sure the default (unanimated) styles are the readable ones, so nothing stays invisible if the animation never runs.
Browser support
#Scroll-driven animations shipped in Chrome and Edge 115 in 2023, then in Safari 26 in September 2025. Firefox had the feature behind a preference for a long time and enabled it by default in Firefox 159, which made it Baseline newly available in autumn 2026. That is very recent, so plenty of visitors will still be on browsers without it for a while.
The good news is that it degrades well if you write it as an enhancement. Put the scroll-driven code inside a feature query:
_10@supports (animation-timeline: view()) {_10 .reveal {_10 animation: fade-up linear both;_10 animation-timeline: view();_10 animation-range: entry 0% entry 100%;_10 }_10}
Browsers without support skip the block and show the content normally. If you must have the effect everywhere, there is a polyfill maintained by the Chrome team (scroll-timeline on GitHub), but it runs on the main thread and loses the performance benefit, so only use it when the effect really matters.
When not to use it
#- When the effect should be triggered, not scrubbed. A scroll-driven animation is tied to scroll position, so scrolling back up plays it in reverse and stopping halfway leaves it halfway. If you want "play this once when it appears", an Intersection Observer that adds a class (or the newer scroll-triggered animation proposals, once they ship widely) is a better fit.
- When content depends on it. Never hide important text behind an animation that might not run. The unanimated state must be usable.
- For heavy layout properties on many elements. Animating layout-affecting properties on a long list will cost more than it saves.
- When users have asked for less motion. Large or fast scroll-linked motion should be switched off under
prefers-reduced-motion.
Wrapping up
#animation-timelinelinks a normal CSS animation to scrolling instead of time.- Use
scroll()for effects tied to a container's scroll position andview()for effects tied to an element passing through the viewport. animation-rangepicks the slice of the scroll where the animation runs, andanimation-timelinemust come after theanimationshorthand.- Animate
transformandopacityto keep the work off the main thread, and respectprefers-reduced-motion. - Support became Baseline in 2026 with Firefox 159, so wrap it in
@supportsand treat it as an enhancement for now.
Further reading: MDN: CSS scroll-driven animations
CSSWas this article helpful?

About the author
Danny Engineering
A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.