๐Ÿ”„ Mastering view-transition-name in CSS

๐Ÿ“Œ What Is view-transition-name?

The view-transition-name property is part of the modernCSS View Transitions API, which allows smooth animated transitions when switching between pages, states, or UI views โ€” without JavaScript-heavy solutions. โšกโœจ
It assigns a unique identifier to an element so the browser can match it across UI updates and animate the difference.

>>โ€œView transitions let the browser animate DOM changes โ€” effortlessly and beautifully.โ€

๐ŸŽฏ Why Use view-transition-name?

  • Smooth page transitions without SPAs
  • Animate elements between states automatically
  • Reduce JavaScript complexity
  • Great for dynamic UI, toggles, light/dark mode transitions, etc.

๐Ÿงฉ Basic Syntax

Syntax

view-transition-name: myTransitionElement;

๐Ÿ”ฅ Important Rules

  • You must enable document.startViewTransition() in JS
  • Elements must have the SAME view-transition-name before and after the change
  • The browser automatically animates differences
  • Names must be unique per element

๐Ÿงช Basic Example

HTML

HTML

<h1 id="title" view-transition-name="main-title">Hello</h1>
<button id="change">Change Text</button>

CSS

CSS

h1 {
  view-transition-name: main-title;
}

JS

JavaScript

document.getElementById('change').onclick = () => {
  document.startViewTransition(() => {
    document.getElementById('title').textContent = 'Welcome!';
  });
};

โœ” The browser animates the old text to the new text smoothly.

๐ŸŽจ Styling the Transition

A view transition creates three pseudo-elements you can style:

  • ::view-transition-old() โ€“ before state
  • ::view-transition-new() โ€“ after state
  • ::view-transition-group() โ€“ container for both

Styling Transition Layers

::view-transition-old(main-title),
::view-transition-new(main-title) {
  animation: fade 0.4s ease;
}

@keyframes fade

@keyframes fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

โœ” Customize how elements fade, slide, scale, or blur during transitions.

๐Ÿ“ฆ Example: Card Switching Animation

HTML

HTML

<div class="card" view-transition-name="info-card">Content A</div>
<button id="swap">Swap</button>

JS

JavaScript

const card = document.querySelector('.card');

document.querySelector('#swap').onclick = () => {
  document.startViewTransition(() => {
    card.textContent =
      card.textContent === 'Content A' ? 'Content B' : 'Content A';
  });
};

CSS

CSS

.card {
  view-transition-name: info-card;
  padding: 20px;
  border-radius: 12px;
  background: #222;
  color: white;
}

::view-transition-new(info-card) {
  animation: cardEnter 0.4s ease-out;
}

@keyframes cardEnter {
  from { opacity: 0; transform: scale(0.9); }
  to   { opacity: 1; transform: scale(1); }
}

โœ” Creates a smooth transition between the two card contents.

๐ŸŒ— Dark Mode Toggle Example

JS

toggleBtn.onclick = () => {
  document.startViewTransition(() => {
    document.documentElement.classList.toggle('dark');
  });
};

CSS

html {
  view-transition-name: page-theme;
}

::view-transition-old(page-theme),
::view-transition-new(page-theme) {
  animation: themeSwap 0.3s ease-in-out;
}

@keyframes themeSwap {
  from { filter: brightness(0.4); }
  to   { filter: brightness(1); }
}

โœ” Smooth, polished theme switching out-of-the-box.

๐Ÿง  Tips & Best Practices

  • Assign unique transition names for each animating element
  • Use startViewTransition() to wrap DOM updates
  • Donโ€™t animate huge elements unless necessary (performance)
  • Style pseudo-elements for full control
  • Avoid transitions for rapidly changing elements

๐Ÿ” Debugging Tips

  • If animation doesnโ€™t happen: check if view-transition-name matches exactly
  • Ensure transitions are triggered inside startViewTransition()
  • View transitions only work on elements inside the updated DOM tree
  • Make sure your browser supports the View Transitions API

๐Ÿ”— Helpful Resources

>>โ€œview-transition-name unlocks native page transitions โ€” smooth, automatic, magical.โ€ โœจ