Smooth Animations with CSS @view-transition

Smooth Animations with CSS @view-transition

February 6, 2025
By Mike Wall
2 min read

As a web developer, I'm always looking for new ways to make my websites smoother and more engaging. Recently, I stumbled across something pretty exciting—CSS's new @view-transition API. If you're like me and are always keen to explore new ways to add polish to your projects, you're going to love this.

What Exactly Is @view-transition?

The @view-transition API allows us to easily create animated transitions between different DOM states or pages without relying heavily on JavaScript libraries or complicated CSS animations. Essentially, it provides a smoother, more visually appealing user experience when navigating your site.

Why I'm Excited About It

Before discovering this, creating smooth animations between pages often required quite a bit of extra JavaScript or animation libraries like GSAP or Framer Motion. While these tools are powerful, they can sometimes add complexity and overhead. The beauty of @view-transition is that it simplifies this entire process.

How to Use It: My First Attempt

Here's a simple example of how you might use it:

<span class="hljs-keyword">@view-transition</span> {
  <span class="hljs-selector-tag">navigation</span>: <span class="hljs-selector-tag">auto</span>;
}

<span class="hljs-selector-pseudo">::view-transition-old(root)</span>,
<span class="hljs-selector-pseudo">::view-transition-new(root)</span> {
  <span class="hljs-attribute">animation-duration</span>: <span class="hljs-number">0.3s</span>;
}

This snippet enables smooth transitions on navigation. What's particularly nice is that browsers handle a lot of heavy lifting behind the scenes, making these animations performant and easy to implement.

Transitioning Specific Elements Using view-transition-name

You can also directly control transitions between specific elements by using the view-transition-name property on HTML elements. Here's a clear example:

HTML:

<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"view-transition-name: box-transition;"</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>

CSS:

<span class="hljs-selector-pseudo">::view-transition-old(box-transition)</span>,
<span class="hljs-selector-pseudo">::view-transition-new(box-transition)</span> {
  <span class="hljs-attribute">animation</span>: fadeScale <span class="hljs-number">0.5s</span> ease-in-out;
}

<span class="hljs-keyword">@keyframes</span> fadeScale {
  <span class="hljs-selector-tag">from</span> { <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">scale</span>(<span class="hljs-number">0.8</span>); }
  <span class="hljs-selector-tag">to</span> { <span class="hljs-attribute">opacity</span>: <span class="hljs-number">1</span>; <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">scale</span>(<span class="hljs-number">1</span>); }
}

In this example, the browser smoothly animates between states of an element tagged with view-transition-name. It's particularly useful for emphasizing UI changes clearly and fluidly.

Things I Learned Along the Way

  • It's Progressive: Not all browsers support @view-transition yet. Make sure you provide fallback animations or behaviors.

  • Minimal Setup: Setting up is refreshingly straightforward compared to traditional animation methods.

  • Highly Customizable: You can target specific elements, transition durations, easing functions, and more.

Challenges to Keep in Mind

As it's a relatively new feature, there are some browser compatibility issues to be aware of. Always check browser support and plan accordingly.

Final Thoughts

I'm just scratching the surface with CSS @view-transition, but the potential here feels huge. If you're looking to upgrade your site's navigation or interactive elements with minimal hassle, definitely give this a shot.

Happy coding!

Table of Contents