Skip to main content

CSS Animations & Transitions

Scroll Animations

Written by Updated Scroll Animations exercises

Drive animation progress from a scroll container or an element's passage through the scrollport instead of elapsed time. It distinguishes scroll and view timelines, animation ranges, named timelines, progressive enhancement, and motion preferences.

Most animations run on a clock. A scroll-driven animation runs on the scrollbar: scroll down and it moves forward, scroll back up and it rewinds. That makes it perfect for reading progress bars and gentle reveal effects.

You build them with the same @keyframes you already know from CSS Animations. The only difference is that you tell the animation to follow scrolling instead of time. If you are also controlling where scrolling stops, see Scroll Snap.

Two kinds of timeline

  • scroll() follows a scrolling container from top to bottom. Use it for things tied to the whole page, like a progress bar.
  • view() follows one element as it comes into view, crosses the screen and leaves. Use it for effects on that element, like fading a card in.

A reading progress bar

Example

css

.reading-progress {
  position: fixed;
  z-index: 10;
  inset: 0 0 auto;
  height: 0.35rem;
  background: #2563eb;
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(root block);
}

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

The bar sits at the top of the page and starts at zero width. animation-timeline: scroll(root block) ties it to the page's vertical scroll, so it is full width exactly when you reach the bottom. No JavaScript, no scroll event listeners.

Fade an element in as it scrolls into view

Example

css

.reveal {
  animation: reveal linear both;
  animation-timeline: view(block);
  animation-range: entry 10% cover 35%;
}

@keyframes reveal {
  from { opacity: 0; translate: 0 2rem; }
  to { opacity: 1; translate: 0 0; }
}

animation-range picks which part of the journey the animation uses. Here it runs from when 10% of the element has entered until it covers 35% of the screen. One trap: write animation-timeline after the animation shorthand. The shorthand resets the timeline, so if it comes second, your timeline is silently wiped.

Make it optional

Browsers that do not support scroll timelines will simply ignore them, so make sure the page looks complete without the effect. Content should never start hidden and depend on the animation to appear. Wrap the effect in @supports (animation-timeline: scroll()), and turn it off for people who prefer reduced motion.

Timeline Reference

SyntaxPurposeExample
scroll()Creates an anonymous scroll timeline.animation-timeline: scroll()
view()Creates an anonymous view timeline.animation-timeline: view()
scroll-timelineNames a scroll timeline.scroll-timeline: --page block
animation-rangeLimits the active timeline range.animation-range: entry 0% cover 40%

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Scroll Animations example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.reading-progress {
  position: fixed;
  z-index: 10;
  inset: 0 0 auto;
  height: 0.35rem;
  background: #2563eb;
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(root block);
}

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

.reveal {
  animation: reveal linear both;
  animation-timeline: view(block);
  animation-range: entry 10% cover 35%;
}

@keyframes reveal {
  from { opacity: 0; translate: 0 2rem; }
  to { opacity: 1; translate: 0 0; }
}
  </style>
</head>
<body>
  <div class="reading-progress"></div><main><h1>Scroll the page</h1><div style="min-height:70vh"></div><article class="reveal">This card uses a view progress timeline where supported.</article><div style="min-height:70vh"></div></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
animation-timeline115115Preview26
scroll-timeline115115Preview26

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Firefox only has this behind a setting so far, which is another reason to treat it as a bonus, not a requirement.

Scroll Animations exercises

Driven by scroll

Write @keyframes grow from width 0% to 100%, play it on the bar, and drive it by the scroll of the page with animation-timeline scroll().

The HTML your code works on
<style>
  .bar { height: 6px; background: teal; }
  .item { height: 100px; }
</style>
<div class="bar"></div>
<div class="item">Item</div>

Result

Check your answer to see it here.