---
title: "View Transitions for multi-page and single-page apps"
description: "Animate between pages and UI states with the View Transition API, from document.startViewTransition in SPAs to cross-document transitions in plain multi-page sites."
url: https://www.danny.engineering/article/view-transitions-for-multi-page-and-single-page-apps
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["CSS", "Performance"]
---

# View Transitions for multi-page and single-page apps

Native apps have always had smooth transitions between screens. On the web, getting the same effect usually meant a heavy animation library, keeping old and new content in the DOM at the same time, and careful choreography to avoid layout jumps. Multi-page sites could not animate between pages at all: the old page vanished and the new one appeared.


The View Transition API changes that. The browser takes a snapshot of the page before a change, takes another after it, and animates between the two using CSS you control. It works for state changes inside a single-page app, and it also works for ordinary navigations between separate HTML pages.


This article is for front-end developers who want polished transitions without shipping a large animation library. We will start with a single-page example, move on to multi-page sites, then cover shared element transitions, transition types and reduced motion.


## How a view transition works


Every view transition follows the same steps:

1. The browser captures a snapshot of the current state.
2. Your code (or a navigation) updates the DOM.
3. The browser captures the new state.
4. It builds a tree of pseudo-elements holding the old and new snapshots, and animates between them.
5. When the animation ends, the pseudo-elements are removed and the real, updated page is left behind.

The pseudo-element tree looks like this:


```css
::view-transition
└─ ::view-transition-group(root)
   └─ ::view-transition-image-pair(root)
      ├─ ::view-transition-old(root)
      └─ ::view-transition-new(root)
```


By default the whole page is one group called `root`, and the browser cross-fades the old snapshot into the new one. Everything else in this article is about customising that.


One point worth knowing early: during the transition, the snapshots are images. The page underneath is already updated, but the user sees the animation until it finishes. Keep transitions short so the page does not feel unresponsive.


## Single-page apps: document.startViewTransition


In a single-page app you control when the DOM changes, so you wrap that change in `document.startViewTransition()`.


```javascript
function showTab(tabName) {
  const update = () => {
    document.querySelectorAll('[data-panel]').forEach((panel) => {
      panel.hidden = panel.dataset.panel !== tabName;
    });
  };

  // Fall back to an instant update where the API is missing
  if (!document.startViewTransition) {
    update();
    return;
  }

  document.startViewTransition(update);
}
```


That is all you need for a cross-fade. The callback can also be async, which is useful when the new content comes from the network:


```javascript
async function loadArticle(id) {
  const transition = document.startViewTransition(async () => {
    const res = await fetch(`/api/articles/${id}`);
    const article = await res.json();
    renderArticle(article);
  });

  await transition.finished;
  console.log('Transition complete');
}
```


Be careful with that pattern. The browser holds the old snapshot on screen while the callback runs, and the page does not respond to input. If the request is slow, fetch the data first and only start the transition when you are ready to update the DOM.


### The ViewTransition object


`startViewTransition()` returns a `ViewTransition` object with three promises:

- `updateCallbackDone` resolves when your callback has finished.
- `ready` resolves when the pseudo-elements exist and the animation is about to start. This is the moment to run custom animations with the Web Animations API.
- `finished` resolves when the animation has ended and the new view is fully interactive.

It also has a `skipTransition()` method, which jumps straight to the end state. Your update callback still runs.


```javascript
const transition = document.startViewTransition(update);

transition.ready.then(() => {
  document.documentElement.animate(
    { clipPath: ['circle(0% at 50% 50%)', 'circle(150% at 50% 50%)'] },
    {
      duration: 400,
      easing: 'ease-out',
      pseudoElement: '::view-transition-new(root)',
    }
  );
});
```


That example reveals the new page through a growing circle, a common effect for switching between light and dark themes.


### Using it with React


In React, the DOM update happens when React commits, not when you call `setState`. To make sure the update happens inside the transition callback, use `flushSync` from `react-dom`:


```typescript
import { flushSync } from 'react-dom';

function toggleView() {
  if (!document.startViewTransition) {
    setView((v) => (v === 'grid' ? 'list' : 'grid'));
    return;
  }

  document.startViewTransition(() => {
    flushSync(() => {
      setView((v) => (v === 'grid' ? 'list' : 'grid'));
    });
  });
}
```


Frameworks increasingly offer their own integrations, so check your router or framework docs before wiring this up by hand.


## Multi-page apps: cross-document view transitions


For a plain multi-page site, you do not write any JavaScript at all. Add this rule to the CSS of both the page you are leaving and the page you are going to:


```css
@view-transition {
  navigation: auto;
}
```


Now, when a user follows a link from one page to another, the browser cross-fades between them instead of flashing to white.


A few rules apply:

- Both pages must opt in. If only one page has the rule, there is no transition.
- The navigation must be same-origin. Transitions do not run when navigating to another site.
- It applies to ordinary navigations such as clicking a link, submitting a form, or using back and forward. It does not run when the user types a URL into the address bar or reloads.
- If the new page takes too long to load, the browser skips the transition rather than freezing the old page indefinitely.

This is one of the cheapest polish improvements you can make to a server-rendered site, static site or blog, because it needs no client-side routing.


### Hooking into cross-document transitions


Two events give you control when you need it:

- `pageswap` fires on the old page just before it is replaced.
- `pagereveal` fires on the new page before its first render.

Both events have a `viewTransition` property, which is the `ViewTransition` object (or `null` if no transition is running). You can use them to set names or types based on where the user is going:


```javascript
window.addEventListener('pagereveal', (event) => {
  if (!event.viewTransition) return;

  const from = navigation.activation?.from?.url;
  if (from && new URL(from).pathname === '/') {
    event.viewTransition.types.add('from-home');
  }
});
```


That example uses the Navigation API to find the previous URL. Check support for it in your target browsers before you rely on it.


## Shared element transitions with view-transition-name


The cross-fade is nice, but the real magic is when an element appears to move from one place to another, such as a product thumbnail growing into the hero image on its detail page.


Give the element a `view-transition-name`:


```css
.product-thumb {
  view-transition-name: product-image;
}
```


On the detail page, give the hero image the same name:


```css
.product-hero {
  view-transition-name: product-image;
}
```


The browser now creates a separate group called `product-image`. Instead of fading, it animates the position and size of the group from the old element to the new one, and cross-fades the contents.


The rule to remember: **each name must be unique on the page at the moment of capture.** If two elements share a name, the transition is skipped and an error is logged to the console.


That makes lists tricky. On a page with twenty product cards, you cannot give every image the same name. Set the name only on the one being clicked:


```javascript
card.addEventListener('click', () => {
  card.querySelector('img').style.viewTransitionName = 'product-image';
});
```


Or give each card a unique name from its ID in your template:


```html
<img class="product-thumb" style="view-transition-name: product-42" src="/img/42.jpg" alt="Blue kettle">
```


Newer browsers also support `view-transition-name: match-element`, which generates a unique name for each element automatically for same-document transitions. Check support before depending on it.


### Customising the animation


Each named group gets its own pseudo-elements, which you style like any other element:


```css
/* Slow down the shared image */
::view-transition-group(product-image) {
  animation-duration: 400ms;
  animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}

/* Slide the rest of the page instead of fading */
@keyframes slide-out {
  to { transform: translateX(-30px); opacity: 0; }
}

@keyframes slide-in {
  from { transform: translateX(30px); opacity: 0; }
}

::view-transition-old(root) {
  animation: 200ms ease-in both slide-out;
}

::view-transition-new(root) {
  animation: 300ms ease-out both slide-in;
}
```


A useful trick is to give a site header its own name so it stays still while the content around it animates:


```css
.site-header {
  view-transition-name: site-header;
}

::view-transition-group(site-header) {
  animation: none;
}
```


## Transition types: forwards and backwards


Often you want different animations depending on direction: slide left when going forward, right when going back. View transition types let you tag a transition and target it in CSS.


In a single-page app, pass types when you start the transition:


```javascript
document.startViewTransition({
  update: () => goToStep(next),
  types: [next > current ? 'forward' : 'backward'],
});
```


Then select on the active type with the `:active-view-transition-type()` pseudo-class:


```css
html:active-view-transition-type(forward) {
  &::view-transition-old(root) { animation-name: slide-out-left; }
  &::view-transition-new(root) { animation-name: slide-in-right; }
}

html:active-view-transition-type(backward) {
  &::view-transition-old(root) { animation-name: slide-out-right; }
  &::view-transition-new(root) { animation-name: slide-in-left; }
}
```


For cross-document transitions, you can list types in the at-rule, or add them in the `pageswap` and `pagereveal` events as shown earlier:


```css
@view-transition {
  navigation: auto;
  types: slide;
}
```


Types arrived after the core API, so support is narrower. Check before relying on them, and make sure the default transition still looks fine without them.


## Respecting prefers-reduced-motion


Some users find movement on screen uncomfortable or disorientating, and set a reduced motion preference in their operating system. Large sliding or zooming transitions are exactly the sort of motion they want to avoid.


The simplest approach is to turn off view transition animations when that preference is set:


```css
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation: none !important;
  }
}
```


For cross-document transitions, you can opt in only when motion is acceptable:


```css
@media (prefers-reduced-motion: no-preference) {
  @view-transition {
    navigation: auto;
  }
}
```


A softer option is to keep a quick cross-fade for reduced motion users and drop only the sliding and scaling. A short fade is generally considered gentle, but the safest default is still to respect the preference fully.


In JavaScript, you can check the same preference and skip the transition altogether:


```javascript
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

if (reduceMotion || !document.startViewTransition) {
  update();
} else {
  document.startViewTransition(update);
}
```


## Performance notes


View transitions are cheap compared to animating real DOM elements, because the browser animates flat snapshots on the compositor. Still, a few habits help:

- **Keep them short.** 200 to 400 milliseconds is plenty. During the animation, the user is looking at images and cannot interact with the new page.
- **Name only what moves.** Every named element creates its own group and snapshot. Dozens of named elements cost memory and can make the transition stutter on low-end devices.
- **Do not block on the network.** In single-page apps, fetch first, then start the transition. In multi-page apps, a fast server response matters more than ever, because a slow page will see the transition skipped.
- **Avoid layout shifts in the new page.** If the new page's images have no dimensions, the shared element may animate to the wrong place. Set `width` and `height` on images.

## Browser support


At the time of writing (late 2026):

- **Same-document transitions** (`document.startViewTransition`) are Baseline. They have been in Chrome and Edge since version 111, in Safari since 18, and in Firefox since 144, released in October 2025.
- **Cross-document transitions** (`@view-transition`) work in Chrome and Edge from version 126 and in Safari from 18.2. Firefox has not fully shipped them yet; its work is tracked in Mozilla's bug tracker, so check caniuse for the current status.
- **Transition types** came later than the core API. Firefox added same-document types in version 147.

The good news is that view transitions are a natural progressive enhancement. In a browser without support, `@view-transition` is ignored and the navigation happens instantly, exactly as it does today. In JavaScript, a feature check on `document.startViewTransition` lets you fall back to a plain update.


## When not to use it

- **Frequent, rapid updates.** Typing in a search box, dragging a slider or live data updates should not trigger a view transition each time. Starting a new transition skips the previous one, and the result feels jittery.
- **Slow updates.** If the DOM update depends on a slow network request, the page appears frozen while the old snapshot is shown. Show a loading state instead.
- **Long, decorative sequences.** Transitions should help users understand what changed. If an animation exists only to look impressive, it will soon feel like it is slowing people down.
- **Cross-origin navigation.** Cross-document transitions only work within the same origin, so they cannot be used between your marketing site and an app on a different domain.
- **Interactive animation.** Because the transition animates snapshots, you cannot interact with the content mid-animation. For gestures such as swipe-to-go-back, where the user controls progress, a different technique may suit better.

## Wrapping up

- Wrap single-page DOM updates in `document.startViewTransition()` and feature-check it for a safe fallback.
- Add `@view-transition` with `navigation: auto` to both pages for cross-document transitions on multi-page sites, with no JavaScript.
- Use `view-transition-name` for shared element transitions, and keep each name unique on the page.
- Style the `::view-transition-old`, `::view-transition-new` and `::view-transition-group` pseudo-elements to customise the animation, and use types for direction-aware effects.
- Respect `prefers-reduced-motion`, keep transitions short, and treat the whole feature as progressive enhancement.

## Related reading

- [MDN: View Transition API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API)
- [MDN: Using the View Transition API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API/Using)
