Sub topic 1
Why rendering work gets expensive
Prerequisites
This chapter assumes you know CSS selectors, the box model, and positioning. You should also know how block and inline layout work, and you should have opened browser DevTools at least once. No JavaScript is needed here.
The three steps that cost time
When a page loads, the browser turns your HTML and CSS into pixels. Three steps in that work matter for this tutorial:
- Style. The browser matches selectors to elements and computes each element's final property values.
- Layout. The browser calculates the size and position of every box, using those computed values.
- Paint. The browser draws text, colors, borders, and images into layers.
The browser then composites the painted layers into the final image. Compositing is cheap compared with the other three, so this tutorial focuses on style, layout, and paint.
Why large pages are slow
By default, the browser assumes any element could affect any other. A change in one place may push boxes around elsewhere, so the browser often has to look at a large part of the page.
Three situations show the cost:
- First load. The browser must style, lay out, and paint every element, even those far below the viewport that the user may never reach.
- Small changes. Adding one item to a list can force layout to run again for many siblings and ancestors. Layout work grows with the number of affected boxes.
- Layout reads in scripts. Reading a value such as
offsetHeightright after changing a style forces the browser to run layout immediately. Doing this in a loop is called layout thrashing.
A page with a few thousand elements can take hundreds of milliseconds to render. On a slow phone, the same page can feel frozen.
The idea this tutorial builds on
Much of this work is wasted. A comment widget, a product card, or a chunk of an article rarely changes the layout of the rest of the page. The browser cannot know that unless you tell it.
CSS containment is how you tell it. The contain property promises that a subtree is independent of the rest of the page. The related content-visibility property goes further and lets the browser skip the work for content the user cannot see. Later chapters cover both in detail.
A quick way to see the cost
Open DevTools, go to the Performance panel, and record a page load. In the flame chart, look for the Recalculate Style, Layout, and Paint entries in the main thread. Their total width is the rendering cost you will try to reduce. Keep a note of these numbers now, because a later chapter compares them before and after adding containment.
Exercise
You append one item to a long list, and DevTools shows a long Layout task afterward. Why might layout be expensive even though only one element changed?
Show answer
By default the browser cannot assume the new item is independent of the rest of the page. The item's size may change its siblings' and ancestors' positions, so layout may need to be recalculated for many boxes. Containment is the way to tell the browser that this subtree is independent.