Home / Motion / View Transitions

View Transitions CSS Template, Free with Live Demo

Free View Transitions API template for a recipe site. Click a card and its picture and title morph smoothly into a full recipe view, then back. Live demo. The example is Fork and Field, a recipe site.

Open live demoDownload HTML fileView code on GitHub
View Transitions CSS template example: Fork and Field, a recipe site

What is view transitions?

The View Transitions API lets the browser animate between two states of a page for you. Here, the picture and title of a recipe card grow into the big picture and heading of the recipe page, like a native app.

You wrap the change in document.startViewTransition() and give matching elements the same view-transition-name. The browser takes a snapshot of both states and morphs between them. Where it is not supported, the page simply switches.

Good for

  • Card grids that open into detail pages
  • Photo galleries
  • Online shops, product to product page
  • Any single page app with views

What you get in this template

The key CSS

This is the part that makes the view transitions look work. Copy it into your own project.

::view-transition-group(*) {
  animation-duration: .45s;
  animation-timing-function: cubic-bezier(.2, .8, .2, 1);
}

/* JS */
card.querySelector('.pic').style.viewTransitionName = 'hero';
document.startViewTransition(() => {
  detailPic.style.viewTransitionName = 'hero';
  showDetail();
});

How to use it

  1. Click Download HTML file above.
  2. Open the file in any code editor, like VS Code.
  3. Change the text, colors and links to match your project. Colors are at the top of the style tag.
  4. Upload it to any host, such as GitHub Pages, Netlify or your own server. It is one file with no build step.

Questions people ask

What is the View Transitions API?

It is a browser feature that animates between two states of the page. You make the change inside startViewTransition() and the browser handles the animation.

What does view-transition-name do?

It tells the browser that two elements, one before and one after the change, are the same thing, so it morphs one into the other.

Can I use view transitions between separate pages?

Yes. Add @view-transition { navigation: auto; } to both pages on the same site. Chrome, Edge and Safari support it.

More Motion designs