Sub topic 5
Writing your own page animations
This chapter assumes you have already added @view-transition { navigation: auto; } to every page that should take part. It also assumes you can write @keyframes and know that the browser builds a tree of view-transition pseudo-elements during a navigation. Here you replace the default crossfade with your own animation and learn which mistakes make custom animations flicker or do nothing.
The working model
When a transition starts, the browser takes a snapshot of the old page and renders the new page. It then builds an overlay of pseudo-elements that sits above the real document. The overlay is made of images, not live elements, so you can only style it with CSS. You cannot run script against the pieces, and links inside the snapshot do not work.
For the whole page, the tree looks like this:
The name root belongs to the document element, which is captured by default. Two of these pseudo-elements are the ones you will style most:
::view-transition-old(root)is the snapshot of the page you are leaving.::view-transition-new(root)is the rendering of the page you are arriving at.
The argument is required. ::view-transition-old without a name is invalid, and the rule is dropped. You can pass a view-transition-name value or * to match every name.
The browser's built-in stylesheet already animates these two. The old image fades out and the new image fades in over about 250 ms. Your CSS replaces those built-in animations through the normal cascade, so a plain animation declaration on the pseudo-element is enough.
Replacing the crossfade
This example moves the old page out to the left while fading, and brings the new page in from the right:
@keyframes slide-out {
to {
opacity: 0;
transform: translateX(-40px);
}
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(40px);
}
}
::view-transition-old(root) {
animation: 200ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 300ms ease-out both slide-in;
}Each keyframe block gives only one end state. The missing end uses the element's current style, which is fully opaque and untransformed. That is why slide-out needs only a to and slide-in needs only a from.
The both fill mode is not optional here. The old animation lasts 200 ms and the new one lasts 300 ms. Without both (or at least forwards on the old animation), the old snapshot would jump back to full opacity at 200 ms, while the new snapshot is still partly transparent. You would see a flash of the old page.
The transition ends when the last animation on any of its pseudo-elements finishes. The longest duration therefore sets the total time.
Blend mode: a common surprise
The default stylesheet gives the old and new images mix-blend-mode: plus-lighter. That mode adds the colors of the two layers together. It makes a crossfade look smooth, because the two opacities always add up to 1.
It works badly when the two layers are not complementary. Suppose the old image stays fully opaque underneath while the new image is revealed on top. Plus-lighter then adds the colors in the overlap and the page looks washed out. Set the blend mode back to normal in that case:
@keyframes reveal {
from {
clip-path: inset(0 0 100% 0);
}
to {
clip-path: inset(0 0 0 0);
}
}
::view-transition-old(root),
::view-transition-new(root) {
mix-blend-mode: normal;
}
::view-transition-old(root) {
animation: none;
}
::view-transition-new(root) {
animation: 400ms ease-out both reveal;
}Here the old snapshot stays still and fully visible because its animation is none. The new page is uncovered from the top downward with clip-path. The old snapshot is removed when the transition ends.
The slide example above also overlaps two partly transparent layers with different timings. If you see bright bands or a flash of extra brightness in the middle of a slide, add the mix-blend-mode: normal rule there too.
Which properties to animate
The snapshots are images, so the cheapest animations are the ones the browser can run without re-laying out the page.
| Property | Notes |
|---|---|
| `opacity` | Cheap. The basis of most transitions. |
| `transform` | Cheap. Use it for slides, scales, and rotations. |
| `clip-path` | Good for wipes and reveals. |
| `filter` | Works, such as `blur()`, but large blurs cost more on big pages. |
| `width`, `height`, `top`, `left` | Avoid on `old` and `new`. The browser already animates the size and position of each named group for you. |
Prefer a few simple properties over many complicated ones. The snapshot covers the whole viewport, so heavy effects run on a large area.
Where the CSS lives
The overlay is built and animated in the page you are arriving at, so that page's styles control the animation. Keep the transition rules in one shared stylesheet that every page links. If two pages carry different copies of the rules, the transition changes depending on which page is the destination, and that is hard to debug.
Both pages must still contain the @view-transition rule. If only one of them has it, no transition runs and nothing reports an error.
Rules that apply to a single name
Selectors are matched by name. A rule for root does nothing to an element that has its own view-transition-name, because that element is pulled out of the root snapshot and gets its own group. The * argument matches every name:
::view-transition-old(*),
::view-transition-new(*) {
animation-duration: 150ms;
}A rule like this is a convenient way to speed up every transition at once. Name-specific rules still win when they have equal or higher specificity and come later. How to name elements and morph them between pages is the next chapter's subject.
Keep it short
While a transition runs, the page underneath does not respond to clicks, scrolling, or typing, because the overlay covers it. A long animation therefore feels like a slow site. Durations from 150 ms to 400 ms are typical, and anything over about 500 ms is hard to justify on ordinary navigations.
You should also let users turn the movement off. The accessibility chapter covers prefers-reduced-motion in detail. The short version is that a media query can set animation: none on all the view-transition pseudo-elements.
Common errors and how to spot them
- Nothing animates. One of the two pages is missing
@view-transition, or the navigation is not eligible, for example a cross-origin link. Check both pages first. - The old page flashes back before the new one appears. The old animation has no
bothorforwardsfill mode and ends before the new one does. - Colors look too bright in the middle. The default
plus-lighterblend mode is adding two overlapping layers. Setmix-blend-mode: normal. - The selector is ignored. The pseudo-element has no name argument, or the name does not match any captured element.
- A rule in one page's stylesheet only works in one direction. The animation uses the destination page's CSS. Move the rules into a shared file.
- The transition feels sluggish. Total time equals your longest animation. Shorten it and remove delays.
Exercise
You write ::view-transition-old(root) { animation: 150ms ease-in slide-out; } and ::view-transition-new(root) { animation: 400ms ease-out both slide-in; }. Users report a brief flash of the old page about 150 ms into each navigation. What is the cause, and what is the fix?
Show answer
The old animation has no fill mode, so at 150 ms it ends and the old snapshot returns to its normal full opacity while the new snapshot, which lasts 400 ms, is still partly transparent. Add both (or forwards) to the old animation so it holds its final state, for example animation: 150ms ease-in both slide-out;.
Exercise
You want the new page to be wiped in from the top with clip-path while the old page stays fully visible underneath. After you set animation: none on the old snapshot, the overlap region looks washed out. Which declaration fixes this, and on which pseudo-elements should it go?
Show answer
mix-blend-mode: normal, set on both ::view-transition-old(root) and ::view-transition-new(root). The default plus-lighter mode adds the colors of two overlapping layers, which brightens the area where the opaque old image sits under the new image.