On this page10 sections
Lecture 038: View Transitions
| Category | 7: Advanced Styling |
| Lecture | 038 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
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-oldand::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:
<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
<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:
<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:
@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
@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
- Which function starts a view transition on the same page?
- What is the default animation?
- Which pseudo-elements hold the old and new snapshots?
- How do you make one element animate its own size and position?
- What CSS enables transitions between pages of the same site?
- Why check
if (document.startViewTransition)?
Practice Answers
document.startViewTransition(callback)- A cross-fade.
::view-transition-old(…)and::view-transition-new(…)- Give it a unique
view-transition-name. @view-transition { navigation: auto; }on both pages.- 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/newfor custom effects. view-transition-nameanimates single elements.@view-transition { navigation: auto; }animates between pages.