Skip to content
Tutorials Center
My learning

Topics

Lesson 102 of 139 ยท Transforms and Animation

Scroll-driven Animations

Link CSS animations to scrolling with animation-timeline, scroll() and view(). Build progress bars and reveal effects with animation-range and safe fallbacks.

A normal keyframe animation moves forward with time: one second in, it is one second along. A scroll-driven animation moves forward with scrolling instead. Scroll halfway down a container and the animation is at 50%. Scroll back up and it plays backward. Stop scrolling and it stops.

Effects like reading progress bars, elements that fade in as they enter the screen, and headers that shrink as you scroll used to need JavaScript scroll listeners. Now you can write them in a few lines of CSS, and the browser runs them smoothly off the main thread.

This is a newer feature, so this lesson also shows how to use it as a progressive enhancement: browsers that don't support it still get a perfectly usable page.

Support at a glance

Scroll-driven animations shipped in Chrome and Edge 115 (2023) and in Safari 26 (2025). Firefox has an implementation behind a configuration flag but, at the time of writing, does not turn it on by default. Check a current compatibility table before you rely on it, and always write a fallback, as every example here does:

CSS
/* everyone gets the normal, static styles first */
.card { opacity: 1; }

/* only supporting browsers get the scroll effect */
@supports (animation-timeline: view()) {
  .card { animation: fade-in linear both; animation-timeline: view(); }
}

You learn more about this pattern in Feature Queries (@supports).

Timelines

Every animation runs on a timeline. The default is the document timeline, which is simply time passing. The animation-timeline property swaps it for a different one. There are two kinds of scroll-based timelines:

  • A scroll progress timeline, created with scroll(), tracks how far a scroll container has been scrolled, from the top (0%) to the bottom (100%).
  • A view progress timeline, created with view(), tracks one element's journey through the visible area of its scroll container, from the moment it starts to appear until it has completely left.

With either timeline, the keyframes stay the same as before. Only the clock that drives them changes.

scroll(): progress through a container

scroll() takes two optional arguments, in any order:

  • Which scroller: nearest (the default; the closest ancestor that scrolls), root (the whole page) or self (the element itself).
  • Which axis: block (the default; vertical in English), inline, y or x.
CSS
.progress {
  animation: grow-bar linear both;
  animation-timeline: scroll();          /* nearest scroller, block axis */
}

@keyframes grow-bar {
  from { scale: 0 1; }
  to   { scale: 1 1; }
}

Two details matter here:

  • Put animation-timeline after the animation shorthand. The shorthand resets animation-timeline to its default, so if you write it first, the shorthand quietly undoes it.
  • Skip the duration and use linear easing. Time-based durations don't mean much on a scroll timeline; the animation is stretched to fill the scroll range. Linear easing makes the progress match the scroll position exactly.

In the TryIt below, the article is its own scroll container, so scroll() finds it as the nearest scroller. The progress bar is position: sticky inside it, so it stays at the top while you read.

A reading progress bar

Scrolling the whole page instead works the same way: put the bar in a position: fixed element and use animation-timeline: scroll(root).

view(): an element entering and leaving

view() attaches the timeline to the animated element itself. The animation runs from 0% when the element's leading edge first peeks into the scroller, to 100% when its trailing edge has left on the other side. That's ideal for "reveal as it scrolls into view" effects.

view() also accepts an axis, and an inset that shrinks or grows the area considered visible: view(block 20% 0%) behaves as if the top 20% of the scroller were hidden.

animation-range

Usually you don't want the animation to stretch across the element's whole journey. A card should finish fading in soon after it enters, not when it reaches the far edge. animation-range picks the slice of the timeline the animation uses. For view timelines, it understands named ranges:

Range From To
cover (the default) Element starts to enter Element has completely left
contain Element is fully inside Element starts to leave
entry Element starts to enter Element is fully inside
exit Element starts to leave Element has completely left

