On this page10 sections
  1. Learning Objectives
  2. 1. A Cross-Fade in One Line
  3. 2. Custom Animations
  4. 3. Animating One Element: view-transition-name
  5. 4. Between Pages
  6. 5. Respect Reduced Motion
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 038: View Transitions

Category7: Advanced Styling
Lecture038 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

When content changes (a new tab, a new page, a list re-ordering), it normally just jumps. The View Transitions API takes a "before" and "after" snapshot and animates smoothly between them, often with a single line of code.


Learning Objectives

  • Animate a change on the same page with document.startViewTransition()
  • Customise the animation with ::view-transition-old and ::view-transition-new
  • Animate one element separately with view-transition-name
  • Turn on transitions between pages with @view-transition

1. A Cross-Fade in One Line

Wrap the code that changes the page in document.startViewTransition().

Example: switch the tabs:

html
<style>
  .tabs button { padding: 8px 14px; border: 0; border-radius: 6px; margin-right: 4px; cursor: pointer; }
  #panel { margin-top: 10px; padding: 18px; border-radius: 10px; background: #e7f5ff; min-height: 60px; }
</style>

<div class="tabs">
  <button onclick="show('Lahore: famous for its food, gardens and Mughal buildings.', '#e7f5ff')">Lahore</button>
  <button onclick="show('Karachi: the biggest city, by the Arabian Sea.', '#fff3bf')">Karachi</button>
  <button onclick="show('Islamabad: the green capital at the foot of the Margalla Hills.', '#d3f9d8')">Islamabad</button>
</div>
<div id="panel">Choose a city.</div>

<script>
  function show(text, colour) {
    const update = () => {
      const p = document.getElementById('panel');
      p.textContent = text;
      p.style.background = colour;
    };
    if (document.startViewTransition) document.startViewTransition(update);
    else update();                                // older browsers: just change it
  }
</script>

The default animation is a smooth cross-fade.


2. Custom Animations

The browser creates pseudo-elements for the old and new snapshots, which you can animate.

Example

html
<style>
  @keyframes slide-out { to { transform: translateX(-30px); opacity: 0; } }
  @keyframes slide-in  { from { transform: translateX(30px); opacity: 0; } }
  ::view-transition-old(root) { animation: 0.3s ease both slide-out; }
  ::view-transition-new(root) { animation: 0.3s ease both slide-in; }
  .step { padding: 24px; border-radius: 10px; background: #f3f0ff; font-size: 20px; }
</style>

<div class="step" id="step">Step 1 of 3</div>
<p><button onclick="next()">Next step</button></p>

<script>
  let n = 1;
  function next() {
    const go = () => { n = n % 3 + 1; document.getElementById('step').textContent = `Step ${n} of 3`; };
    document.startViewTransition ? document.startViewTransition(go) : go();
  }
</script>

3. Animating One Element: view-transition-name

Give an element a unique view-transition-name, and the browser animates its change in size and position as well.

Example: toggle the card's size:

html
<style>
  #card { view-transition-name: card; background: #1971c2; color: white; border-radius: 12px; padding: 12px; width: 120px; cursor: pointer; }
  #card.big { width: 100%; padding: 40px; font-size: 22px; }
</style>

<div id="card" onclick="const t = () => this.classList.toggle('big'); document.startViewTransition ? document.startViewTransition(t) : t();">
  Click me
</div>

4. Between Pages

For a normal multi-page site (same website, different pages), add this CSS to both pages:

css
@view-transition { navigation: auto; }

Clicking a link then cross-fades to the next page instead of flashing white. Elements with the same view-transition-name on both pages (such as a product photo) animate from one position to the other.


5. Respect Reduced Motion

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

Common Beginner Mistakes

1. Two elements with the same view-transition-name. Names must be unique on the page at the moment of the transition.

2. Not checking for support. Use if (document.startViewTransition) so older browsers simply update without animating.

3. Long, flashy transitions. Keep them around 0.2–0.4 seconds.


Practice Questions

  1. Which function starts a view transition on the same page?
  2. What is the default animation?
  3. Which pseudo-elements hold the old and new snapshots?
  4. How do you make one element animate its own size and position?
  5. What CSS enables transitions between pages of the same site?
  6. Why check if (document.startViewTransition)?

Practice Answers

  1. document.startViewTransition(callback)
  2. A cross-fade.
  3. ::view-transition-old(…) and ::view-transition-new(…)
  4. Give it a unique view-transition-name.
  5. @view-transition { navigation: auto; } on both pages.
  6. So browsers without support still update the page, just without animating.

Key Takeaways

  • startViewTransition(update) animates any change on the page.
  • Style ::view-transition-old/new for custom effects.
  • view-transition-name animates single elements.
  • @view-transition { navigation: auto; } animates between pages.