Scroll-driven CSS animations

By

Tagged: CSS

Animate elements from scroll position with animation-timeline scroll() and view(). No JavaScript needed. Check current browser support before shipping.

Scroll-driven animations tie an animation’s progress directly to a scroll position.

Instead of telling CSS to run an animation for 500 milliseconds, you tell it to run from the top to the bottom of a scroller, or as an element moves into the viewport. The browser handles the updates while the user scrolls, so you don’t need scroll event listeners or JavaScript math running on every frame.

This builds straight on top of regular CSS animations. Your keyframes stay the same, and you swap out the usual time-based timeline for a scroll-based one.

If you want a refresher on keyframes, transforms, motion preferences, and responsive layout first, check out the free CSS course.

Scroll-Driven Animation Generator

Generate CSS view and scroll timeline animations with live reveal, progress, and parallax demos.

Scroll animationsInteractive · runs in your browser

This browser supports scroll-driven CSS animations — the live demo uses native timelines.

Scroll-driven CSS is limited here — output still includes @supports fallbacks and optional JS.

Preset

Controls

Live scroll demo

Fade up on scroll
Slide in on scroll
Parallax layer

Scroll inside the box to preview the effect.

CSS output

Fallback JS (optional)

Scroll-driven animations tie keyframes to scroll position instead of time. Support is growing in Chromium and Safari — always ship a fallback for Firefox and older browsers.

Two kinds of scroll timeline

CSS gives you two timeline functions to work with:

  • scroll() tracks the scroll position of a container
  • view() tracks an element as it travels through a scrollport

Reach for scroll() when the effect belongs to a whole page or container, like a reading progress bar or a horizontal carousel. Reach for view() when you care about a specific element showing up on screen, like a card fading in as it enters the viewport, an image transforming while it’s visible, or a section indicator.

Keep in mind that these are scroll-driven animations, which aren’t the same as scroll-triggered animations. A scroll-triggered animation starts once you hit a certain scroll point and then plays out on a timer, whereas a scroll-driven animation scrubs forward and backward in lockstep with scrolling.

Build a reading progress bar

Let’s build the classic example first. Drop an element near the top of your page:

<div class="reading-progress" aria-hidden="true"></div>

Pin it to the top of the viewport with CSS:

.reading-progress {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: royalblue;
  transform: scaleX(0);
  transform-origin: left;
}

Next, define the keyframes for the animation:

@keyframes grow-progress {
  from {
    transform: scaleX(0);
  }

  to {
    transform: scaleX(1);
  }
}

Then hook that animation up to the root scroller:

.reading-progress {
  animation: grow-progress 1ms linear;
  animation-timeline: scroll(root block);
}

Here root points to the main document scroller, and block picks its block axis (vertical on a standard left-to-right page). At the top of the page, the timeline sits at 0%. By the time you scroll to the bottom, it’s at 100%, and the bar scales right along with it.

Don’t let that 1ms duration throw you off. It won’t make the bar finish in one millisecond, because scroll position drives the progress now. Setting a tiny non-zero duration keeps things working in Firefox and acts as a harmless fallback in browsers that don’t support scroll timelines yet.

One detail that trips people up: animation-timeline has to come after the animation shorthand. The shorthand resets animation-timeline back to auto, so if you flip those two lines, you’ll end up back on a normal time-based animation.

It’s also much better to animate transform here than to update width on every frame, since the browser can usually handle transforms without recalculating page layout.

Give it a try below. Scroll inside the frame and watch the blue bar grow:

Use the nearest scroll container

You’re not limited to the whole document. scroll() can track a smaller scrolling container too. Start with a scrollable panel and a bar inside it:

<section class="activity-panel">
  <div class="panel-progress"></div>
  <div class="activity-list">...</div>
</section>

Give the panel overflow so it scrolls:

.activity-panel {
  position: relative;
  max-height: 20rem;
  overflow-y: auto;
}

Now attach the animation to the nearest scrolling ancestor:

