On a mid-range phone, a box sliding back and forth stutters, even though the code is just one small animation. Change one CSS property and the motion turns smooth. The key is the time each frame takes.
An animation feels smooth when the browser can draw 60 frames every second. That means each frame has to be done within 16.7 ms (1,000 ms divided by 60). When a frame takes longer, it shows late, and the motion stutters. That stutter is called jank. Picture making a flipbook, a little book whose pages you flip fast. If you redraw the whole scene from scratch on every page, you can't keep up. If the character is drawn once on a clear plastic sheet, and on every page you just slide the sheet along, you can. Redrawing is left, sliding the sheet is transform.
60 frames per second
An ordinary screen shows a new picture 60 times a second. In between, the browser has 16.7 ms to get the next frame ready: running the Style, Layout, Paint and Composite you know from the CSS & Rendering Pipeline lesson.
- A frame done in under 16.7 ms: it shows on time, the motion is smooth.
- A frame that needs 17 ms: it misses the screen's turn and only shows on the next one. If every frame does that, the animation only runs at 30 frames per second (30 fps).
- A 120 Hz screen has tighter turns: only 8.3 ms per frame.
left or transform, frame by frame
The Lab slides a box back and forth on a page with 1,500 elements. This is the cost of one frame (simulated numbers for a mid-range phone):
| Step | left |
transform: translateX() |
|---|---|---|
| Style | 0.5 ms | 0.5 ms |
| Layout | 12.0 ms | skipped |
| Paint | 4.0 ms | skipped |
| Composite | 0.5 ms | 0.5 ms |
| Total | 17.0 ms, over 16.7 ms, so 30 fps | 1.0 ms, 60 fps |
From 17.0 ms down to about 1 ms. left changes the position in the layout, so on every frame the browser works out the whole page's Layout again and repaints the area that changed. transform doesn't touch layout: the box is drawn once into its own layer, and on every frame the GPU just slides that layer.
The cost of Layout grows with the number of elements. In the Lab it's 0.008 ms per element: on a 600 element page, left only costs 9.8 ms and still makes it. On a 3,000 element page, left costs 29.0 ms. transform stays at 1.0 ms however many elements there are.
One more advantage: a CSS animation of transform and opacity can be run by the compositor, off the main thread. So it stays smooth even while the main thread is busy running JavaScript.
Code: a smooth sliding box
<style>
.box {
width: 100px;
height: 100px;
background: #2563eb;
will-change: transform;
animation: slide 2s ease-in-out infinite alternate;
}
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
</style>
<div class="box"></div>
@keyframes slidedefines the steps of the motion: from (from) to (to). The name is up to you, as long as it matches the one called inanimation.transform: translateX(200px)moves the box 200 px to the right visually. Its own place in the layout doesn't change.animation: slide 2s ease-in-out infinite alternateruns it: the name first, then a 2 second duration,ease-in-outeasing, repeating forever (infinite), back and forth (alternate).will-change: transformtells the browser up front that this element'stransformwill change. The browser gets its layer ready before the animation starts, so the first frame doesn't hitch.
The choppy version only differs in what's inside @keyframes:
.box {
position: relative;
animation: slide-bad 2s ease-in-out infinite alternate;
}
@keyframes slide-bad {
from { left: 0; }
to { left: 200px; } /* Layout and Paint on every frame */
}
Rules of thumb
| To make it | Use | Avoid |
|---|---|---|
| Move | transform: translate() |
left, top, margin |
| Grow or shrink | transform: scale() |
width, height |
| Rotate | transform: rotate() |
|
| Fade in or out | opacity |
visibility, display |
will-change: only where needed
Every layer takes GPU memory, roughly width × height × 4 bytes (and more again on phone screens with a high pixel density). A 100 × 100 px box is only 40 KB. But 200 cards of 400 × 300 px, all given will-change, need about 96 MB. A cheap phone can run out of memory, and the page gets slower instead.
- Put
will-changeonly on elements that really will be animated. - For an animation that happens rarely, add it with a class just before the animation and remove it afterwards.
- Never write
* { will-change: transform; }.
⚠️ Where does jank usually come from?
- Animating with
left,top,widthorheight. Every frame runs Layout and Paint. Move it withtranslateX()inside@keyframes. - Putting the animation name last.
animation: 2s slideis still valid CSS, but this exercise asks for the name first:animation: slide 2s .... Andanimation: nonemeans no animation. will-changeon every element. Too many layers use up GPU memory.- Reading sizes right after changing styles, again and again. Say, changing
style.leftand then readingoffsetWidthstraight away, inside a loop. The browser is forced to run Layout on every pass. This is called layout thrashing: do all the reads first, then all the changes.
TL;DR: A smooth animation needs every frame done within 16.7 ms (60 fps). left, top, width and height run Layout and Paint on every frame: on a 1,500 element page, left costs 17.0 ms and drops to 30 fps. transform and opacity only need Composite: 1.0 ms, however many elements there are. Write the motion in @keyframes with transform: translateX(), run it with animation: slide 2s ... (name first), and give will-change: transform only to elements that are really animated.
⚡ 16.7 ms per frame
Meaning: An animation feels smooth when the browser can draw 60 frames a second, so every frame has to be done within 16.7 ms. A late frame makes the motion stutter, which is called jank.
⚡ left or transform
Guess first: which properties are the safest to animate, because they only trigger Composite?
transform and opacity. In the Lab, one frame with left costs 17.0 ms and drops to 30 fps, while transform takes just 1.0 ms.
Meaning: left, top, width and height run Layout and Paint on every frame. transform draws the box once into its own layer, then the GPU just slides that layer along.
⚡ A smooth sliding box
Write:
.box {
will-change: transform;
animation: slide 2s ease-in-out infinite alternate;
}
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(200px); }
}
Meaning: The animation's name comes first. Give will-change only to elements that are really animated, because every layer takes GPU memory.