Inside the browser: from HTML bytes to pixels on the screen
A browser is one of the most complex programs on a computer: Chromium has tens of millions of lines of code, which puts it almost on the scale of an operating system. It launches dozens of processes, parses HTML and CSS, runs JavaScript with its own compiler, and draws the page on the graphics card 60–120 times per second. Let’s look at what happens inside when a page opens; the journey of a request across the network is described in the article on networking.
Many processes instead of one
Early browsers were a single process: if one tab hung, the whole browser hung, and a bug in decoding an image on any website could give an attacker access to everything. In 2008 Chrome split the browser into processes, and today all major browsers are built this way.
The main browser process draws the window, the tabs and the address bar, stores bookmarks, passwords and cookies, and is the only one with full access to files and the OS. Renderer processes — one per site — parse HTML and CSS, run JavaScript, and compute layout. The GPU process is the only one that talks to the graphics card. The network service performs all network requests. There are also utility processes: audio decoding, extensions, unpacking archives.
Since 2018, desktop Chrome has had site isolation enabled: pages from different sites (for example, your bank and an ad frame inside a news site) live in different processes. This was the response to the Spectre vulnerability: it turned out that JavaScript code can indirectly, by timing cache accesses, read any data in the memory of its own process. Therefore no one else’s data should be in that process — and different processes are separated by virtual memory at the hardware level.
The price is memory. Each renderer process takes tens to hundreds of megabytes, and a browser with 30 tabs easily consumes 3–6 GB. So browsers unload tabs that have not been opened for a long time, merge processes when memory is short, and compress inactive data.
See also: The operating system: who shares out the processor, memory and devices
The sandbox and inter-process messages
A renderer process executes foreign, potentially hostile code — the JavaScript of any website, the parsing of any images and fonts. So it runs in a sandbox: the OS forbids it almost everything. In Linux this is the seccomp-bpf system call filter plus separate namespaces; in Windows, a restricted access token, a low integrity level, and a ban on kernel graphics calls. A renderer process cannot open a file, create a network connection, or launch a program.
So how does it load images and submit forms? Through messages to the main process over an inter-process channel (in Chromium the system is called Mojo). “I need the resource https://example.com/logo.png” — the main process checks whether this site is allowed to make such a request and passes it on to the network service. The response comes back the same way, usually through shared memory so that large data does not have to be copied.
Hence the principle of defense in depth. To hack a computer through a web page, an attacker needs to find two bugs: one in the JavaScript engine or in content parsing — to run their own machine code in the renderer process — and a second one in the main process, the OS or a driver — to escape the sandbox. That is why companies pay researchers hundreds of thousands of dollars for such chains.
You can easily see the isolation for yourself: Chrome has a built-in “Task Manager” (Shift+Esc) that shows all browser processes and how much memory and CPU each one uses.
See also: The operating system: who shares out the processor, memory and devices
Parsing HTML and the DOM tree
HTML parsing happens in two stages. The tokenizer — a state machine with about 80 states — reads the text character by character and emits tokens: “opening div tag with a class attribute,” “text Hello,” “closing div tag.” The tree builder takes the tokens and builds the DOM (Document Object Model) — a tree of objects in memory: html → head and body → div → text node.
HTML is a remarkably forgiving language. The standard describes in detail what to do with any errors: unclosed tags, extra closing tags, a table inside a paragraph. So any HTML, however broken, produces the same tree in every browser. This is a deliberate decision: the web must be able to display pages from 1996.
Each DOM node is a C++ object inside the engine (Blink in Chrome, Gecko in Firefox, WebKit in Safari), from tens to hundreds of bytes in size. A large page has tens of thousands of nodes. JavaScript accesses them through wrappers: a call to document.querySelector is a transition from the JS world into the engine’s C++ code.
A <script> tag without attributes stops parsing: the script might call document.write and insert HTML right into the stream, so the parser is obliged to wait until it has loaded and executed. The defer attribute says “run after parsing,” async says “run as soon as it loads,” and ES modules behave like defer by default. Incorrectly included scripts are one of the main causes of slow page loading.
CSS: the cascade and style computation
Styles come from CSS files, <style> tags, style attributes, and the browser’s own styles (headings are bold by default). The parsed rules form the CSSOM. Until the styles have loaded, the browser does not paint the page — otherwise it would flash without any styling.
Next, for every DOM node, all matching rules must be found. Selectors are checked from right to left: for the rule “.menu a span” the engine first checks whether this is a span at all (most nodes are ruled out immediately), and only then walks up through the ancestors. Engines use hash tables keyed by class and tag, as well as Bloom filters, so that for each node only a handful of rules out of thousands need to be checked.
If several rules set the same property, the winner is decided by the cascade: importance (!important), origin, selector specificity (an ID beats a class, a class beats a tag), and then order in the file. Some properties (color, font) are inherited from the parent. The result is the computed style: for each node, several hundred properties with concrete values.
When a script changes an element’s class, the engine does not recompute the styles of the whole page: it marks the affected nodes as “dirty” and recomputes only them and, if necessary, their descendants. This invalidation is one of the trickiest parts of the engine: recompute too much and it is slow, too little and the picture is wrong.
The JavaScript engine: from text to machine code
JavaScript is executed by an engine: V8 in Chrome and Edge, SpiderMonkey in Firefox, JavaScriptCore in Safari. The code is first parsed into a tree (an AST), and lazily at that: the bodies of functions that have not yet been called are only quickly checked, and are fully parsed on their first call. Then the Ignition interpreter translates the function into compact bytecode and executes it.
While the code runs, the engine collects statistics: which functions are hot, which types arrive as arguments. Hot functions move up through compilation tiers: Sparkplug quickly translates bytecode into simple machine code, Maglev does so with optimizations, and for the hottest functions TurboFan builds heavily optimized x86-64 or ARM64 machine code tailored to the types it has seen.
The key trick is hidden classes. In JS you can add a property to an object at any moment, and naively every access to obj.x would require a dictionary lookup. V8 gives objects of the same shape a shared “map”: “x is at offset 12.” The place in the code where obj.x is read remembers the last map and offset (an inline cache). If the shape matches, the read costs one check and one load instruction, as in C.
Optimized code is built on assumptions: “there are always integers here.” If an assumption is violated (a string arrives), the engine performs deoptimization — it throws away the machine code and returns to the interpreter. JS memory is freed by the garbage collector: young objects are collected in a fraction of a millisecond, and a major collection runs mostly in parallel on background threads. Compilers, JIT and garbage collection are covered in detail in the article “Languages and runtimes.”
See also: Languages and runtimes: how program text becomes processor instructions · The processor core: an educational datapath and a modern core
The event loop and the frame budget
Every page has a main thread, and almost everything runs on it in turn: JavaScript, style computation, layout, paint recording. The work is managed by the event loop. It takes one task from the queue — a click handler, a timer, an incoming network response — runs it to completion, then all the microtasks (promise continuations), and then, if it is time for a frame, the update of the picture.
At a 60 Hz refresh rate a frame has 16.7 ms, at 120 Hz 8.3 ms. If a click handler computes for 300 ms, the page is unresponsive the whole time: it doesn’t update and doesn’t handle other clicks. Tasks longer than 50 ms are considered “long,” and the INP responsiveness metric, which Google has used to evaluate websites since 2024, considers a delay of up to 200 ms from an action to the next frame to be good.
JavaScript on a page is single-threaded not because that makes the engine easier to write, but because the DOM is not designed for simultaneous changes from several threads. Heavy computations are moved to a Web Worker — a separate thread with its own JS engine, without access to the DOM, that communicates with the page through messages.
The path of a click: the OS passes the mouse event to the browser’s main process, which determines the tab and forwards the event to that tab’s renderer process. If there are no handlers at the click location that could cancel scrolling, the compositor can respond by itself without waiting for the main thread. Otherwise the event is placed in the event loop’s queue.
Layout: where each box will end up
Knowing the styles is not enough: you have to compute where on the screen each element will be and how big it is. This is done by layout. The engine builds a layout tree — similar to the DOM, but without invisible elements (display: none) and with additional boxes (for example, for ::before). Then it traverses it recursively: the parent tells its children the available width, the children compute their sizes, and the parent positions them.
There are many layout rules: block (top to bottom), inline (text with line wrapping), flex, grid, tables, positioning. The most expensive part is text. A string has to be shaped: characters are turned into font glyphs, taking into account ligatures, kerning, and scripts in which letters change shape, as in Arabic. This is done by the HarfBuzz library. Then the text is broken into lines according to the box width, following line-breaking rules.
Layout depends on the window width, so when the window is resized almost everything is recomputed. A change to a single element can shift everything below it. Engines try to recompute only the affected subtrees and cache the results.
A common mistake in code is “layout thrashing”: a script in a loop changes an element’s style and immediately reads its size (offsetHeight). To answer honestly, the browser is forced to perform layout right away, and so on every iteration. If you first read all the sizes and then make all the changes, layout runs only once.
Paint recording
After layout, the geometry and style of every box are known, but there are no pixels yet. The paint stage walks the tree in stacking order and builds a list of drawing commands: “fill the rectangle 0,0–1280,80 with color #1E1E2E,” “draw the text ‘Hello’ in Inter 16 px at point 24,50,” “draw an image in a rectangle,” “shadow with an 8 px blur.”
Order matters: the background is painted before the text, and elements with a higher z-index are painted later. Stacking contexts create their own groups, inside which the order is computed separately — hence the classic puzzle of “why doesn’t z-index: 9999 help.”
The list of commands is not yet pixels but a description similar to a vector drawing. It can be rasterized at any scale: when the page is zoomed or on a high-pixel-density screen. The browser also reuses it if only one box has changed — only that part is re-recorded.
Paint is recorded on the main thread of the tab’s process, and the result is then handed to the compositor. From this moment the main thread is free for JavaScript, while the pixels are prepared by other threads and the graphics card.
The compositor and the graphics card
The compositor splits the page into layers. Elements that move independently get a layer of their own: scrollable areas, video, canvas, elements with transform and opacity animations, a fixed header. Each layer is divided into tiles — for example, 256 × 256 pixels — and only those tiles that are visible, or will soon become visible when scrolling, are rasterized.
Rasterization — turning commands into pixels — is performed on the graphics card through the Skia graphics library: rectangles and text outlines become triangles and shaders, and glyphs are taken from an atlas of already drawn characters. The graphics card computes millions of pixels in parallel, as at the Graphics card level in the model.
Then the GPU process assembles the frame: it overlays the finished tiles of all layers with their offsets and transparency, adds the browser interface, and hands the frame to the OS system compositor (DWM in Windows, WindowServer in macOS, the Wayland compositor in Linux), which combines the windows of all programs into the final image in the framebuffer.
The main benefit of this scheme is that scrolling and transform and opacity animations work without the main thread: the compositor simply moves the finished layers or changes their transparency, which is a few matrix operations on the graphics card. That is why a page scrolls smoothly even when JavaScript is busy. Animating the width or top property, by contrast, forces layout to be recomputed and the page repainted on every frame — hence the advice to animate transform.
So, the full frame pipeline: event → JavaScript → styles → layout → paint recording → layers → rasterization → frame assembly → screen. Optimizing a web page means making the browser go through as few stages as possible, as rarely as possible.
See also: Graphics card: how an image appears