Sub topic 14
From DOM and CSSOM to Pixels
This chapter assumes you know what the DOM is from the previous chapters, can read basic CSS, and have used browser DevTools. It covers the steps that turn parsed HTML and CSS into pixels on screen.
Building the CSSOM
While the parser builds the DOM, it also processes CSS from <link rel="stylesheet"> tags and <style> elements. The result is the CSSOM (CSS Object Model), a tree of style rules. The browser resolves cascade, specificity and inheritance, so each element ends up with a computed value for every property.
CSS blocks rendering. The browser will not paint until it has the CSSOM, because painting with partial styles would flash unstyled content. A slow stylesheet therefore delays the first paint.
The pipeline
The browser combines the DOM and the CSSOM into a render tree. It then runs three more stages.
- Render tree. It contains only what will be drawn. Elements with
display: noneand non-visual elements such as<head>are left out. An element withvisibility: hiddenstays in the tree, because it still takes up space. - Layout (also called reflow). The browser computes the size and position of every box, starting from the viewport width. Percentages,
emunits and line wrapping are resolved here. - Paint. The browser records drawing instructions for each box: backgrounds, borders, text and images. These are rasterized into bitmaps.
- Compositing. Some elements are placed on separate layers. The compositor, usually running on the GPU, stacks the layers in the right order and sends the result to the screen.
Why the stage matters
Changing a property restarts the pipeline at a certain stage, and every later stage runs again.
| Change | Earliest stage re-run | Cost |
|---|---|---|
| `width`, `margin`, `font-size` | Layout | High |
| `color`, `background-color`, `box-shadow` | Paint | Medium |
| `transform`, `opacity` | Compositing | Low |
This is why animations should use transform and opacity. Moving an element with left forces layout on every frame. Moving it with transform: translateX() usually only needs the compositor.
A common error: layout thrashing
Layout is normally batched. The browser waits until it must draw. But reading a layout value such as offsetHeight forces it to compute layout immediately. If you alternate reads and writes in a loop, it recomputes layout on every iteration.
const items = document.querySelectorAll('li');
// Slow: each iteration writes, then reads, forcing a layout each time.
items.forEach((item) => {
item.style.width = (item.parentElement.offsetWidth / 2) + 'px';
});
// Faster: do all reads first, then all writes.
const widths = Array.from(items, (item) => item.parentElement.offsetWidth / 2);
items.forEach((item, i) => {
item.style.width = widths[i] + 'px';
});How scripts interact with rendering
A synchronous <script> pauses HTML parsing, and it waits for any pending stylesheets, because the script might read computed styles. A stylesheet placed before a script can therefore delay both parsing and rendering. This is the reason for defer in the earlier example: the script runs after parsing finishes and does not block the parser.
Summary
The browser builds the CSSOM from the stylesheets and combines it with the DOM into a render tree. It then runs layout, paint and compositing. Cheaper changes skip the earlier stages. Avoid forced layouts by separating reads from writes.
Exercise
A menu slides in by animating its left property from -300px to 0. The animation looks choppy. Which pipeline stage is likely the cause, and what change would help?
Show answer
Animating left changes the element's position, so the browser re-runs layout on every frame, then paint and compositing. Animate transform: translateX() instead, from -300px to 0. This usually only needs the compositor, so layout and paint are skipped.