Sub topic 5
Customizing the animation with CSS
This chapter assumes you can write @keyframes animations, you know what a pseudo-element is, and you have already wrapped a DOM update in document.startViewTransition(). It also assumes you know the default cross-fade runs on the root snapshot. Here you will replace that default with your own CSS and learn which pseudo-element to target for each effect.
Where the default animation comes from
The browser ships a user-agent stylesheet that styles the transition pseudo-elements. You never wrote it, but it is ordinary CSS, so your own rules can override it. In summary, it does three things:
- It gives
::view-transition-old(*)a fade-out animation and::view-transition-new(*)a fade-in animation. - It gives every
::view-transition-group(*)a duration of about 0.25 seconds. - It sets
mix-blend-mode: plus-lighteron the old and new images, so the two layers add together during the cross-fade instead of dimming the page in the middle.
Your author styles beat user-agent styles. To change the animation, you select the same pseudo-elements and give them different animation values.
Overriding the cross-fade
The simplest customization changes the duration and easing of the old and new layers. You write your own keyframes and attach them to the pseudo-elements.
@keyframes fade-out { to { opacity: 0; } }
@keyframes fade-in { from { opacity: 0; } }
::view-transition-old(root) {
animation: 300ms ease-out both fade-out;
}
::view-transition-new(root) {
animation: 300ms ease-in both fade-in;
}The keyframes only name one side. fade-out has a to step but no from, so the animation starts from the element's current opacity, which is 1. The both fill mode keeps the first and last frames applied outside the animation's active time. Without it, the new image can flash at full opacity before it begins fading in.
A slide instead of a fade
You can use any animatable property on the old and new images. This example moves the outgoing view left while the incoming view slides in from the right.
@keyframes slide-out {
to { transform: translateX(-40px); opacity: 0; }
}
@keyframes slide-in {
from { transform: translateX(40px); opacity: 0; }
}
::view-transition-old(root) {
animation: 250ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 250ms ease-out both slide-in;
}Because the old and new images overlap while both are partly visible, plus-lighter can make the overlap look brighter than either image. If you see a flash of extra brightness, set mix-blend-mode: normal on both pseudo-elements.
Choosing the right pseudo-element
Each part of the tree has a different job. Choosing the wrong one is the most common reason a custom animation seems to do nothing.
| Selector | What it is | What to animate |
|---|---|---|
| `::view-transition-group(name)` | Container for one named element | Position and size (the browser generates this animation) |
| `::view-transition-image-pair(name)` | Wrapper for the old and new images | Rarely changed. It isolates the blend between its two children. |
| `::view-transition-old(name)` | Snapshot of the old state | Opacity, transform, clip-path, filter |
| `::view-transition-new(name)` | Live rendering of the new state | The same properties as the old image |
The argument is a transition name. The page as a whole is named root, and * matches every name. Rules that use (*) are a quick way to set one duration for the whole transition:
::view-transition-group(*) {
animation-duration: 350ms;
}Changing how a shared element moves
For elements that have their own view-transition-name, the group does the moving. The browser generates keyframes that go from the old box to the new box, covering transform, width, and height. You should adjust only the timing of that animation and leave the keyframes alone.
::view-transition-group(hero) {
animation-duration: 400ms;
animation-timing-function: ease-in-out;
}If you write animation: my-keyframes ... on a group, you replace the generated animation. The element then no longer travels from its old position to its new one. Use animation-duration, animation-timing-function, and animation-delay as separate properties to keep it.
A group's animation-duration also decides how long the whole named element stays in the transition, so keep it consistent with the images inside it.
Varying the animation per navigation
Pseudo-element rules are global to the document. The same ::view-transition-old(root) rule applies to every transition you start. If forward and back navigation should animate differently, mark the document before the transition starts and scope your selectors to that mark.
.slide::view-transition-old(root) {
animation: 250ms ease-in both slide-out;
}
.slide::view-transition-new(root) {
animation: 250ms ease-out both slide-in;
}function updateView() {
document.querySelector('#content').innerHTML = renderItem();
}
function navigateWithSlide() {
const root = document.documentElement;
root.classList.add('slide');
const transition = document.startViewTransition(() => updateView());
transition.finished.finally(() => root.classList.remove('slide'));
}The class sits on the html element, which is the originating element of the pseudo-element tree, so the selector matches. Remove the class when finished settles, whether the transition succeeded or was skipped. Otherwise it stays on and affects the next transition. Later chapters cover transition types, which solve the same problem without manual classes.
Gotchas and edge cases
- The pseudo-elements only exist during a transition. DevTools cannot show them on a static page. Slow the animation down or use the Animations panel to inspect them.
- Specificity applies normally. A rule such as
html::view-transition-old(root)beats a plain::view-transition-old(root). Keep your selectors consistent, or one will override the other. - Long durations feel slow. The page is visually frozen on the old snapshot at the start, so keep most transitions between 200 and 400 milliseconds.
animation: noneremoves the fade as well. If you set it on old and new, both layers swap instantly. This is useful when you animate from JavaScript, and a later chapter covers it.- Motion preferences still apply. A custom slide is motion. A later chapter shows how to switch it off for users who ask for reduced motion.
Exercises
Exercise
You write ::view-transition-group(hero) { animation: bounce 500ms; } and the card no longer moves from its old position to the detail position. Why, and how do you change the speed and easing safely?
Show answer
The animation shorthand replaced the browser-generated animation that moves the group from the old box to the new box. Set animation-duration and animation-timing-function on the group instead, so the generated keyframes stay in place.
Exercise
Write CSS that makes the old root snapshot slide 60px to the right while fading out over 200ms, with the new root snapshot fading in over 200ms.
Show answer
@keyframes out-right { to { transform: translateX(60px); opacity: 0; } } @keyframes in-fade { from { opacity: 0; } } ::view-transition-old(root) { animation: 200ms ease-in both out-right; } ::view-transition-new(root) { animation: 200ms ease-out both in-fade; }