How Browser Renders Web Page: Full Process
In this article
When you open a web page, the browser goes through a multi-step process — parsing HTML, building the DOM, applying CSS, and painting pixels — to display the page. This article explains each stage in the order it happens, with concrete examples and common pitfalls.
Background: The Browser's Job
A browser's core job is to take a URL and turn it into a visual, interactive page. This involves two main parts: the network layer (fetching resources) and the rendering engine (turning those resources into pixels). The rendering engine is the part that parses HTML and CSS, constructs the page structure, and draws it to the screen. Different browsers use different engines — Chrome and Edge use Blink, Firefox uses Gecko, Safari uses WebKit — but the fundamental rendering pipeline is similar across all of them.
The Rendering Pipeline: Step by Step
Here is the high-level sequence a browser follows for a typical page load:
- Parse HTML — The browser reads the raw HTML bytes and converts them into a tree of nodes called the DOM (Document Object Model).
- Parse CSS — It processes CSS (both external stylesheets and inline styles) to build the CSSOM (CSS Object Model).
- Create the Render Tree — The DOM and CSSOM are combined to form a tree of visible elements, each with its computed style.
- Layout (reflow) — The browser calculates the exact position and size of every element in the render tree.
- Paint (rasterization) — It converts each element into actual pixels on the screen.
- Compositing — Layers are combined and displayed, often using the GPU for smooth scrolling and animations.
This pipeline is not strictly one-way; the browser may re-run parts of it when the page changes (e.g., when JavaScript modifies the DOM).
Parsing HTML and Building the DOM
HTML parsing is the first major step. The browser reads the HTML file byte by byte, tokenizes it, and builds a tree of nodes. Each tag becomes an element node, text becomes a text node, and attributes become properties of the element. For example, <div id="main">Hello</div> creates a div element with an id attribute and a child text node containing "Hello".
During parsing, the browser also discovers external resources like stylesheets, scripts, and images. It starts downloading them in parallel, but script execution blocks parsing unless the script is marked async or defer. That's why placing scripts at the end of the body is a common performance tip.
CSS Parsing and the CSSOM
CSS parsing is similar to HTML parsing but produces a different tree: the CSSOM. Each rule in a stylesheet is matched against elements in the DOM. The browser calculates the final style for each element, considering inheritance, cascade, and specificity. For instance, if you have p { color: blue; } and .highlight { color: red; }, a paragraph with class highlight will be red because a class selector has higher specificity than a type selector.
The CSSOM is also where the browser applies default styles (like the user-agent stylesheet) and any custom properties. This step is critical because incorrect or conflicting CSS can cause unexpected layout results.
Render Tree, Layout, and Paint
After both trees are ready, the browser combines them into the render tree, which contains only visible elements. Elements with display: none or visibility: hidden are excluded (though visibility: hidden still takes up space, so it's in the layout but not painted).
Next comes layout (also called reflow). The browser calculates the geometry: width, height, x/y position, and how elements flow relative to each other. For example, a block element like a div takes the full width of its parent by default, while an inline element like span sits within the text flow.
Finally, paint converts the render tree into pixels. The browser fills in colors, draws borders, renders text, and applies effects like shadows. Modern browsers break the page into layers (e.g., for elements with transform or opacity) and composite them together, which allows the GPU to handle animations efficiently.
JavaScript and Dynamic Changes
JavaScript runs after parsing (unless it's inline and blocks), and it can modify the DOM, CSSOM, or both. When that happens, the browser may need to re-run parts of the pipeline. For example, if you change an element's height via JavaScript, the browser must recalculate layout and then repaint that area. This is why frequent DOM manipulations can cause performance issues — each change triggers a reflow and repaint.
To mitigate this, developers use techniques like batching DOM reads/writes, using requestAnimationFrame, or leveraging the Virtual DOM in frameworks like React to minimize direct DOM changes.
Common Problems and Misconceptions
One common misconception is that the browser renders the page as soon as it receives the HTML. In reality, it streams and parses incrementally, so the user may see partial content before all resources are loaded. Another issue is render-blocking CSS: external stylesheets delay the first paint because the browser needs the CSSOM to build the render tree. Similarly, JavaScript at the top of the page blocks parsing until it executes.
Another frequent problem is layout thrashing — when JavaScript reads layout properties (like offsetHeight) and then writes styles in a loop, forcing multiple reflows. A simple fix is to read all needed values first, then write changes in a batch.
Frequently Asked Questions
What is the difference between the DOM and the render tree?
The DOM represents the entire HTML structure, including elements that are hidden or not rendered. The render tree only contains visible elements that will be painted on screen. For example, a script tag is in the DOM but not in the render tree.
Why does a page sometimes show a blank white screen before loading?
This usually happens because the browser is waiting for CSS or JavaScript that blocks rendering. If the HTML is small but the CSS is large, the browser may not paint anything until the CSS is parsed. Similarly, if a script is at the top without async, it blocks the HTML parsing and thus the first paint.
What is 'critical rendering path'?
It refers to the sequence of steps the browser must complete to render the first frame: parsing HTML, building the DOM, parsing CSS, building the CSSOM, creating the render tree, layout, and paint. Optimizing this path (e.g., minifying CSS, inlining critical CSS) speeds up page load.
Does the browser render HTML from top to bottom?
Yes, the HTML parser reads the document from top to bottom, but it may pause when it encounters a script that must be executed. It also starts downloading resources (images, CSS) as soon as they are encountered, not necessarily in order, but the parsing itself is sequential.
What is the role of the GPU in rendering?
The GPU is used for compositing layers and accelerating certain operations like CSS transforms, opacity changes, and scrolling. When you animate an element with transform: translateX(), the browser can move the layer on the GPU without re-layouting, making the animation smooth.