< Back

Animations In Web Apps Have Never Been Easier

How a quiet revolution is slowly changing web apps forever

calendar

Feb 2, 2025

Header for posting Animations In Web Apps Have Never Been Easier
Photo by Kelly Sikkema on Unsplash

What’s the one thing that can instantly bring a dull web app to life?

Animations.

Animations please the eye and bring a web app to life. But animations on the web are not just eye candy. They help users understand the context and flow of a web app through motion.

For example, when an item is moved from one list to another, the change can be instant or it can be an animated motion so the user clearly sees where the item went. With the animation, the flow will be much clearer and yes, it’s also much nicer to look at.

The power of CSS animations and transitions

We’ve already had access to CSS animations and transitions for years in all browsers and they’re quite easy to use. Let’s say that you want to change the background color of a link from green to blue when the user hovers the mouse over it. This is easy to accomplish with CSS:

/* default color */
a {
background-color: green;
}

/* hover color */
a:hover {
background-color: blue;
}

In this example, the change of color will be instant but with a single line of CSS, we can animate the change:

/* default color */
a {
background-color: green;

/* here's the magic */
transition: background-color .3s ease-out;
}

/* hover color */
a:hover {
background-color: blue;
}

Now, the background-color property will transition from green to blue in 0.3 seconds and the change will ease out, meaning that the change will slow down towards the end of the transition.

With CSS animations, we can do the same but now we define the change inside a @keyframes block where we define a start and an end state: the animation of the background-color property from green to blue:

/* keyframes block that defines the animation */
@keyframes color-change {
from {
background-color: green;
}
to {
background-color: blue;
}
}

/* default color */
a {
background-color: green;
}

/* animation triggered on hover */
a:hover {
animation: color-change .3s forwards;
}

In this example, we start the animation when the user hovers the link and define it takes 0.3 seconds and stays in the end state (green) with forwards.

There is much more you can do with transition and animation, but for this example, all you need to know for now is that you can animate changes in almost any CSS property.

But even though you can do amazing things with CSS transitions and animations, when you want to animate elements going from one position to another or want to transition one element into another, things become complicated very quickly.

What if you want to transition a product card in a webshop into a full product page? Or the moving of an element from one list to another? It can be done with transitions and animations only, but it would involve quite some JavaScript to do the calculations and this would quickly increase when you add more animations, so it’s surely not an easy task.

Until now.

Enter View Transitions

The View Transition API enables web apps to easily transition between two DOM views (or states). Using the API is as simple as updating the DOM with a function and passing that function to document.startViewTransition:

document.startViewTransition(() => {
// update the DOM somehow
});

If you update the whole page using this function, you will get a fading transition between the old and the new page by default. The browser will take care of this for you. You only need to wrap your DOM updating function in document.startViewTransition.

Let’s revisit the example I gave of clicking an item to move it from a todo-list to the done-list with appendChild. The JavaScript for it could look like this:

const todoList = document.querySelector('#todo-list');
const doneList = document.querySelector('#done-list');

todoList.addEventListener('click', e => {
const clickedItem = e.composedPath().find(el.tagName && el.tagName === 'LI');

if(clickedItem) {
doneList.appendChild(clickedItem);
}
})

This will instantly move the clicked item from the todo-list to the done-list, but if we give each item in both lists a unique view-transition-name:

<ul>
<li style="view-transition-name: item-1">Item 1</li>
<li style="view-transition-name: item-2">Item 2</li>
<li style="view-transition-name: item-2">Item 2</li>

...
</ul>

and wrap this in document.startViewTransition:

document.startViewTransition(() => {
doneList.appendChild(clickedItem);
}):

the browser will animate the moving of the clicked item to the done-list out-of-the-box. The only thing you need to do is wrap the logic in document.startViewTransition and the browser will figure out the animation for you.

Here’s what it looks like:

Animated moving of list items with view transitions

Can you imagine having to do this with JavaScript? You would have to get the position of the element in the todo-list, calculate the new position of the element in the done-list, calculate the animation, and then run it with JavaScript. With view transitions, this is almost a one-liner!

So how does this work?

As I explained, document.startViewTransition animates the changes between two DOM states: the one you’re starting from and the one you're transitioning to with the callback function passed to document.startViewTransition.

When an HTML element has the sameview-transition-name in both views or when two different HTML elements share the same view-transition-name in both views, the browser will animate the changes in size and position between the elements in both views.

This is how you can animate the moving of an element from one position to another or let one element morph into another. With view transitions, this is incredibly easy to do.

View transitions are represented in the DOM by pseudo-elements that take snapshots of the old and new views. You can target these pseudo-elements with CSS so you can customize the default animations that the browser provides with CSS animations to literally anything you want.

For an in-depth explanation of customizing view transitions, see my article below:

View Transitions: The Final Frontier For Web Apps?

Here’s how to make your web app look like a native app

itnext.io

The future is now

With the View Transitions API, web apps finally have a framework to implement compelling animations in an easy way where most of the heavy lifting is done by the browser. We can now animate page transitions and “local” transitions on the same page using only CSS animations that we’re already familiar with.

And the good news doesn’t stop there. This quiet revolution has already reached Chrome and Safari which now fully support view transitions for both single-page apps and multi-page apps and support in Firefox is on its way.

View transitions are a crucial step in making web apps more “app-like” and with this tool at your fingertips, crafting seamless animations is no longer a luxury reserved for experts, but something you can implement today.


Join my email list Modern Web Weekly, for a weekly update on Progressive Web Apps (PWA), Web Components and new features of the modern web platform, tested and explained in plain English.