How Browser Rendering Works Behind the Scenes
In this article
When you open a webpage, your browser converts HTML, CSS, and JavaScript into a visual page through a multi-step process called rendering. The browser parses the HTML, builds a DOM tree, computes styles, creates a render tree, then paints pixels to the screen.
Background: The Rendering Engine
Every browser has a core component called a rendering engine. Chrome and Edge use Blink, Firefox uses Gecko, and Safari uses WebKit. The engine is responsible for turning raw text files into an interactive display. It works in a pipeline that runs on the main thread, meaning it processes tasks sequentially. Understanding this pipeline helps you debug layout issues, optimize performance, and explain why some pages feel slow.
Step 1: Parsing HTML and Building the DOM
When the browser receives HTML from the server, it starts parsing the bytes into tokens, then into nodes. These nodes form the Document Object Model (DOM), a tree structure that represents the page's content. Parsing happens incrementally: as soon as the browser sees a tag, it adds it to the tree and can start rendering parts of the page before the full HTML arrives.
Blocking resources, like external CSS and synchronous JavaScript, pause parsing. The browser must fetch and execute these before continuing, which is why scripts are often placed at the bottom of the body or loaded with defer or async.
Step 2: Parsing CSS and Building the CSSOM
CSS is parsed separately into the CSS Object Model (CSSOM). It is also a tree, but it contains style rules. The browser computes the final style for each element by matching selectors and applying the cascade (specificity and source order). This step is called style calculation. The CSSOM is render-blocking: the browser won't paint until it is built, because it needs to know how elements should look.
Step 3: Creating the Render Tree and Layout
The DOM and CSSOM are combined into a render tree. The render tree contains only visible elements – for example, display: none elements are excluded, but visibility: hidden elements are included (they are invisible but still take up space). Each node in the render tree has its computed style.
Next comes layout (also called reflow). The browser calculates the geometry: the size and position of every element in pixels. It starts from the root and works down, respecting the CSS box model (margin, border, padding, content). Layout is one of the most expensive operations, especially on complex pages.
Step 4: Paint and Compositing
After layout, the browser paints each render tree node – filling in colors, images, borders, and shadows. Painting is done in layers, not all at once. The browser can split the page into multiple layers (e.g., for elements with will-change or transform). These layers are then composited together on the GPU to produce the final image.
Compositing is efficient because it allows parts of the page to change without repainting everything. For example, animating transform only triggers compositing, not layout or paint, which is why it's smoother than animating top or margin.
Common Problems and Misconceptions
A frequent misconception is that display: none removes an element from layout. It does – the element is not in the render tree. But visibility: hidden keeps the space. Another issue is layout thrashing: reading layout properties (like offsetHeight) in a loop forces the browser to recalculate layout repeatedly, causing jank. To avoid it, batch reads and writes.
Another common problem is render-blocking CSS. Even a small CSS file delays first paint. Inline critical CSS and load the rest asynchronously to speed up initial rendering.
How to Optimize Rendering Performance
- Minify HTML, CSS, and JavaScript to reduce parse time.
- Use
deferorasyncfor scripts to avoid blocking parsing. - Set explicit dimensions on images to prevent layout shifts.
- Animate only
transformandopacityto trigger compositing, not layout. - Use CSS containment (
contain: layout) to isolate parts of the page.
Frequently Asked Questions
What is the difference between layout and paint?
Layout (reflow) calculates the position and size of elements. Paint fills in the visual details like colors and images. Layout happens before paint; a change in layout forces repaint, but a paint change (like color) doesn't trigger layout.
Why does a page flash unstyled content (FOUC)?
FOUC occurs when the browser paints HTML before the CSS finishes loading. This can happen if the CSS is loaded asynchronously or if there's a delay. To prevent it, ensure CSS is render-blocking (default) or use a loader that hides content until styles are ready.
Does JavaScript always block rendering?
No. JavaScript blocks parsing and rendering only if it is synchronous and placed in the HTML without defer or async. With defer, the script runs after parsing; with async, it runs as soon as it's fetched, but doesn't block parsing.
What is the critical rendering path?
The critical rendering path is the sequence of steps the browser takes to render the first meaningful paint: parse HTML, build DOM, parse CSS, build CSSOM, create render tree, layout, paint. Optimizing this path reduces time to first paint.
Why does changing a CSS class sometimes cause a whole page reflow?
If the class changes an element's dimensions or position, the browser must recalculate layout for that element and any affected ancestors or descendants. In worst cases, it recalculates the entire page. Use contain or change only compositor-friendly properties to limit the scope.