Add view transitions to your website
Tags: progressive, enhancement, viewtransitions, api
Published: 2023-06-12 15:29:19

---
title: Add view transitions to your website
published: true
date: 2023-05-24 14:42:49 UTC
tags: progressive,enhancement,viewtransitions,api
canonical_url: https://adactio.com/journal/20195
---

I must admit, when [Jake](https://jakearchibald.com/) told me he was leaving Google, I got very worried about the future of [the View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API).

To recap: Chrome shipped support for the API, but only for single page apps. [That had me worried](https://adactio.com/journal/19573):

> If the View Transitions API works across page navigations, it could be the single best thing to happen to the web in years.
> 
> If the View Transitions API only works for single page apps, it could be the single worst thing to happen to the web in years.

Well, the multi-page version still hasn’t yet shipped in Chrome stable, but it is available in Chrome Canary behind a flag, so it looks like it’s almost here!

[Robin took the words out of my mouth](https://robinrendle.com/notes/view-transitions-api/):

> Anyway, even this cynical jerk is excited about this thing.

Are you the kind of person who flips feature flags on in nightly builds to test new APIs?

Me neither.

But I made an exception for the View Transitions API. [So did Dave](https://daverupert.com/2023/05/getting-started-view-transitions/):

> I think the most telling predictor for the success of the multi-page View Transitions API – compared to all other proposals and solutions that have come before it – is that I actually implemented this one. Despite animations being my bread and butter for many years, I couldn’t be arsed to even try any of the previous generation of tools.

[Dave’s post](https://daverupert.com/2023/05/getting-started-view-transitions/) is an excellent step-by-step introduction to using view transitions on your website. To recap:

Enable these two flags in [Chrome Canary](https://www.google.com/intl/en_uk/chrome/canary/):

```
chrome://flags#view-transition
chrome://flags#view-transition-on-navigation

```

Then add this `meta` element to the `head` of your website:



You could stop there. If you navigate around your site, you’ll see that the navigations now fade in and out nicely from one page to another.

But the real power comes with transitioning page elements. Basically, you want to say “this element on this page should morph into that element on that page.” And when I say morph, I mean morph. As Dave puts it:

> Behind the scenes the browser is rasterizing (read: making an image of) the before and after states of the DOM elements you’re transitioning. The browser figures out the differences between those two snapshots and tweens between them similar to Apple Keynote’s “Magic Morph” feature, the liquid metal T-1000 from <cite>Terminator 2: Judgement Day</cite>, or the 1980s cartoon series <cite>Turbo Teen</cite>.
> 
> If those references are lost on you, how about the popular kids book series <cite>Animorphs</cite>?

Some classic examples would be:

- A thumbnail of a video on one page morphs into the full-size video on the next page.
- A headline and snippet of an article on one page morphs into the full article on the next page.

I’ve added view transitions to [The Session](https://thesession.org/). Where I’ve got index pages with lists of titles, each title morphs into the heading on the next page.

Again, [Dave’s post](https://daverupert.com/2023/05/getting-started-view-transitions/) was really useful here. Each transition needs a unique name, so I used Dave’s trick of naming each transition with the ID of the individual item being linked to.

In the [recordings](https://thesession.org/recordings) section, for example, there might be a link like this on the index page:

```
<a href="/recordings/7812" style="view-transition-name: recording-7812">The Banks Of The Moy</a>

```

Which, if you click on it, takes you to the page with this heading:

```
<h1><span style="view-transition-name: recording-7812">The Banks Of The Moy</span></h1>

```

Why the `span`? Well, like Dave, I noticed some weird tweening happening between block and inline elements. Dave solved the problem with `width: fit-content` on the block-level element. I just stuck in an extra inline element.

Anyway, the important thing is that the name of the view transition matches: `recording-7812`.

I also added a view transition to pages that have maps. The position of the map might change from page to page. Now there’s a nice little animation as you move from one page with a map to another page with a map.

[![thesession.org View Transitions](https://i.ytimg.com/vi/llAHyqGqw8o/mqdefault.jpg)](https://www.youtube.com/watch?v=llAHyqGqw8o)

That’s all good, but I found myself wishing that I could _just_ have those enhancements. Every single navigation on the site was triggering a fade in and out—the default animation. I wondered if there was a way to switch off the default fading.

There is! That default animation is happening on a view transition named `root`. You can get rid of it with this snippet of CSS:

```
::view-transition-image-pair(root) {
  isolation: auto;
}
::view-transition-old(root),
::view-transition-new(root) {
  animation: none;
  mix-blend-mode: normal;
  display: block;
}

```

Voila! Now only the view transitions that you name yourself will get applied.

You can adjust the timing, the easing, and the animation properites of your view transitions. Personally, I was happy with the default morph.

In fact, that’s one of the things I like about this API. It’s another good example of declarative design. I say _what_ I want to happen, but I don’t need to specify the details. I’ll let the browser figure all that out.

That’s what’s got me so excited about this API. Yes, it’s powerful. But just as important, it’s got a very low barrier to entry.

Chris has gathered a bunch of examples together in his post [Early Days Examples of View Transitions](https://chriscoyier.net/2023/05/18/early-days-examples-of-view-transitions/). Have a look around to get some ideas.

If you like what you see, I highly encourage you to add view transitions to your website now.

“But wait,” I hear you cry, “this isn’t supported in any public-facing browser yet!”

To which, I respond “So what?” It’s a perfect example of progressive enhancement. Adding one `meta` element and a smidgen of CSS will do absolutely no harm to your website. And while no-one will see your lovely view transitions _yet_, once browsers do start shipping with support for the API, your site will automatically get better.

Your website will be enhanced. Progressively.