Sub topic 12
Performance: what to animate and why
This chapter assumes you can write @keyframes, attach them with animation-timeline: scroll() or view(), and read a simple animation-range. It explains why some scroll-driven animations stay smooth and others stutter, and how to choose properties that keep them smooth.
Why scroll-driven animation can be fast
Before scroll-driven animations, scroll-linked effects were built with a scroll event listener. The listener ran on the main thread. It read scrollTop, calculated a value, and wrote a style. The main thread also runs your JavaScript, style calculation and layout, so a busy page made the effect lag behind the scroll.
A CSS scroll timeline is declarative. The browser knows the animation is a function of scroll position, so it can update the animation itself. In Chromium, an animation on a property that the compositor can handle runs off the main thread. The compositor is a separate part of the browser that moves already-painted layers around on the GPU. The animation then stays in step with scrolling even while the main thread is busy.
That benefit depends on which properties you animate. The rest of this chapter is about that choice.
The rendering pipeline in brief
To turn CSS into pixels, the browser runs up to four steps:
- Style: work out which rules apply to each element.
- Layout: work out the size and position of every box.
- Paint: draw the pixels for each box into layers.
- Composite: combine the layers on screen, applying transforms and opacity.
A property change forces the browser to restart at the earliest step it affects. Changing width restarts at layout, which then forces paint and composite. Changing background-color restarts at paint. Changing transform or opacity can often skip straight to composite.
With a time-driven animation this cost is paid once per frame for the animation's duration. With a scroll-driven animation it is paid on every scroll movement, and the user controls how fast that happens. Fast flicks and trackpad scrolling can produce many updates in a second.
Properties by cost
| Property group | Earliest step | Typical cost during scroll |
|---|---|---|
| `transform`, `opacity` | Composite | Lowest. Can run off the main thread. |
| `filter`, `backdrop-filter` | Composite or paint | Depends on the filter. Blur on a large area is expensive. |
| `color`, `background-color`, `box-shadow`, `border-color` | Paint | Medium. Runs on the main thread and repaints. |
| `width`, `height`, `margin`, `padding`, `top`, `left`, `font-size` | Layout | Highest. Re-lays out the page, then repaints. |
| `clip-path` | Paint | Medium. Depends on the shape and the element size. |
Treat the table as a guide rather than a guarantee. Browsers differ, and the set of properties that run on the compositor changes between versions. Check the browser you target with its developer tools instead of assuming.
Replace layout properties with transforms
The reading progress bar is the standard example. A first attempt often animates width:
@keyframes grow-width {
from { width: 0; }
to { width: 100%; }
}
.progress-slow {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
animation: grow-width linear both;
animation-timeline: scroll(root block);
}This looks correct and works. But every scroll step changes the bar's width, so the browser runs layout and paint for it on the main thread. Animate scaleX instead:
@keyframes grow {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
.progress {
position: fixed;
inset: 0 0 auto 0;
height: 4px;
background: rebeccapurple;
transform-origin: left;
animation: grow linear both;
animation-timeline: scroll(root block);
}Two details matter here. transform-origin: left makes the bar grow from the left edge. Without it, scaleX scales from the center. The animation-timeline declaration comes after the animation shorthand, because the shorthand resets animation-timeline to auto.
The same translation applies elsewhere:
- Use
translateX()ortranslateY()instead of animatingleftortop. - Use
scale()instead of animatingwidthandheight. - For a fill indicator, scale a full-size element instead of resizing a box.
- To reveal content, animate
opacityandtransformtogether, as in therevealkeyframes used withview().
Scaling distorts the element's content, including text and rounded corners. This is fine for a solid bar. For anything with text or borders, use translate and opacity, or accept the cost of a layout property for that one element.
Fixed elements and hidden costs
The .progress bar is position: fixed, which keeps it in its own region and avoids reflowing the page when it changes. Be aware of two side effects of transforms:
- An element with a
transform(other thannone) becomes the containing block for itsposition: fixedandposition: absolutedescendants. A fixed child inside an animated card will no longer be fixed to the viewport. - It also creates a new stacking context, which can change which elements draw on top of others.
The reveal keyframes end with transform: none. That is deliberate. After the animation finishes, and with fill-mode: both holding the final frame, the element has no transform and no longer acts as a containing block. If you ended on translateY(0), the element would keep a transform and these side effects would remain.
will-change: use it narrowly
will-change: transform asks the browser to promote an element to its own compositor layer ahead of time. For a scroll-driven animation the browser usually promotes the element once the animation is active, so you rarely need it.
Adding it everywhere has costs. Each layer uses GPU memory, and many large layers can make the page slower. If you add it, apply it to the few elements that animate, and remove it from elements that are not on screen or whose animations have finished.
.progress {
will-change: transform;
}Measure before and after. If you cannot see an improvement, remove it.
Many elements, many animations
A view() timeline is created for each element that uses it. A page with a few dozen cards is fine. A list with thousands of items, each with its own view timeline, creates many animations that the browser must update on every scroll step.
Ways to keep this under control:
- Keep keyframes small. One or two compositor-friendly properties per element is a good budget.
- Use
animation-rangeso that each animation is only active for the part of the scroll it needs. Outside its range, the element holds a fixed value, andfill-mode: bothkeeps that value in place. - Avoid animating the whole of a very long list. Apply the effect to items in a featured section instead.
- Do not combine a scroll-driven animation with a JavaScript
scrolllistener that reads layout (for examplegetBoundingClientRect()) on every event. This forces layout on each event and defeats the point.
Easing and performance
Easing does not change cost, but it changes how the result feels. Scroll-driven animations should usually use linear. The position of the animated element then tracks the user's finger or wheel directly. A curve such as ease-in-out makes the element speed up and slow down relative to the scroll, which can look like lag. Use non-linear easing only when you want that deliberate effect.
Measuring in the browser
Use the tools to confirm a change helps:
- Open the Performance panel in DevTools, start recording, scroll the page, and stop.
- Look at the main thread. Long "Recalculate style", "Layout" or "Paint" blocks that line up with scrolling mean your animation is paying for those steps.
- Turn on paint flashing in the rendering options. Areas that flash green while you scroll are being repainted. A progress bar that flashes on every scroll step is animating a paint or layout property.
- Throttle the CPU (for example 4x slowdown) and scroll again. Compositor-driven animations should stay smooth under throttling, while main-thread ones will visibly lag.
A reviewed example
This card reveal applies the guidance in the chapter. It animates only opacity and transform, uses linear easing, limits the range, and ends with no leftover transform.
@keyframes reveal {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
@supports (animation-timeline: scroll()) {
.card {
animation: reveal linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
}
}
}If you replaced translateY(40px) with top: 40px and added a box-shadow animation, the effect would look similar but cost more on every scroll step.
Common mistakes
- Animating
width,height,toporleftfor progress bars and indicators. - Animating
box-shadowor largefilter: blur()values on many elements. - Leaving a non-
nonetransform on the final keyframe and breaking fixed or absolute children. - Putting
will-changeon every element. - Using non-linear easing and then judging the animation as laggy.
- Testing only on a fast desktop. Test with CPU throttling, and on a real mid-range phone if you can.
Exercise
A page has a sidebar indicator that animates height from 0 to 100% using a scroll timeline. Scrolling is janky on slow devices. Rewrite the keyframes so that the animation uses a compositor-friendly property, and say what else you must add to the rule so it looks right.
Show answer
Use a full-height element and animate transform: scaleY() instead of height:
@keyframes fill {
from { transform: scaleY(0); }
to { transform: scaleY(1); }
}
Add transform-origin: top so the indicator grows downward from the top edge instead of from its center. Keep animation-timeline after the animation shorthand, and use linear easing with both fill. Changing height forces layout on every scroll step, while scaleY only needs compositing.
Exercise
Why does the reveal keyframes block end with transform: none instead of translateY(0), and what problem does that avoid?
Show answer
A computed transform other than none makes the element a containing block for position: fixed and position: absolute descendants, and it creates a stacking context. With fill-mode: both the last keyframe stays applied after the animation, so translateY(0) would keep those side effects. transform: none removes them once the card is fully revealed.