The Rendering Pipeline
The Rendering Pipeline
Section titled “The Rendering Pipeline”Introduction
Section titled “Introduction”The browser’s rendering pipeline converts HTML, CSS, and JavaScript into pixels on the screen. Understanding each stage helps you write performant code.
Pipeline Stages
Section titled “Pipeline Stages”flowchart LR A["JavaScript\n(DOM/CSS changes)"] --> B["Style\nCalculation"] B --> C["Layout\n(Reflow)"] C --> D["Paint\n(Repaint)"] D --> E["Compositing"]1. Style Calculation
Section titled “1. Style Calculation”The browser computes which CSS rules apply to each element and calculates their final styles.
// Accessing computed stylesconst style = getComputedStyle(element);console.log(style.color); // 'rgb(255, 0, 0)'
// Changing a class triggers style recalculationelement.classList.add('highlight');2. Layout (Reflow)
Section titled “2. Layout (Reflow)”The browser calculates the position and size of each element. This is the most expensive stage.
// Reading layout properties forces a synchronous layoutconst width = element.offsetWidth; // Forces layoutconst height = element.offsetHeight; // Uses cached layout
// Batch reads togetherconst rect = element.getBoundingClientRect();// Then batch writeselement.style.width = '100px';element.style.height = '200px';3. Paint (Repaint)
Section titled “3. Paint (Repaint)”The browser fills in pixels — colors, backgrounds, borders, text.
// Changes that only trigger paint (no layout)element.style.color = 'red';element.style.backgroundColor = '#f0f0f0';element.style.boxShadow = '2px 2px 4px black';4. Compositing
Section titled “4. Compositing”The browser layers painted elements and composites them on the GPU.
// Changes that only trigger compositing (fastest!)element.style.transform = 'translateX(100px)';element.style.opacity = '0.5';Pipeline Optimization
Section titled “Pipeline Optimization”| Stage | Triggers | Cost |
|---|---|---|
| Style | Class changes, inline styles | Low |
| Layout | Width, height, position, font | High |
| Paint | Color, background, shadow | Medium |
| Compositing | Transform, opacity | Low 🚀 |
Summary
Section titled “Summary”- The pipeline runs in order: Style → Layout → Paint → Composite
- Layout (reflow) is the most expensive stage
- Compositing-only changes (transform, opacity) are cheapest
- Batch read/write operations to avoid forced layouts