The first lesson made a claim it didn't prove yet: transform and opacity can be animated in the composite step alone, with no layout and no repaint. Why? Because transform moves, turns or resizes the picture of an element without changing its place in the layout.
Think of a room photo stuck on a clear plastic sheet on top of the page. Sliding the sheet doesn't move any other text. If you move the photo inside the page itself, the browser has to arrange the page again, and with margin all the text below it flows again. top and margin move the photo inside the page, while transform only slides the sheet. That clear sheet is called a layer, and it is explained below.
Transform functions
A transform value holds functions, commands written with brackets:
| Function | Meaning | On Mrs. Sri's page |
|---|---|---|
translate(x, y), translateX(), translateY() |
move; percentages are of the element's own size | the card rising translateY(16px), the gallery translateX(-100%) |
scale(n) |
make bigger or smaller | the night button pressed scale(0.96) |
rotate(angle) |
turn | the gallery arrow rotate(180deg) |
skew(angle) |
slant | rarely used in UI |
The pivot point for turning and resizing is set by transform-origin. By default it is the middle of the element (50% 50%), so scale(0.96) on the night button shrinks it towards its middle. The countdown bar in the toast uses transform-origin: left, so that the bar shrinks towards the left.
The order of functions decides the result
One transform may hold several functions, and their order changes the result. Read the functions from right to left: the rightmost function is applied to the element first, then the result gets the function to its left. We put both orders on a 100 × 40 px box, in Chromium 147, WebKit 26.4 and Firefox 146. The last column is the bounding box: the smallest box that holds the element after the transform. It shows the box's left and top position in px:
transform |
What happens | Its bounding box |
|---|---|---|
translateX(50px) rotate(45deg) |
turned in place, then moved straight 50 px to the right | left 150.5, top 70.5 |
rotate(45deg) translateX(50px) |
moved 50 px first, then all of it is turned 45° around the pivot, so the move turns too: 35.4 px right and 35.4 px down | left 135.9, top 105.9 |
So the order of the functions in transform affects the final result.
translate, rotate, scale as properties of their own
Since Chrome 104, Firefox 72 and Safari 14.1, all three functions also exist as separate properties: translate, rotate and scale. Their order is always fixed: translate, then rotate, then scale, then transform. The browser treats them like one long transform written from the left in that order: transform: translate() rotate() scale() <the transform value>. Then the functions are read from right to left as usual: the transform value applies first, translate last. We measured it:
rotate: 45deg; transform: translateX(50px)is exactly the same astransform: rotate(45deg) translateX(50px).scale: 2; transform: translateX(50px)moves the box 100 px, because the move intransformis scaled up byscale.
The point: each motion gets its own property, so one motion doesn't overwrite another. An example is the room card (.room) below. It enters with translate, then grows a little with scale when it is picked:
.room { translate: 0 16px; opacity: 0;
transition: translate 300ms ease-out, opacity 300ms ease-out, scale 150ms ease-out; }
.room.in { translate: none; opacity: 1; } /* entering */
.room.selected { scale: 1.02; } /* picked, without removing its entry move */
If both motions were written through transform, the picked card's transform: scale(1.02) would replace the whole transform: translateY(16px) of the entry animation.
Measured: transform versus top
We recorded a Performance trace in Chromium 147 while the Terrace Room card rose 16 px in 300 ms, with the Attic Room and the Garden Room cards below it. The table counts how many times layout and paint ran. The numbers come from three recordings:
| The card rises through | Layout | Paint |
|---|---|---|
top: 16px to 0 |
35 to 37 times | 70 to 72 times |
margin-top: 16px to 0 |
37 to 39 times | 74 to 76 times, the two cards below move too |
transform: translateY(16px) to none |
once | twice, only at the start |
translate: 0 16px to none |
once | twice, only at the start |
transform + will-change: transform |
0 | 0 |
Through top, layout is computed 35 to 37 times, in almost every frame. Through transform, layout is computed only once. That one layout and two paints at the start happen while the browser moves the card onto a layer of its own.
Properties that only need composite
The composite step puts together layers: separate pictures that have already been painted, like the clear plastic sheet at the start of this lesson. When the card has its own layer, moving, turning or resizing it (transform) and changing its transparency (opacity) only means stacking that layer again with a new position or transparency. No pixel has to be repainted. The compositor thread can do that work without waiting for the main thread.
Other properties aren't so lucky. In the same trace:
- A thickening
box-shadow: 0 layouts but 74 paints, because the shadow has to be painted again in every frame. filter: blur(4px)toblur(0): 0 layouts and 6 paints, but style was recomputed 37 to 39 times (with transform only 7 to 9 times). Style is computed on the main thread, so the main thread still works in every frame. Blur is also heavy work for the GPU (the graphics chip), because every pixel is computed from many pixels around it.
will-change: the layer is ready before it starts
will-change: transform tells the browser that this element's transform is going to change. The browser gives the element a layer of its own right away, before the animation starts. The result in the trace: 0 layouts and 0 paints, compared with 1 layout and 2 paints at the start without will-change. So the first frame doesn't have to wait for a layer to be made.
The price is memory. Every layer is a bitmap (a picture made of pixels) in GPU memory, of width × height × 4 bytes per pixel. The Terrace Room card is 328 × 143 px. On a phone with a 3x screen (each CSS px is drawn with 3 × 3 screen pixels), its layer is 984 × 429 pixels × 4 bytes = about 1.7 MB. Chromium's layer data (LayerTree, which the Layers panel also uses) shows: 3 cards with will-change added 3 layers, and 30 cards added 30 layers, about 51 MB. will-change also creates a new stacking context, just like transform (remember the Z-Index lesson). The code below sets will-change on the room card (card) only while its animation runs:
card.style.willChange = 'transform'; // right before the animation
card.classList.add('in');
card.addEventListener('transitionend', () => {
card.style.willChange = ''; // release it once it's done
}, { once: true });
{ once: true } makes that listener run only once. Put will-change on an element that really is about to move, shortly before it moves, and release it afterwards. Not on every element.
⚠️ Five mix-ups about transform and layers
- Animating
top,left,width,heightormargin. Layout runs in almost every frame, and withmargin, everything below moves too. Usetransformortranslate. * { will-change: transform; }. Every element becomes a layer: tens of MB of GPU memory for one page. Use it only where needed, then release it.- Two
transforms overwriting each other. Thetransformin an:activerule replaces thetransformof the entry animation, it doesn't add to it. Split them intotranslate,rotate,scale. - Swapping the order of functions.
rotate(45deg) translateX(50px)moves diagonally, not straight. transformon an inline element. A<span>withdisplay: inlinecannot be transformed. Give itdisplay: inline-block.
TL;DR: transform (translate moves, scale resizes, rotate turns, skew slants) changes an element's picture without changing the layout (the position and size of every element). Percentages in translate are of the element's own size, and the pivot for rotate and scale is set by transform-origin. The order of functions affects the result. They are read from right to left, so translateX(50px) rotate(45deg) moves straight, while rotate(45deg) translateX(50px) moves diagonally. The translate, rotate, scale properties (Chrome 104, Firefox 72, Safari 14.1) work like transform: translate() rotate() scale() <the transform value> read from right to left, and don't overwrite each other. In Chromium 147, a card rising 16 px in 300 ms through top = 35 to 37 layouts and 70 to 72 paints (colouring pixels). Through transform = 1 layout and 2 paints at the start. With will-change: transform = 0 and 0. transform and opacity only need composite (putting together layers, separate pictures already painted). box-shadow is repainted every frame, and with filter, style is still recomputed every frame. will-change prepares the layer before the animation starts, but every layer takes memory on the GPU, the graphics chip (a 328 × 143 px card on a 3x screen is about 1.7 MB): set it briefly, then release it.
⚡ Move the picture, not the place
Write:
.room { translate: 0 16px; opacity: 0;
transition: translate 300ms ease-out, opacity 300ms ease-out, scale 150ms ease-out; }
.room.in { translate: none; opacity: 1; } /* entering */
.room.selected { scale: 1.02; } /* picked, without removing its entry move */
Meaning: The room card (.room) rises 16 px as it enters (translate), and grows 2% when it is picked (scale). translate and scale change an element's picture without changing its place in the layout (the position and size of every element), so other elements don't move. Because each motion has its own property, they don't overwrite each other.
⚡ The order of functions
Guess first: do translateX(50px) rotate(45deg) and rotate(45deg) translateX(50px) give the same result?
No. The functions are read from right to left. The first order turns the element in place, then moves it straight 50 px. The second order moves it first and then turns everything, so the move turns too.
Meaning: The order of functions in transform affects the final result.
⚡ top versus transform
Meaning: A card rising 16 px in 300 ms through top makes the browser compute layout 35 to 37 times, in almost every frame. Through transform, layout runs only once, at the start. So transform and opacity only need composite, the step that puts finished pictures together into one.
⚡ The layer is ready before it starts
Write:
card.style.willChange = 'transform'; // right before the animation
card.classList.add('in');
card.addEventListener('transitionend', () => {
card.style.willChange = ''; // release it once it's done
}, { once: true });
Meaning: will-change: transform makes the browser prepare a layer (a separate picture) for the card (card) before the animation starts, so no paint has to be repeated. But every layer takes GPU memory (the graphics chip's memory): about 1.7 MB for one card on a 3x screen. That's why this code sets it right before the animation and releases it when the transition ends.