---
title: "Scroll-driven animations with animation-timeline"
description: "Build reading progress bars, reveal effects and shrinking headers with CSS scroll-driven animations, using scroll() and view() timelines instead of scroll listeners."
url: https://www.danny.engineering/article/scroll-driven-animations-with-animation-timeline
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["CSS", "Performance"]
---

# Scroll-driven animations with animation-timeline

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.


```html
<div class="progress" aria-hidden="true"></div>
<main>
  <!-- long article content -->
</main>
```


```css
.progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 4px;
  background: var(--accent);
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(root block);
}

@keyframes grow {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}
```


`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), `root` or `self`.
- The axis can be `block` (the default), `inline`, `x` or `y`.

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.


```css
.reveal {
  animation: fade-up linear both;
  animation-timeline: view();
  animation-range: entry 0% entry 100%;
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(2rem);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
```


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:

- `entry` is the part where the element is entering the scrollport.
- `exit` is the part where it is leaving.
- `contain` is the part where it is fully inside the scrollport (or fully covering it, if it is taller).
- `cover` is 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:


```css
.card {
  animation: in-and-out linear both;
  animation-timeline: view();
}

@keyframes in-and-out {
  entry 0%   { opacity: 0; scale: 0.9; }
  entry 100% { opacity: 1; scale: 1; }
  exit 0%    { opacity: 1; scale: 1; }
  exit 100%  { opacity: 0; scale: 0.9; }
}
```


## 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.


```css
.gallery {
  overflow-x: auto;
  scroll-timeline: --gallery inline;
}

.gallery-indicator {
  animation: grow linear both;
  animation-timeline: --gallery;
}
```


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:


```css
.gallery-wrapper {
  timeline-scope: --gallery;
}
```


## 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.


```css
.site-header {
  position: sticky;
  top: 0;
  animation: shrink linear both;
  animation-timeline: scroll(root);
  animation-range: 0 200px;
}

@keyframes shrink {
  to {
    padding-block: 0.5rem;
    box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
  }
}
```


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.


```javascript
const bar = document.querySelector('.progress');

bar.animate(
  { transform: ['scaleX(0)', 'scaleX(1)'] },
  {
    fill: 'both',
    easing: 'linear',
    timeline: new ScrollTimeline({ source: document.documentElement }),
  }
);
```


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 `transform` and `opacity` where you can. Animating `width`, `height`, `top`, `padding` or `box-shadow` works but needs layout or paint on the main thread every frame.
- Use `scale` and `translate` (or `transform: scaleX()`) for growing bars rather than animating `width`.
- Do not add `will-change` to 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:


```css
@media (prefers-reduced-motion: no-preference) {
  .reveal {
    animation: fade-up linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 100%;
  }
}
```


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:


```css
@supports (animation-timeline: view()) {
  .reveal {
    animation: fade-up linear both;
    animation-timeline: view();
    animation-range: entry 0% entry 100%;
  }
}
```


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-timeline` links a normal CSS animation to scrolling instead of time.
- Use `scroll()` for effects tied to a container's scroll position and `view()` for effects tied to an element passing through the viewport.
- `animation-range` picks the slice of the scroll where the animation runs, and `animation-timeline` must come after the `animation` shorthand.
- Animate `transform` and `opacity` to keep the work off the main thread, and respect `prefers-reduced-motion`.
- Support became Baseline in 2026 with Firefox 159, so wrap it in `@supports` and treat it as an enhancement for now.

Further reading: [MDN: CSS scroll-driven animations](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_scroll-driven_animations)
