Sub topic 11
From HTML response to rendered page
This chapter assumes you can read an HTTP request and response, know what a URL is, and have seen basic HTML. It covers what the browser does after the response body arrives.
The browser starts before the download ends
A response with Content-Type: text/html is handed to the HTML parser. The parser reads bytes as they arrive and does not wait for the whole file. It turns the text into the Document Object Model (DOM), a tree of objects where each element is a node.
When the parser meets a tag that points to another file, such as <link rel="stylesheet">, <script src>, or <img>, the browser sends a new HTTP request for that URL. A relative URL like /app.css is resolved against the page's URL first. One page therefore usually means many requests, often to several hosts.
The rendering pipeline
- DOM. The parser builds the tree of elements.
- CSSOM. The browser parses CSS into a similar tree of style rules.
- Render tree. The DOM and CSSOM combine. Elements with
display: noneare left out. - Layout. The browser computes the size and position of each box.
- Paint and composite. It draws the boxes into layers and combines the layers on screen.
What blocks rendering
CSS blocks rendering. The browser avoids showing unstyled content, so it waits for stylesheets before it paints.
A plain <script src="app.js"> blocks the parser. The browser must download and run the script before it continues, because the script might change the document. Two attributes change this:
| Attribute | Download | Runs |
|---|---|---|
| none | Pauses parsing | Immediately, in order |
| `defer` | In parallel | After parsing finishes, in order |
| `async` | In parallel | As soon as it arrives, in any order |
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
<link rel="stylesheet" href="/app.css">
<script src="/app.js" defer></script>
</head>
<body>
<h1>Hello</h1>
<img src="/logo.png" alt="Logo" width="120" height="40">
</body>
</html>Setting width and height on images lets the browser reserve space. Without them, the layout shifts when the image arrives, and the browser must run layout again.
Common errors
- Missing
<!doctype html>. The browser falls back to quirks mode, which uses old layout rules. - Wrong
Content-Type. If the server sends HTML astext/plain, the browser shows the source text instead of a page. - Missing charset. Without
charset=utf-8in the header or a<meta charset>tag, non-ASCII characters can appear garbled. - A script that reads the DOM too early. A script in the head without
deferruns before the body exists, sodocument.querySelectorreturnsnull. - Blocked or failed subresources. A 404 on a CSS file does not stop the page. It loads without those styles. Check the status of each request, not only the first one.
JavaScript changes the DOM
Scripts can modify the DOM after the first render. The browser then repeats the needed layout and paint steps. Pages built this way fetch data with fetch and insert it into the tree. Viewing the page source shows the original HTML response. Inspecting the elements in DevTools shows the current DOM, and the two can differ a lot.
Exercise
A page loads with no styles, and the browser's network panel shows /app.css returned status 404. Is the HTML response at fault? Why does the page still display?
Show answer
No. The HTML response was fine. The browser sent a separate request for /app.css, and that request failed. A failed stylesheet does not stop parsing or rendering, so the browser renders the DOM with default styles. The fix is to correct the stylesheet URL or the server path.
Exercise
A script in <head> runs document.querySelector("h1").textContent = "Hi" and throws an error. Name two ways to fix it.
Show answer
Add defer to the script tag so it runs after parsing. Alternatively, move the script to the end of <body>, after the h1 element.