Lesson 25 of 52Modern CSS
View Transitions and Scroll-Driven Animations
Animate between page states with the View Transition API, link whole pages with cross-document transitions, and tie animation to scroll with pure CSS.
A designer hands you a prototype: when you sort a product grid the cards glide to their new places, when you open a product the thumbnail grows into the hero image on the next page, and a thin bar along the top fills as you read. Two years ago each of those meant a JavaScript animation library, measuring element positions before and after every change, and a scroll listener firing hundreds of times a second. Today the browser does all three. The View Transition API animates between two DOM states (even across full page loads), and scroll-driven animations bind ordinary CSS keyframes to scroll position, off the main thread. This lesson covers both in plain HTML, CSS and a few lines of JavaScript; Animation and View Transitions in the React course shows the same browser feature through React's component.
The problem view transitions solve
Animating a change of state is easy when you know both states up front: a transition from one class to another. It is hard when the DOM itself changes. Sorting a list re-orders elements, filtering removes some, navigating replaces the whole page. To animate those by hand you have to record where every element was, apply the change, measure where everything is now, then animate each element from old to new with transforms. That technique is called FLIP (First, Last, Invert, Play), and it is fiddly, slow on large lists and impossible across a page load.
A view transition flips the approach around. The browser takes a picture of the old state, lets you change the DOM however you like, takes a picture of the new state, and animates between the two pictures in an overlay. Your real DOM is never animated, so it does not matter how you changed it.
Same-document transitions: startViewTransition
The whole API is one call. Wrap your DOM update in document.startViewTransition():
function update() {
list.replaceChildren(...sorted);
}
if (!document.startViewTransition) {
update(); // older browser: just do it
} else {
document.startViewTransition(update);
}Same-document view transitions work in Chrome, Edge and Safari, and became Baseline when Firefox 144 shipped them in October 2025. The fallback above still matters for older browsers and costs one line: the update happens, only the animation is missing. That is the first principle of this whole lesson. Motion is an enhancement; the change must work without it.
What happens during that call, step by step:
You hand over the function that changes the DOM. Nothing has changed yet.
The object returned by startViewTransition has three promises, updateCallbackDone, ready and finished, plus skipTransition() to jump to the end. Two failure modes are worth memorising because they confuse people in code review:
- Duplicate names. Every
view-transition-namemust be unique among rendered elements when a snapshot is taken. If two elements share a name, the transition is skipped (readyrejects with anInvalidStateError), although your update still runs. The page changes with no animation and an error in the console. - Slow updates. Rendering is frozen while your update runs. If it takes too long (a slow network request inside the callback), the browser gives up after a few seconds with a
TimeoutError. Fetch first, then start the transition with an update that only touches the DOM.
Naming elements: view-transition-name and match-element
Without names, the whole page cross-fades as one image called root. To make individual elements glide, give them a view-transition-name. An element with the same name in the old and new state becomes one group that moves from its old box to its new one. A name only in the new state is an entry; only in the old state, an exit.
Naming twenty cards by hand (card-1, card-2...) used to require inline styles or generated CSS. Two newer values fix that, both supported in all current major browsers:
view-transition-name: match-elementnames an element by its identity: the same DOM node before and after is matched automatically. Perfect for re-ordering existing elements. It only works within one document, because a new page has new nodes.view-transition-classputs a class on the generated pseudo-elements so you can style many groups with one selector:::view-transition-group(.card).
Preview
Sort and filter a few times. The JavaScript does nothing clever: it re-orders the real elements and replaces the children. The motion comes entirely from the snapshot and the match-element names. Change 350ms to 1200ms to watch the groups slide and resize slowly, then delete the view-transition-name line to see the plain whole-page cross-fade you get without names.
A transition runs on this page. How many ::view-transition-group pseudo-elements are created? Type a number.
<style>
.card { view-transition-name: match-element; }
.sidebar { view-transition-name: sidebar; }
.footer { view-transition-name: none; }
</style>
<aside class="sidebar">...</aside>
<div class="card">A</div>
<div class="card">B</div>
<div class="card">C</div>
<footer class="footer">...</footer>Customising the animation
The pseudo-element tree is ordinary CSS. Animation and View Transitions in the React course walks through it in detail; the short version is that every named group has a ::view-transition-group(name) that moves and resizes, holding ::view-transition-old(name) and ::view-transition-new(name) images that fade. Target them like any element:
/* slow down everything */
::view-transition-group(*) { animation-duration: 250ms; }
/* the header should not move or fade at all */
.site-header { view-transition-name: header; }
::view-transition-group(header) { animation: none; }
/* a new item scales in, a removed one scales out */
::view-transition-new(.card):only-child { animation: pop-in 200ms ease-out; }
::view-transition-old(.card):only-child { animation: pop-out 150ms ease-in; }The :only-child trick works because an entry has only a new image in its pair and an exit has only an old one.
Direction with transition types
Going "next" and "back" should usually move in opposite directions. Types label a transition so CSS can choose animations: pass them when you start it, and match them with :active-view-transition-type().
Preview
The object form startViewTransition({ update, types }) arrived after the function form, so the try falls back to an untyped transition in browsers that only know the original call. Type names are yours to choose; "forward" and "backward" are just a convention.
Cross-document transitions for multi-page sites
Everything so far needed JavaScript to change the DOM. A traditional multi-page site, where every link loads a new HTML document, can animate between pages with CSS only. Opt in on both the old and the new page:
@view-transition {
navigation: auto;
}
/* on the listing page and on the product page */
.product-photo { view-transition-name: var(--photo-name); }Now a same-origin navigation (a link click, or back and forward) cross-fades the pages, and an element with the same view-transition-name on both pages morphs from its old position to its new one: the thumbnail grows into the hero image. Because each product needs a unique name, set it per element, for example style="--photo-name: photo-42" on the listing card and the same on the product page.
The rules:
- Both documents must opt in, and they must be same-origin with no cross-origin redirect in between.
- Only navigations the user starts on the page count. Typing a URL in the address bar does not animate.
- The old page fires
pageswapand the new page firespagereveal, each with theViewTransitionobject, so you can add types or names based on where the user is going. - If the new page is slow to render, the browser may snapshot it before your hero image exists.
<link rel="expect" href="#hero" blocking="render">tells the browser to wait until that element has been parsed.
Support is narrower than same-document transitions: Chrome, Edge and Safari (18.2 and later) support it, while Firefox does not yet, as of late 2026. In a browser without it the @view-transition rule is ignored and links navigate exactly as before, which is why you can ship it today. Check current support before promising it to a designer as part of a design.
@view-transition { navigation: auto; } to the product page only. A user clicks from the listing page to a product page. What happens?Scroll-driven animations
The second half of this lesson is a different feature with a similar spirit. Normally a CSS animation runs on the document timeline: time passing. A scroll-driven animation runs on a scroll timeline instead, so 0 percent of the keyframes means "scrolled to the start" and 100 percent means "scrolled to the end". There is no duration; scroll position is the clock.
Two timeline functions cover nearly every case:
animation-timeline: scroll()tracks how far a scroll container has scrolled.scroll(root)uses the page,scroll(nearest)(the default) the nearest scrolling ancestor,scroll(self)the element itself, and you can add an axis such asscroll(root inline).animation-timeline: view()tracks an element's own journey through its scroll container's visible area (its scrollport): from first peeking in at the bottom to leaving at the top.
animation-range chooses which slice of that journey the keyframes cover, with named ranges:
entry 0%
Top edge appears
The element's top edge reaches the bottom of the scrollport. Nothing is visible yet.
entry 100%
Fully in view
The bottom edge has come in too. The entry range is the part where it is sliding in. A reveal usually ends here or a little later.
contain
Fully visible
The whole element is inside the scrollport. For an element taller than the viewport, this is when it fills it.
exit 0%
Starts leaving
The top edge reaches the top of the scrollport and begins to disappear.
exit 100%
Gone
The bottom edge has left. The cover range spans the whole journey, from entry 0% to exit 100%.
Preview
Scroll the preview. Change the reveal's range to entry 0% cover 50% so boxes finish fading in only when they reach the middle of the preview, or swap translate: 0 40px for scale: 0.9. Notice the order of declarations: animation-timeline comes after the animation shorthand. The shorthand resets animation-timeline to auto, so writing them the other way round silently turns your scroll animation back into a time-based one with no duration.
Support and the fallback rule
Scroll-driven animations work in Chrome and Edge (since 2023) and Safari 26. Firefox has implemented them but, as of late 2026, has not yet shipped them in a stable release, so they are not Baseline. Check webstatus.dev or caniuse before you rely on them.
That makes the structure of the demo the important part:
- The base styles show the content fully visible. The reveal keyframes start at
opacity: 0, but they only apply inside the@supportsand reduced-motion blocks, so in Firefox, or with reduced motion on, every box is simply there. @supports (animation-timeline: scroll())gates the scroll animation itself.@media (prefers-reduced-motion: no-preference)means motion is opt-in for people who have not asked for less.
The classic bug is the reverse: .reveal { opacity: 0 } in the base styles and a scroll animation to fade it in. In any browser without support, or for a user with reduced motion, the content is invisible. A screen reader still reads it, so nobody notices in testing, and sighted users see blank space.
animation-timeline: view(); animation: reveal linear both;Performance: why this beats scroll listeners
The old way to build a reading progress bar was a scroll event listener that reads scrollY and writes a width. That code runs on the main thread, the same thread that runs your JavaScript, layout and React renders. When the main thread is busy, the bar stutters, and writing width forces layout on every scroll event.
A scroll-driven animation of transform or opacity can run on the compositor, the part of the browser that moves already-painted layers. It stays smooth even while the main thread is blocked. The same rule from Transitions and Animations applies with extra force: animate only transform (or the individual translate, scale and rotate properties) and opacity. Animating width, top or background-position on scroll forces layout or paint on every frame and drops you back to the main thread.
IntersectionObserver is still the right tool when you need JavaScript to run as something enters view: lazy-loading data, analytics impressions, starting a video. Its callbacks fire asynchronously, only when a threshold is crossed, so it is cheap. For a visual effect that follows scroll continuously, the CSS timeline wins. For a one-off reveal in browsers without scroll timelines, IntersectionObserver adding a class is a fine fallback.
View transitions have their own costs. Every named element is captured as an image, so naming hundreds of elements on a long page uses real memory and time. Name what moves meaningfully, keep durations around 150 to 350 milliseconds, and remember the page underneath is covered by the overlay until the animation ends, so a long transition makes the UI feel slower, not smoother.
Match each effect to the best tool.
- view transition
- A browser-managed animation between two DOM states, done by snapshotting the old and new states and animating the snapshots.
- view-transition-name
- A CSS property that captures an element as its own group so it animates separately from the page; names must be unique.
- match-element
- A view-transition-name value that names elements by identity, so the same DOM node is matched before and after without writing names.
- cross-document view transition
- A transition between two same-origin pages, enabled in CSS on both with @view-transition { navigation: auto; }.
- scroll timeline
- A timeline whose progress is the scroll position of a container, used with animation-timeline: scroll().
- view timeline
- A timeline tracking an element's progress through its scroll container's visible area, used with animation-timeline: view().
- animation-range
- Chooses which part of a timeline, such as entry, exit or cover, the keyframes are mapped onto.