Track 02 · Lesson 12 of 17Inside the browser

Track 02 · Inside the browser

The rendering pipeline

Lesson 12 of 17· 6 min

To draw a frame, the browser works out styles, then layout (sizes and positions), then paint (pixels), then composites the layers together. Changing a CSS property restarts the pipeline from a different stage. Pick a property to see how much work it causes.

What does each change cost?

Pick a property, then animate it

  1. DOM
  2. CSSOM
  3. Style (runs)
  4. Layout
  5. Paint
  6. Composite (runs)

Work per frame

Changing transform runs Style → Composite. The element is moved as a finished layer, often by the GPU. No layout, no paint. DOM and CSSOM are built when the page loads; they only rebuild when you change the HTML or stylesheets.

/* Cheap to animate */
.box {
  transition: transform 0.6s ease;
}
.box.moved {
  transform: translateX(220px) rotate(20deg);
}

The stages#

  1. Style: work out which CSS rules apply to each element.
  2. Layout: work out every element's size and position. Change one element's size and its neighbors may move too.
  3. Paint: fill in the pixels: text, colors, borders, shadows, images.
  4. Composite: stack the painted layers in the right order and show them. This step is often done by the GPU.

A change only restarts the pipeline from the first stage it affects. Change a width and everything runs again. Change a color and layout is skipped. Change a transform or opacity and the browser can go straight to compositing.

Layout thrashing#

Reading a layout value, like offsetHeight or getBoundingClientRect(), forces the browser to finish layout first. Write a style, read a size, write again, read again, all in a loop, and the browser has to lay out the page on every iteration. Batch your reads, then your writes.

Check yourself

Which CSS property is cheapest to animate?