← Back to HTML & CSS, page 9

html-css-0169 · Hard senior

What is the View Transitions API, and how do you use it with a fallback?

document.startViewTransition(update) snapshots the old state, runs the update and animates to the new state. view-transition-name and the ::view-transition pseudo-elements customize it. It is newer, so check support, run the update directly where it is missing, and respect reduced motion.

Full answer

The View Transitions API animates between two states of the page with very little code. The browser takes a snapshot of the old state, runs your update, takes a snapshot of the new state, and cross-fades between them. You can then customize the animation with CSS.

Basic use:

js
document.startViewTransition(() => { updateTheDom(); });

The callback must change the DOM, and it can return a promise. The call returns a ViewTransition object with promises such as ready, updateCallbackDone and finished.

Customizing with CSS:

  • view-transition-name on an element gives it its own snapshot, so it can move or morph between positions. Names must be unique on the page at one time.
  • Pseudo-elements such as ::view-transition-old(name), ::view-transition-new(name) and ::view-transition-group(name) control the animation of each snapshot, with ordinary keyframes and timing.
  • The default cross-fade can be changed or removed for the whole page with ::view-transition-group(root).

Work for the same site across pages needs an extra opt-in with a rule for cross-document transitions, which is newer still.

Safety rules:

  • It is a newer feature, so check the support table. Always test if (!document.startViewTransition) and run the update directly. The page must work the same without the animation.
  • Respect reduced motion by disabling the animation inside @media (prefers-reduced-motion: reduce).
  • During the transition the page is not interactive, because input goes to a snapshot. Keep the duration short.
  • Do not give many elements names that are not needed, since each one makes a separate snapshot.

The box changes size on click. In a browser with the API, the change is animated from its old to its new snapshot. In a browser without it, the size changes at once, with the same final result.

The final state is the same in both cases, which is the test for a good fallback.

html
<style>
  #box { width: 60px; height: 60px; background: tomato; view-transition-name: box; }
  #box.big { width: 120px; }
  @media (prefers-reduced-motion: reduce) { ::view-transition-group(*) { animation: none; } }
</style>
<div id="box"></div>
<button id="b" type="button">Grow</button>
<script>
  function update() { box.classList.toggle("big"); }
  b.addEventListener("click", () => {
    if (!document.startViewTransition) { update(); window.vt = null; return; }
    window.vt = document.startViewTransition(update);
  });
</script>

Checked in Chrome 156 only, where view transitions are available. A browser without them updates at once with no animation, and that case was not run. Check the support table. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • startViewTransition wraps a DOM update.
  • The browser animates between old and new snapshots.
  • view-transition-name and ::view-transition-* customize it.
  • Feature-test and keep the same final state without it.
  • Keep transitions short and honour reduced motion.

Common mistakes

  • Calling startViewTransition with no check for support.
  • Reusing the same view-transition-name on several visible elements.
  • Using long transitions, which block interaction.

Follow-ups

  • What does view-transition-name do?
  • Why can the page not be used during a long view transition?

Tips

  • Describe the snapshot idea in two sentences.
  • Say that the update must work with or without the API.