Sub topic 1
Why Rendering Work Gets Expensive
This chapter explains the work a browser does to turn HTML and CSS into pixels, and why that work grows with page size. Later chapters build on this model to show how CSS containment limits the work.
Prerequisites
This chapter assumes you already know:
- CSS selectors, the box model, and basic positioning.
- How block and inline layout work, and what
overflowdoes. - How to open browser DevTools and record a performance trace.
No JavaScript is needed here beyond reading a short snippet.
The pipeline in brief
After the browser parses HTML and CSS, it runs a series of steps to put pixels on screen. The steps that matter for this tutorial are style, layout, and paint.
- Style (also called style recalculation) works out which CSS rules apply to each element and computes the final value of every property. The result is the computed style of each element.
- Layout works out the size and position of every box. It turns computed styles such as
width,display, andfloatinto geometry. - Paint records how each box should be drawn: backgrounds, borders, text, and shadows. The browser then rasterizes these records into pixels. It often does this on separate layers that are composited together.
Each step takes the output of the one before it. A change early in the pipeline usually forces the later steps to run again. Changing a width in CSS can trigger style, layout, and paint. Changing only a background-color can skip layout and trigger just style and paint.
Why cost grows with the page
Each step does work for every element it has to consider. A larger DOM gives the browser more to do.
- Style cost depends on the number of elements and on how many rules the browser must match against them. A change to a class on a high-level element can force the browser to re-check every descendant.
- Layout cost depends on the number of boxes and on how much they depend on one another. Boxes in normal flow depend on their siblings and parents, so a change in one place can move many others.
- Paint cost depends on how much area must be redrawn and how complex it is. Large shadows, filters, and many overlapping layers add up.
A page with 500 elements may render in a few milliseconds. A page with 50,000 elements, such as a long feed, a big table, or a generated document, can spend hundreds of milliseconds on one render. A frame at 60 frames per second has about 16 ms. A render that takes much longer than that makes scrolling and input feel slow.
The cost of the first render
When a page loads, the browser must style, lay out, and paint the whole document before it can show a correct result. It does this even for content far below the viewport, because the layout of the content above can depend on it. For example, the total page height, and therefore the scrollbar size, depends on every box.
This is the main reason long pages load slowly. Most of the content is not visible, yet the browser still pays to render it. The content-visibility property, covered in later chapters, targets exactly this cost.
The cost of updates
Rendering is not only a load-time cost. Every time the page changes, the browser may have to redo part of the pipeline. The change might come from a script, an animation, a hover state, or a new item added to a list.
The browser tries to limit the work. It tracks which elements are "dirty" and revalidates only those. The difficulty is that the browser cannot assume much about how elements affect each other. CSS allows many features whose effects reach outside an element.
- A child with
position: absolutecan be placed relative to an ancestor far up the tree. - Content can overflow its parent and overlap other boxes.
- A CSS counter or a
quotesvalue set high in the tree changes content far below it. - A change to an element's height can push every following sibling down.
Because of these possibilities, a small change can force the browser to re-run layout over a large part of the tree, sometimes the whole document. This is often called layout invalidation. When script reads a layout value such as offsetHeight right after a write, the browser must run layout immediately instead of waiting until the next frame. That is called a forced synchronous layout, and repeated in a loop it is a common cause of jank.
const items = document.querySelectorAll('.item');
// Slow: each iteration writes, then reads, forcing layout every time.
items.forEach((item) => {
item.style.width = '200px';
console.log(item.offsetHeight);
});
// Better: do all writes first, then all reads.
items.forEach((item) => {
item.style.width = '200px';
});
items.forEach((item) => {
console.log(item.offsetHeight);
});Batching reads and writes helps, but it does not reduce the amount of work one layout pass must do. That is the problem containment addresses.
The missing information
The browser is conservative because it lacks guarantees. Suppose one widget in a large page changes. The browser cannot know, without checking, whether that change affects:
- the size of the widget's parent,
- the position of the widget's siblings,
- styles that use counters or other inherited effects,
- what gets drawn outside the widget's box.
If you, the author, know that the widget is self-contained, you have information the browser does not. CSS containment is the way to pass that information on. With it, the browser can limit style, layout, and paint work to the widget's subtree and can skip the rest of the page.
Spotting the problem
You can tell that rendering is a bottleneck by looking for these signs.
| Symptom | Likely rendering cause |
|---|---|
| Slow first paint on a long page | Style and layout for the entire document |
| Scrolling stutters | Heavy paint, or layout triggered during scroll |
| A small update causes a long task | Wide layout invalidation across many elements |
| Typing or clicking feels delayed | Script triggering forced synchronous layout |
In the DevTools Performance panel, rendering appears as purple "Recalculate Style" and "Layout" blocks and as green "Paint" blocks. Wide or frequent blocks show where the browser spends time. A later chapter shows how to record and compare traces before and after a change.
Common misunderstandings
- "Only visible elements cost anything." Elements below the fold still need style and layout unless you tell the browser otherwise. Elements with
display: noneare skipped, but that also removes them from the page. - "Paint is the expensive part." For large pages, style and layout are often larger costs. Measure instead of guessing.
- "Fewer elements always fixes it." Reducing the DOM helps, but you often cannot remove content. Containment lets you keep the content and reduce the work.
What comes next
The next chapter introduces the idea of containment itself. It shows how declaring a subtree independent gives the browser permission to stop checking the rest of the page when something inside changes.
Exercise
A script updates the text of one small badge in a page with 30,000 elements, and DevTools shows a long Layout block. Why might the browser lay out far more than the badge, and what kind of information from you would let it do less?
Show answer
The browser cannot assume the badge is independent. Its size change might move siblings or alter its parent's size, so it may re-run layout over a large part of the tree. If you declare the badge's subtree self-contained, which is what CSS containment does, the browser can limit style, layout, and paint work to that subtree.
Exercise
Which of these changes can skip the layout step: changing an element's width, or changing its background-color? Explain why.
Show answer
Changing background-color can skip layout. It does not alter any box's size or position, so only style and paint need to run. Changing width alters geometry, so layout must run, followed by paint.