You can use a name alone (animation-range: entry) or give a start and end with percentages, even mixing ranges: animation-range: entry 10% cover 40%. Like animation-timeline, write animation-range after the animation shorthand.

Reveal cards as they scroll into view

Try changing animation-range to cover and notice how the cards are only fully visible when they reach the middle of the box. Then try entry 0% cover 30%.

Don't hide content that depends on the animation. If a reveal starts from opacity: 0, make sure that style only exists inside the @supports block, as above. Otherwise, users of browsers without support would see empty space where your content should be.

Horizontal scrolling and the inline axis

Both functions work sideways too. In a horizontal carousel, view(inline) tracks each slide as it crosses the container from right to left. A keyframe at 50% is reached when a slide is centered, which makes a "cover flow" effect easy: small at the edges, full size in the middle.

A scaling carousel with view(inline)

The strip also uses scroll snap so a tile always settles in the center. This demo takes the opposite approach to reduced motion: the animation only exists inside @media (prefers-reduced-motion: no-preference), so it is opt-in. The console line shows how to feature-detect from JavaScript with CSS.supports().

Named timelines

scroll() and view() are anonymous timelines: they find their scroller automatically. Sometimes the element you want to animate isn't inside the scroller, or you want several elements to share one timeline. Then you name it with a dashed identifier:

CSS
.gallery {
  overflow-x: auto;
  scroll-timeline: --gallery x;   /* name and axis */
}

.gallery .counter {
  animation: count linear both;
  animation-timeline: --gallery;
}

view-timeline: --name does the same for view progress, set on the element being tracked. By default a named timeline is only visible to the scroller's descendants. To let a sibling or a distant element use it, declare timeline-scope: --gallery on a shared ancestor.

Good uses and pitfalls

Scroll-linked motion is easy to overdo. Some guidelines:

  • Prefer subtle effects. Small fades and short movements guide the eye. Big parallax layers, spinning and zooming can make people dizzy, so they belong behind a prefers-reduced-motion: no-preference check, if anywhere.
  • Animate cheap properties. Stick to opacity, transform and the individual translate, scale and rotate properties. They can run on the compositor, which keeps scrolling smooth.
  • Never hijack scrolling. The user must stay in control of scroll speed and direction. Scroll-driven animations follow scrolling; they don't change it.
  • Remember the fallback. Every effect should be an enhancement on top of a page that already works.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What drives the progress of an animation with animation-timeline: scroll()?

    A scroll progress timeline maps the scroll position of a container (0% at the start, 100% at the end) to the animation's progress.
  2. 2. Why should animation-timeline be written after the animation shorthand?

    Shorthands reset all their longhands. Writing animation after animation-timeline sets the timeline back to auto, the time-based default.
  3. 3. Which timeline is best for fading in a card as it enters the visible area?

    A view progress timeline tracks the element's own visibility within its scroller, which is exactly what a reveal effect needs.
  4. 4. With a view timeline, what does animation-range: entry cover?

    The entry range runs from the element's first appearance at the edge until it is completely inside the scroll container's visible area.
  5. 5. How should you protect users whose browser lacks support for scroll-driven animations?

    Progressive enhancement means the page works without the feature. Only browsers that pass the @supports test get the animated (and initially hidden) styles.

Summary

  • Scroll-driven animations replace time with scroll position via animation-timeline.
  • scroll() tracks a scroller's progress (nearest, root or self; block, inline, x or y).
  • view() tracks an element crossing its scroller; animation-range picks a slice such as entry, exit, contain or cover.
  • Declare animation-timeline and animation-range after the animation shorthand, and use linear easing.
  • Named timelines (scroll-timeline, view-timeline, timeline-scope) let other elements share a timeline.
  • Support is in Chromium and Safari 26, but not yet on by default in Firefox, so wrap effects in @supports with a visible fallback and respect reduced motion.

Next: View Transitions โ€” animate between two states of a page, or even between pages.