How a Browser Turns Code Into Pixels
Follow the rendering pipeline from HTML and CSS bytes to the screen, and learn which kinds of change force layout, paint or only compositing.
Key takeaways
- HTML becomes the DOM and CSS becomes the CSSOM, and the browser combines them before anything styled can appear.
- Layout computes sizes and positions, paint fills in pixels and compositing assembles layers.
- Stylesheets block rendering, and classic scripts without defer or async block HTML parsing.
- JavaScript, style, layout and much of paint compete for the same main thread.
- Animating transform and opacity is usually cheaper than animating properties that change layout.
A browser turns code into a picture in stages. It parses HTML into a tree of elements, parses CSS into rules, works out which styles apply to what, calculates where everything goes, paints it, and assembles the painted layers on screen. JavaScript can change any of it, sending parts of the process around again.
Knowing the order explains most front-end performance advice: why stylesheets block display, why some animations stutter and why one line of script can make a whole page recalculate.
The pipeline at a glance
Different browser engines implement these stages with their own optimizations and names, but the model holds broadly.
Parsing HTML into the DOM
The server's response arrives in pieces, and the browser starts parsing before it has everything. It decodes bytes into characters, splits them into tokens such as start tags and text, and builds the Document Object Model (DOM), a tree in which each element is a node with parents and children.
The HTML parser is forgiving. An unclosed paragraph or a missing tag is repaired according to rules in the HTML standard, so broken markup still produces a tree, sometimes not the one the author intended.
While the main parser works, a preload scanner looks ahead for files referenced in the markup, such as images and scripts, and requests them early. Resources that only appear after script runs are invisible to it, which is one reason they load late.
Parsing CSS into the CSSOM
Stylesheets are parsed into the CSS Object Model. Unlike HTML, CSS can't be applied usefully until the browser has it all, because a later rule may override an earlier one. For that reason, stylesheets in the head are render-blocking: the browser waits for them before painting, to avoid showing unstyled content.
A stylesheet with a media condition that doesn't currently apply, such as print styles, is still downloaded but doesn't block the first render.
Where scripts fit
A classic script tag stops HTML parsing: the browser fetches the file and runs it before continuing, because the script might write into the document. And if stylesheets before it are still loading, the script waits for them, since it may ask about styles.
For most site code, defer or a module script is a sensible default. async suits independent scripts. A module marked async runs as soon as it and its imports are ready. See taming render-blocking resources and who does what among HTML, CSS and JavaScript.
Style calculation
For every element, the browser finds all matching selectors and resolves conflicts using the cascade: origin and importance, cascade layers, specificity and source order. Inherited properties such as text color pass down from parents. Relative values like em become concrete ones.
The browser then builds what's often called the render tree, containing what will actually be displayed. An element with display: none is left out. One with visibility: hidden stays, taking up space without being seen. Generated content from pseudo-elements such as ::before is added.
Layout
Layout (also called reflow) determines the geometry: how wide each box is, where lines of text wrap, where flex and grid items land. It depends on the viewport, fonts and content, and a change to one box can ripple through its neighbors, parents and children.
If an image has no declared dimensions, the browser lays out the page without knowing its size and moves things when it arrives. This is a main source of layout shift, measured by Core Web Vitals. Setting width and height attributes lets the browser reserve space.
Paint and compositing
Paint produces drawing instructions: backgrounds, borders, text, shadows, images, in the correct stacking order. These are then rasterized into pixels, often in tiles and often with help from the graphics processor.
Some parts of the page are placed on separate layers. Compositing combines the layers into the frame you see. Moving or fading an existing layer doesn't require layout or repaint, and this work can often run off the main thread.
Layers aren't free: each consumes memory. Hints such as will-change help in specific cases and hurt when applied everywhere.
A small worked example
Take a page with one stylesheet in the head, a heading, a photo and a deferred script. The browser receives the first chunk of HTML and begins building the DOM. The preload scanner spots the stylesheet, image and script and requests all three.
Parsing continues while they download. Nothing is painted yet, because the stylesheet hasn't arrived. Once it has, the browser builds the CSSOM, computes styles, lays out the page and paints the heading. If the photo is still in transit, the browser leaves room for it, provided its dimensions were declared.
HTML parsing finishes, the deferred script runs, and the browser fires the DOMContentLoaded event. Suppose the script adds a notice at the top of the page. That changes geometry, so styles and layout are recalculated and the content below moves. The photo arrives, gets decoded and is painted into its reserved box. When all initial resources are in, the load event fires.
Change one detail and the story shifts. Remove the defer attribute and put the script in the head, and the heading can't appear until that file has downloaded and run. Leave out the image dimensions, and the text jumps when the photo lands. Both are one-line mistakes with visible effects.
Accessibility tree
Alongside the visual pipeline, the browser builds an accessibility tree from the DOM and styles. Screen readers and other assistive technology use it to learn each element's role, name and state. Real buttons, headings, lists and form labels populate it correctly for free. A clickable div looks like a button on screen and means nothing in this tree unless you add the missing semantics and keyboard behavior yourself.
What a change costs
When something changes after the first render, the browser redoes only what it must.
Screens refresh many times a second. At 60 refreshes per second, each frame has under 17 milliseconds, shared between your script and the browser's own work. Miss that window and the animation stutters.
The main thread
JavaScript, style calculation, layout and much of painting happen on one main thread per page context. While a long script runs, the browser can't respond to a click or draw a new frame.
A particular trap is forced synchronous layout. If a script changes a style and then immediately reads a measurement such as offsetHeight, the browser must run layout right then to answer. Doing that repeatedly in a loop is known as layout thrashing:
// Slow: each pass writes, then forces layout to read
for (const box of boxes) {
box.style.width = container.offsetWidth + "px";
}
// Better: read once, then write
const width = container.offsetWidth;
for (const box of boxes) {
box.style.width = width + "px";
} Other helpful habits: use requestAnimationFrame for visual updates, split long tasks, move heavy calculation to a web worker, and keep the DOM from growing without bound. Properties such as content-visibility let a browser skip work for offscreen sections.
Fonts and images
Text may wait for a web font. The font-display descriptor controls whether the browser hides text briefly, shows a fallback or skips a late font; see web font loading. Images must be downloaded and decoded into pixels, and decoding a very large image costs memory and time even if it's shown small.
Watching it happen
The Performance panel in developer tools records a timeline of parsing, script, style, layout and paint. Record a page load or an interaction, then look for long tasks, repeated layout entries and frames that overrun. Rendering options can flash repainted areas and outline layout shifts. Tool names vary by browser; developer tools basics is a gentle start.
Test with processor throttling, since a fast computer hides most of these costs. Why websites load slowly covers the network side that comes before rendering.
Common questions
Is the DOM the same as the HTML file?
No. The DOM is the live tree the browser built from it, including repairs and any changes made by scripts. "View source" shows the file, while the elements panel shows the DOM.
Do all browsers render a page identically?
Mostly, for standard features, but small differences in fonts, defaults and support remain. Test in more than one engine.
Why does my page flash unstyled text or content?
Usually styles or fonts arrived after something was painted, often because a stylesheet is injected by script or loaded in an unusual way. Check the load order in the network panel.