.panel-progress {
  position: sticky;
  top: 0;
  height: 3px;
  background: seagreen;
  transform-origin: left;
  animation: grow-progress 1ms linear;
  animation-timeline: scroll(nearest block);
}

Using nearest tells the browser to look up the tree for the closest ancestor that scrolls. That way, the component stays self-contained and doesn’t care about the rest of the page.

Reveal an element with view()

A view timeline follows a single element as it passes through its nearest scrollport.

Start by defining a small reveal animation:

@keyframes reveal-card {
  from {
    opacity: 0;
    transform: translateY(2rem);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

Then attach each card to its own view timeline:

.card {
  animation: reveal-card 1ms linear both;
  animation-timeline: view(block);
  animation-range: entry 0% entry 100%;
}

view(block) watches the card along the block axis. By default, that timeline begins as the card starts entering and doesn’t finish until the card leaves on the other side.

Use animation-range to narrow that down. Setting it to entry runs the animation from the moment the card first intersects the scrollport until it’s completely inside.

Scroll inside the frame below to see each card reveal as it enters:

Understand the useful ranges

A view timeline breaks an element’s trip into named ranges. The four you’ll use most are:

  • entry, while the element is entering the scrollport
  • contain, while the element fits completely inside
  • exit, while the element is leaving
  • cover, across the entire time any part of the element is visible

You can also target those ranges right inside @keyframes if you want an element to reveal on the way in and fade back out on the way out:

@keyframes appear-and-leave {
  entry 0% {
    opacity: 0;
    transform: scale(0.9);
  }

  entry 100%, exit 0% {
    opacity: 1;
    transform: scale(1);
  }

  exit 100% {
    opacity: 0;
    transform: scale(0.9);
  }
}

.photo {
  animation: appear-and-leave 1ms linear both;
  animation-timeline: view();
}

Named range keyframes let you choreograph the element’s whole journey in one place. Still, when you’re building an effect for the first time, sticking to a single animation-range property is much easier to debug.

Create a named timeline

Anonymous scroll() and view() timelines handle most cases fine. You’ll want a named timeline when the element you’re animating isn’t inside the scrolling container.

Say you have a horizontal gallery and a progress indicator sitting next to each other as siblings:

<div class="gallery-shell">
  <div class="gallery">...</div>
  <div class="gallery-indicator"></div>
</div>

First, give the gallery container a named scroll timeline:

.gallery {
  overflow-x: auto;
  scroll-timeline-name: --gallery-scroll;
  scroll-timeline-axis: inline;
}

By default, only descendants of the scroller can see that timeline. Because the indicator is a sibling instead of a child, lift the timeline’s scope up on their shared parent:

.gallery-shell {
  timeline-scope: --gallery-scroll;
}

Now the indicator can point to that named timeline:

.gallery-indicator {
  animation: grow-progress 1ms linear;
  animation-timeline: --gallery-scroll;
}

Custom timeline names always start with --, just like CSS custom properties.

If your animated element already lives inside the scroller, you won’t need timeline-scope. Start with an anonymous scroll() timeline and only reach for a named one when your DOM structure requires it.

Drag the gallery sideways and watch the green indicator track its scroll position:

Choose the correct axis

Scroll timelines accept either logical or physical axes. The logical ones adapt to the page’s writing mode:

  • block follows the direction blocks stack (vertical in most layouts)
  • inline follows the direction text flows (usually horizontal)

The physical ones stay fixed regardless of writing mode:

  • y always tracks vertical scrolling
  • x always tracks horizontal scrolling

For a standard article progress bar, use block:

animation-timeline: scroll(root block);

For a horizontal gallery, use inline:

animation-timeline: scroll(nearest inline);

I prefer logical axes for components because they keep working if the document’s writing mode changes.

Adjust when a view animation starts

Sometimes an animation feels too early if it kicks in the exact pixel an element touches the viewport edge. You can shrink the effective scrollport by passing inset values to view():

.card {
  animation: reveal-card 1ms linear both;
  animation-timeline: view(block 15% 15%);
}

Those two 15% values pull the start and end edges inward by 15%, so the animation waits until the card crosses that inner line and wraps up before it hits the opposite edge.

Another option is to leave the default view timeline alone and narrow it with animation-range:

.card {
  animation: reveal-card 1ms linear both;
  animation-timeline: view();
  animation-range: entry 20% cover 40%;
}

That starts the animation partway through entry and finishes it early in the cover range.

Pick one of these at first. Mixing insets, named ranges, and range keyframes right away makes it a lot harder to figure out why an animation triggers at the wrong spot.

Progressive enhancement

Your page still needs to be readable in browsers that don’t support scroll-driven animations.

Start by styling the normal, visible state:

.card {
  opacity: 1;
  transform: none;
}

Then layer the scroll-driven animation inside an @supports block:

@supports (animation-timeline: view()) {
  .card {
    animation: reveal-card 1ms linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 100%;
  }
}

Browsers without support skip that whole block and show the cards as-is. Make sure opacity: 0 stays inside @supports, otherwise unsupported browsers will leave your content invisible forever.

Debug the timeline before the effect

If an animation sits completely still, strip it back to two unmistakable states so you can tell right away if the timeline is firing:

@keyframes debug-progress {
  from {
    background: red;
    transform: scale(0.5);
  }

  to {
    background: lime;
    transform: scale(1);
  }
}

Wire that test animation up to the same timeline. If nothing changes when you scroll, you know the issue is in your timeline setup rather than your keyframes.

Run through this quick checklist:

  1. Does the target element overflow and scroll?
  2. Did you put animation-timeline after the animation shorthand?
  3. Are you watching the right axis?
  4. Is the named timeline in scope for the element you’re animating?
  5. Does the view() element travel far enough through the scrollport to trigger the range you chose?

Open your browser’s DevTools to inspect the CSS animations and look at the computed styles. In particular, check that animation-timeline resolved to your scroll or view timeline instead of falling back to auto.

Respect reduced motion

Even though the user controls the scroll position, scroll-linked motion can still feel uncomfortable for people sensitive to movement.

Turn it off when reduced motion is enabled:

@media (prefers-reduced-motion: reduce) {
  .reading-progress,
  .card,
  .photo {
    animation: none;
  }
}

A subtle reading progress bar that only scales along one edge can still be fine in some designs, but big parallax shifts and reveal animations should go away completely.

Common mistakes

The most common gotcha is writing the animation shorthand after animation-timeline:

.card {
  animation-timeline: view();
  animation: reveal-card 1ms linear both;
}

Because animation resets the timeline back to auto, always put animation-timeline last.

A few other things will quietly break a scroll-driven animation:

  • Pointing at a container that doesn’t scroll. scroll(nearest) can match a scroll container whose content has no overflow, so the timeline never progresses.
  • Using view() on an element that’s taller than the scrollport or barely moves through it. Ranges like contain get tiny or never happen at all if the element can’t fit completely inside the viewport.
  • Animating layout-heavy properties instead of transform and opacity. If your effect also paints big shadows, filters, or backgrounds, test it on a slower device to make sure scrolling stays smooth.
  • Hiding elements by default without wrapping that rule in @supports. Always start from visible, readable content and enhance from there.

When CSS is enough

CSS is the right tool whenever scroll position directly drives a visual change, like progress bars, card reveals, scale effects, or subtle parallax.

Reach for JavaScript when scrolling needs to trigger real app behavior, like loading more data, updating the URL, starting media playback, or firing analytics events.

Current browsers support scroll-driven animations, though individual features don’t all have the exact same support yet. Keep your visible fallback in place and check the MDN scroll-driven animations guide when you want to verify a specific property.

If you’re already comfortable with CSS animations and CSS transitions, the only new piece here is the timeline itself. Try building a reading progress bar first, then add a single view() reveal on a card.

Want me to talk about your product? You can sponsor this site.

Related posts about css: