What I Learned From Building View Transitions
My most important lessons
Mar 9, 2025
In the past couple of weeks, I recreated a number of demos from Nitish Khagwal and Leonel Ngoya that were built with React Motion to work with only view transitions.
You can find them online as codepens:
While building, I ran into many issues and gotchas that both improved my understanding of view transitions and also helped me to identify some patterns and best practices that I will share here now.
Transitioning different aspect ratios
One of the first issues I ran into was when transitioning elements with different aspect ratios, for example in the list stack demo where the height of the container needs to transition. Here’s what it looks like:
The transition works by setting the class expanded on the container through which CSS rules for the expanded state are applied. When this is wrapped in a call to document.startViewTransition, the change is animated but in the first version, the container would not transition correctly and the container would briefly be visible in the expanded and collapsed state:

View transitions for an HTML element are represented by a ::view-transition-grouppseudo-element that contains separate pseudo-elements for the old and new DOM state, in this case, the state for when the container does not have the expandedclass and when it does have this class.
For the <div> that has view-transition-name: container it looks like this:
::view-transition-group(container)
└─ ::view-transition-image-pair(container)
├─ ::view-transition-old(container)
└─ ::view-transition-new(container)
::view-transition-old(container) and ::view-transition-new(container) are absolutely positioned within their parent element and maintain their aspect ratio by default and since the default transition is a fade, both elements will be visible when this fading transition is roughly halfway.
By giving both pseudo-elements height: 100%, the issue is solved. Since this happens for all elements that have their dimensions transitioned I usually set this for all pseudo-elements for the old and new DOM states:
::view-transition-old(*),
::view-transition-new(*) {
height: 100%;
}
When you have this same issue when the width is transitioned you can set width: 100%as well.
Stretching elements
When transitioning elements with different aspect ratios, you often run into the issue of stretching elements, especially text. When an element containing text transitions into another element or state that is wider, the text will look stretchy as you can see in this screenshot from the list stack:

This can easily be fixed by setting width: fit-content on the old and new DOM states:
::view-transition-old(button-label),
::view-transition-new(button-label) {
/* prevents stretching of text */
width: fit-content;
}
Now the text looks good again.
Fading out unwanted elements
When recreating the help button demo I ran into multiple issues with elements that needed to be hidden for the transition to work correctly.
The button looks like this:
When the button was closed, I ran into the issue where the text under “Need Help?” was still visible when the button was closing so it would display outside the button:

The “Need Help?” label is transitioned back into the label of the button but the text that now displays outside of the button will be hidden so since it now is displayed for too long and is displayed outside the button, we might as well hide it earlier.
This can be done by defining a fade-out animation and applying that:
::view-transition-group(card-subtitle-1) {
animation-name: fade-out;
animation-duration: 0.1s;
}
Now the text is faded out in 0.1 seconds and the issue is fixed:

Fading in elements with a delay
When the button transitions into the form, a similar issue occurs with the text: it’s visible before the button has fully transitioned into the form:

Similar to fading out the text when transitioning from the form to the button, we can fade in the text when transitioning from the button to the form and when we add a small delay, we can make sure to only show the text when the button has fully transitioned into the form:
::view-transition-group(card-subtitle-1) {
animation-name: fade-in;
animation-duration: 1s;
animation-delay: .2s;
}
By making the duration of the fade-in transition (1 second) longer than the default duration of the animations (0.4 seconds) and adding a small delay, we can make sure that the text only fades in when the button has fully transitioned into the form and is not displayed out of bounds.
View transition types for different animations
In the previous example, there were different CSS rules defined for ::view-transition-group(card-subtitle-1) depending on if the text had to be faded in or faded out.
To make this work, we need to define these rules depending on whether the transition goes from the button to the form or the form to the button. This is where view transition types come in.
We can specify which view transition type we want to apply by providing the type or types as an argument to document.startViewTransition:
document.startViewTransition({
update: () => {
// update the DOM
},
types: ['to-card']
});
In this example, the button is transitioned to the card with the view transition type to-card. For this transition, the text needs to be faded in. We can specify this in CSS by using the :active-view-transition-type pseudo-class:
html:active-view-transition-type(to-card) {
&::view-transition-group(card-subtitle-1) {
animation-name: fade-in;
animation-duration: 1s;
animation-delay: .2s;
}
}
These CSS rules are now only applied when the view transition type to-card is active. In the same way, we can use the view transition type back-to-button to fade out the text:
html:active-view-transition-type(back-to-button) {
&::view-transition-group(card-subtitle-1) {
animation-name: fade-out;
animation-duration: 0.1s;
}
}
Applying shared styles with view transition classes
When you need to apply the same styles to many elements with different view-transition-names you can use view-transition-class instead of having to apply the same styles to many elements.
For the collection grid demo, I wanted to make the elements in the Subscriptions section slide down slightly when they were revealed. It looks like this:
There are four elements consisting of a title and a price that all have a different view-transition-name so that would mean I would need to apply the sliding-down animation to the ::view-transition-group of each element separately, so eight in total:
::view-transition-group(item-title-1),
::view-transition-group(item-price-1)
/* element 2 to 7 */
::view-transition-group(item-title-8),
::view-transition-group(item-price-8) {
animation-name: slide-down;
animation-delay: 0.2s;
animation-duration: 0.6s;
}
You can see how this can get unmanageable quickly if the number of items increases. But by applying a view-transition-class to all title and price elements we can set all needed styles at once. We wrap this in the block for the expand view-transition-type:
/* block for the "expand" view transition type */
html:active-view-transition-type(expand) {
/* apply the view-transition-classes */
.grid-item:nth-child(3).active {
.item-title {
view-transition-class: item-title-expand-slide-down;
}
.item-price {
view-transition-class: item-price-expand-slide-down;
}
}
/* apply the css to the view-transition-groups with
* the view-transition-class as the selector
*/
&::view-transition-group(.item-title-expand-slide-down),
&::view-transition-group(.item-price-expand-slide-down) {
animation-name: slide-down;
animation-delay: 0.2s;
animation-duration: 0.6s;
}
}
Now the slide-down animation is applied to all elements with the view-transition-class"item-title-expand-slide-down" and "item-price-expand-slide-down".
Lessons learned
These are some of the lessons and best practices I picked up while building view transitions. If you want to learn more about view transitions you should of course keep reading Modern Web Weekly as I write about them regularly, but I strongly advise you to build stuff with them. Try to recreate existing animations you see elsewhere and pay attention to details. This will teach you the most as you will run into things that you never would have thought of otherwise.
View transitions are still in heavy development and there are a lot of improvements coming up that I will write about. Stay tuned!