CSS Scroll-Driven Animations: Scroll Effects Without JavaScript
Progress bars, reveal-on-scroll and parallax with animation-timeline, and none of the scroll listeners
Animations Driven by Scrolling, Not by Time
Reading progress bars, headers that shrink as you scroll, images that fade in as they enter the viewport: these effects used to need a scroll event listener or an IntersectionObserver, plus code to map scroll position to styles. That code runs on the main thread, competes with everything else your page is doing, and is easy to get wrong.
CSS scroll-driven animations replace the time-based clock of a normal CSS animation with a scroll position. You write ordinary @keyframes, then set animation-timeline to scroll() or view(). At 0% scroll the animation is at its first keyframe, at 100% it is at its last, and the browser keeps them in sync as the user scrolls in either direction.
Scroll-driven animations shipped in Chrome and Edge 115 and in Safari 26. Firefox has a complete implementation that spent a long time behind a preference, so plan a fallback (covered below) and check current support before you ship.
This guide assumes you know CSS animations and @keyframes. It explains the two timeline types, how animation-range works, the declaration-order rule that breaks most first attempts, and how to keep these animations fast and accessible.
Two Kinds of Timeline: scroll() and view()
Every scroll-driven animation is attached to a timeline that reports progress from 0% to 100%.
- A scroll progress timeline, created with
scroll(), tracks how far a scroll container has been scrolled, from the top (0%) to the bottom (100%). Use it for effects tied to the whole page or a whole panel, such as a reading progress bar. - A view progress timeline, created with
view(), tracks how far a specific element has travelled through its scroll container's visible area. It starts when the element begins to enter and ends when it has completely left. Use it for effects on individual elements, such as fade-in-on-scroll.
scroll() arguments
scroll() takes two optional arguments, in any order: which scroller to track and which axis.
animation-timeline: scroll(); /* nearest scrolling ancestor, block axis */
animation-timeline: scroll(root); /* the document viewport */
animation-timeline: scroll(self inline); /* the element's own horizontal scrolling */The scroller can be nearest (the default), root or self. The axis can be block (the default), inline, x or y.
view() arguments
view() takes an optional axis and an optional inset that shrinks or grows the area used to measure progress:
animation-timeline: view(); /* block axis, no inset */
animation-timeline: view(block 20% 10%); /* ignore the top 20% and bottom 10% of the scrollport */Positive insets move the edges inward, so the animation starts later and ends earlier. That is useful when a sticky header covers the top of the viewport.
Your First Scroll Animation: A Reading Progress Bar
@keyframes grow-progress {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress-bar {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: #7c3aed;
transform-origin: left;
animation: grow-progress linear both;
animation-timeline: scroll(root);
}Three details matter here.
1. Declare animation-timeline after the animation shorthand
The animation shorthand resets every animation longhand it does not mention, including animation-timeline, back to auto (the normal time-based clock). animation-timeline cannot be set inside the shorthand, so it must come after it. Swap the two lines above and the progress bar simply never moves. This is the single most common reason a first scroll-driven animation "doesn't work".
2. Use linear easing
The default ease timing function makes the bar speed up and slow down relative to the scroll position, which feels wrong. linear keeps the effect locked to the scrollbar.
3. Leave out the duration, or use 1ms
A scroll timeline has no real duration, so the specification uses auto, which is the default when you omit it. MDN recommends animation-duration: 1ms as a common practice, because Firefox's implementation required a non-zero duration and because a 1ms animation is effectively invisible in browsers without scroll-driven support. Either works in Chromium and Safari.
Animating transform: scaleX() rather than width is deliberate: transforms can run on the compositor, off the main thread.
Reveal on Scroll With view() and animation-range
@keyframes reveal {
from { opacity: 0; transform: translateY(2rem); }
to { opacity: 1; transform: translateY(0); }
}
.reveal {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}Without animation-range, a view timeline runs across the element's entire journey, from first pixel entering to last pixel leaving. The card would only be fully visible at the exact moment it left the screen. animation-range picks the slice of that journey to use.
The named ranges
cover: the full journey, from the moment the element starts entering until it has completely left. This is the default.contain: the part where the element is fully inside the scrollport (or, for an element taller than the scrollport, where it fully covers it).entry: from the moment the element starts entering until it is fully inside.exit: from the moment the element starts leaving until it is fully outside.
Each name accepts percentages, so entry 0% entry 100% (shortened to just entry) means "from starting to enter until fully entered". You can also mix names, as in animation-range: entry 25% cover 50%.
Separate entry and exit animations
You can put range names directly in keyframe selectors to fade an element in and out with one animation:
@keyframes in-and-out {
entry 0% { opacity: 0; transform: scale(0.9); }
entry 100% { opacity: 1; transform: scale(1); }
exit 0% { opacity: 1; }
exit 100% { opacity: 0; }
}
.panel {
animation: in-and-out linear both;
animation-timeline: view();
}Ranges on scroll timelines
For a scroll() timeline, animation-range takes lengths or percentages of the scroll distance. This shrinks a header over the first 200px of scrolling and then holds it:
@keyframes shrink-header {
to { padding-block: 0.5rem; box-shadow: 0 2px 12px rgb(0 0 0 / 0.15); }
}
.site-header {
position: sticky;
top: 0;
animation: shrink-header linear both;
animation-timeline: scroll(root);
animation-range: 0 200px;
}Named Timelines and timeline-scope
scroll() and view() are anonymous timelines: they look up the DOM tree for the nearest scroller. When the scroller is not an ancestor of the animated element, give the timeline a name.
.gallery {
overflow-x: auto;
scroll-timeline: --gallery inline; /* name + axis */
}
.gallery-progress {
animation: grow-progress linear both;
animation-timeline: --gallery;
}Named timelines are only visible to the element that declares them and its descendants. If the progress indicator is a sibling of the gallery rather than a child, it will not find --gallery. Fix that by declaring the name on a shared ancestor with timeline-scope:
.gallery-wrapper {
timeline-scope: --gallery; /* hoist the name so siblings can use it */
}The view equivalent is view-timeline (for example view-timeline: --hero block), which lets one element's journey through the viewport drive animations on other elements.
Performance: Why CSS Beats Scroll Listeners
A JavaScript scroll handler runs on the main thread. If the page is busy parsing, running a framework render or handling input, the handler runs late and the effect stutters. Scroll-driven animations are declared up front, so the browser can update them as part of scrolling itself, and animations of compositor-friendly properties can run off the main thread entirely.
To keep that benefit:
- Animate
transformandopacitywhenever you can. Animatingwidth,height,toporbox-shadowforces layout or paint work on every scroll frame. Scale a bar instead of resizing it. - Use
overflow: clipinstead ofoverflow: hiddenon decorative wrappers.overflow: hiddenturns the element into a scroll container, so an anonymousscroll()orview()timeline below it finds the wrong scroller and the animation appears frozen.overflow: clipclips without creating a scroll container. - Don't add
will-changeeverywhere. The browser already knows these animations exist. Add it only after profiling shows a problem.
You can confirm smoothness in Chrome DevTools: record a scroll in the Performance panel and check that frames are not dominated by long "Recalculate Style" or "Layout" tasks. The CSS performance tutorial covers the rendering pipeline in more detail.
Browser Support, @supports and Safe Fallbacks
- Chrome and Edge 115+ (2023)
- Safari 26+ (2025)
- Firefox: implemented, but long gated behind the
layout.css.scroll-driven-animations.enabledpreference. Check Can I use for the current release status.
The golden rule: content must be visible without the animation. A reveal effect that sets opacity: 0 in normal CSS hides content forever in browsers that ignore animation-timeline. Put the whole effect behind a feature query:
@supports (animation-timeline: view()) {
@media (prefers-reduced-motion: no-preference) {
.reveal {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry;
}
}
}Browsers without support simply show the content in its final state, which is the right fallback for decorative motion. If an effect is essential, there is a scroll-timeline polyfill, or you can use an IntersectionObserver to toggle a class in those browsers only.
Respect Reduced Motion
Scroll-linked motion is one of the effects most likely to cause discomfort for people with vestibular disorders, because the movement is tied directly to their own input. Large parallax, zooming and rotating effects carry the most risk. A thin progress bar carries very little.
Two patterns work well:
- Opt in to motion. Wrap the animation in
@media (prefers-reduced-motion: no-preference), as in the example above. People who ask for reduced motion get the static final state. - Switch the timeline off. If the animation is already declared, MDN recommends setting
animation-timeline: noneinside@media (prefers-reduced-motion: reduce), with a selector specific enough to win over the original rule.
Reduced motion does not always have to mean no motion. Replacing a slide-and-scale entrance with a short opacity fade is often a good compromise. See the CSS accessibility tutorial for more on motion preferences.
Controlling Scroll Timelines From JavaScript
The same timelines are available to the Web Animations API through the ScrollTimeline and ViewTimeline constructors. This is handy when keyframes are generated at runtime, or when a framework component needs to start and cancel the effect.
const bar = document.querySelector(".progress-bar");
bar.animate(
{ transform: ["scaleX(0)", "scaleX(1)"] },
{
fill: "both",
timeline: new ScrollTimeline({ source: document.documentElement }),
}
);
// Reveal a card as it enters the viewport
const card = document.querySelector(".card");
card.animate(
{ opacity: [0, 1], transform: ["translateY(2rem)", "none"] },
{
fill: "both",
timeline: new ViewTimeline({ subject: card }),
rangeStart: "entry 0%",
rangeEnd: "entry 100%",
}
);Leave out duration, as in CSS. Feature-detect with "ScrollTimeline" in window before using these constructors.
Troubleshooting Checklist
If a scroll-driven animation does nothing, check these in order:
- Is
animation-timelinedeclared after theanimationshorthand? This causes most failures. - Is there anything to scroll? A
scroll()timeline on a container without overflow has no progress, so the animation stays inactive. - Is an ancestor accidentally a scroll container?
overflow: hiddenoroverflow: autoon a wrapper changes which scrollernearestfinds. Useoverflow: clipfor clipping. - Is a named timeline out of scope? Siblings need
timeline-scopeon a common ancestor. - Does the element snap back after the range? Add
both(oranimation-fill-mode: both) so it keeps the first and last keyframe outside the range. - Is the easing
linear? Non-linear easing makes the effect feel disconnected from scrolling.
Chrome DevTools' Animations panel shows scroll-driven animations and lets you scrub through them, which makes range problems easy to spot.
Key Takeaways
animation-timeline: scroll()tracks a scroller.view()tracks an element's trip through the viewport.- Always declare
animation-timelineafter theanimationshorthand, and uselineareasing. animation-rangewithentry,exit,containandcovercontrols when the effect runs.- Use named timelines plus
timeline-scopewhen the scroller is not an ancestor. - Animate
transformandopacity, and useoverflow: clipon wrappers. - Put effects behind
@supportsandprefers-reduced-motionso content is always visible.
Your next step: find one scroll listener or IntersectionObserver in your project that only exists to drive a visual effect, and replace it with a view timeline.
Frequently Asked Questions
What are CSS scroll-driven animations?
They are normal CSS keyframe animations whose progress is controlled by scrolling instead of time. You set animation-timeline to scroll() to follow a scroll container, or view() to follow an element moving through the viewport.
Why is my scroll-driven animation not working?
The most common cause is declaring animation-timeline before the animation shorthand, which resets it to auto. Other causes are a container with nothing to scroll, an ancestor with overflow: hidden becoming the scroller, or a named timeline that a sibling element cannot see without timeline-scope.
What is the difference between scroll() and view()?
scroll() maps the full scroll range of a container to 0%–100%. view() maps the period during which one specific element is visible in its scroll container. Use scroll() for page-level effects like progress bars and view() for per-element reveals.
Which browsers support scroll-driven animations?
Chrome and Edge from version 115 and Safari from version 26. Firefox has an implementation that was long gated behind a preference, so check caniuse for its current status and wrap effects in @supports (animation-timeline: view()).
Are CSS scroll-driven animations better for performance than JavaScript?
Usually, yes. Scroll listeners run on the main thread and can lag when the page is busy. Scroll-driven animations of compositor-friendly properties such as transform and opacity can run off the main thread, so they stay smooth.
How do I make a scroll animation run only once?
Scroll-driven animations always follow scroll position, so they reverse when the user scrolls back. For a one-time entrance animation, use an IntersectionObserver to add a class that triggers a normal time-based animation instead.
References and Further Reading
- Scroll-driven animation timelines · MDN Web Docs
- Scroll-driven Animations specification · W3C
- Animate elements on scroll with Scroll-driven animations · Chrome for Developers
- A guide to Scroll-driven Animations with just CSS · WebKit
Related Articles
Continue your learning journey with these related topics