Computing Foundations › How the Internet Works › Day 20

Day 20: How Browsers Render: HTML, CSS, and JavaScript

Day 20 of 365 — How Browsers Render: HTML, CSS, and JavaScript

After this lesson you will be able to explain what a browser does after it receives a page — parsing HTML into the DOM and CSS into the CSSOM, building the render tree, laying out and painting — and use that pipeline to reason about page speed, client-side versus server-side rendering, and how interactive front-ends update the page.

Course
Computing Foundations
Category
How the Internet Works
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-020-how-browsers-render-html-css-and

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-020-how-browsers-render-html-css-and
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: the HTML source compared with the live DOM


Historical background


What it is — and what it is not

Diagram: HTML, CSS, and JavaScript layered over one page element, showing structure, style, and behavior

LanguageIts one jobExampleWithout it
HTMLStructure and content<h1>Hello</h1> marks a top-level headingNothing to show
CSSPresentationh1 { color: navy; }Content shows, unstyled
JavaScriptBehaviourChange text on a clickThe page cannot respond

It is not:


Why it was created and what problems it solves

Diagram: a page built up from HTML through CSS to JavaScript


How it works

From bytes to a tree: the DOM

The second tree: the CSSOM

Combining them: render tree, layout, paint

Diagram: how the DOM and CSSOM combine into the render tree

Flowchart: the browser rendering pipeline from HTML and CSS to the painted page

StepInputOutputWhat it decides
Parse HTMLHTML bytesDOM treeWhat exists and how it nests
Parse CSSCSS from all sourcesCSSOMWhich styles apply where
Render treeDOM + CSSOMRender treeWhat is visible, with its styles
LayoutRender treeBox geometryExact size and position of every box
PaintPositioned boxesPixelsFinal colours, text and images

JavaScript, blocking, and the event loop

Diagram: a blocking script compared with a deferred one


An everyday analogy

Diagram: an image without declared dimensions compared with one that has them

Printing a newspaper.

The newspaperThe browser
The manuscriptHTML
The style guideCSS
The editor making live changesJavaScript
The marked-up copyThe DOM
Deciding column widths and page breaksLayout
Ink on paperPaint
One typesetter, one job at a timeThe event loop

Examples in practice

Diagram: how a long task blocks the event loop


Implications: security, privacy, performance, scalability, and cost

Diagram: interleaved DOM reads and writes compared with batched ones


Alternatives: free, open source, and commercial

Diagram: moving computation from the main thread to a web worker

ToolTypeWhat it gives you
Browser developer toolsBundled, freeThe DOM, the styles, the timeline, all live
LighthouseFreeA graded audit of the rendering path
React / Vue / SvelteFree, open sourceDeclarative UI on top of the same DOM
Streamlit / GradioFree, open sourceAn AI demo without writing front end at all
Web workersFree, built inMove heavy work off the main thread
Puppeteer / PlaywrightFree, open sourceDrive a real browser, so scripts have run

TermWhat it actually is
DOMThe live tree of the page, as it currently is
CSSOMThe parsed, resolved styles
Render treeVisible nodes with their computed styles
Layout / reflowComputing every box’s size and position
PaintTurning boxes into pixels
Event loopOne-task-at-a-time scheduling on the main thread
Critical rendering pathEverything that must finish before first paint

When to use it — and when not to

Diagram: inserting untrusted input as markup compared with as text


The AI connection

Diagram: appending streamed text compared with re-rendering the whole panel


Knowledge check

quiz:
  question: "A scraper fetches a page with `curl` and finds an empty container where the browser clearly shows a populated table. What explains the difference?"
  options:
    - "The table is built by JavaScript into the DOM, and curl retrieves only the original HTML"
    - "The server returns different HTML depending on the User-Agent header"
    - "The table is styled with CSS that curl cannot apply"
    - "The response was compressed and curl did not decompress the body"
  answer_index: 0
  explanation: "The source HTML and the rendered page are different things. curl fetches bytes; it never runs scripts, so anything a script inserted into the DOM exists on screen and nowhere in what curl received."

Hands-on exercise

Watch the pipeline run, then break each stage on purpose. Worked through fully in the Day 20 lab directory.

What you are doingWhere
Compare source with rendered DOMView source, then the Elements panel
Edit the DOM liveDouble-click a node in Elements and change its text
See computed stylesThe Styles and Computed panes
Watch the pipelineThe Performance panel, record a reload
Find render-blocking resourcesThe Network panel, and Lighthouse
Freeze the page deliberatelyRun a long loop in the Console

Expected output

Parse HTML       12 ms
Recalculate Style 4 ms
Layout            7 ms
Paint             3 ms
[blocked] main thread busy 4200 ms

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. Which pairing of web language to job is correct?

  1. HTML is structure, CSS is presentation, JavaScript is behavior
  2. HTML styles the page, CSS adds behavior, JavaScript defines structure
  3. HTML defines behavior, CSS defines structure, JavaScript defines presentation
  4. All three define presentation; only their syntax differs
Show answer

Answer: A. HTML is structure, CSS is presentation, JavaScript is behavior

The three web languages divide the work cleanly: HTML marks up structure and content, CSS controls how it looks, and JavaScript makes it respond to the user.

Q2. What is the DOM?

  1. The original HTML text file exactly as the server sent it
  2. A live tree built from the HTML that scripts can change
  3. A compressed copy of the page kept for faster downloads
  4. The list of CSS rules that apply to the page
Show answer

Answer: B. A live tree built from the HTML that scripts can change

The Document Object Model is a live, in-memory tree the browser builds by parsing the HTML; because scripts can add, remove, and edit nodes, the DOM can differ from the original source text.

Q3. What does the browser build by parsing the page's CSS?

  1. The DOM tree for the document
  2. The render tree used for painting
  3. The CSSOM, mapping rules to elements
  4. The event loop that schedules tasks
Show answer

Answer: C. The CSSOM, mapping rules to elements

Parsing CSS produces the CSSOM (CSS Object Model), which stores the computed styles; the browser later combines it with the DOM to form the render tree.

Q4. Which sequence correctly orders the critical rendering path?

  1. Layout, paint, parse HTML, build render tree
  2. Build the render tree, parse HTML, paint, layout
  3. Paint, layout, build the render tree, parse HTML
  4. Parse HTML and CSS, build the render tree, layout, paint
Show answer

Answer: D. Parse HTML and CSS, build the render tree, layout, paint

The browser first parses HTML and CSS into the DOM and CSSOM, combines them into the render tree, computes geometry in the layout step, and finally paints the pixels.

Q5. An element is styled with display: none. What happens to it?

  1. It stays in the DOM but is not drawn
  2. It is removed from the DOM entirely
  3. It is painted but placed off-screen
  4. It causes a parsing error in the browser
Show answer

Answer: A. It stays in the DOM but is not drawn

display: none keeps the node in the DOM but drops it from the render tree, so it takes no space and is never painted — existence in the DOM and visibility on screen are different things.

Q6. Why can a large JavaScript file placed in the page's <head> delay when content first appears?

  1. JavaScript files are always larger than HTML files
  2. An ordinary script is render-blocking: the parser stops to run it
  3. The browser refuses to paint any page that contains JavaScript
  4. CSS cannot load until all JavaScript has finished
Show answer

Answer: B. An ordinary script is render-blocking: the parser stops to run it

When the parser reaches an ordinary <script> tag it must run it before continuing, because the script might change the HTML being parsed; a big script in the head therefore blocks DOM construction and delays first paint.

Q7. Why can a single slow JavaScript function make an entire page freeze?

  1. Slow functions disconnect the page from the network
  2. The browser deletes the DOM while a function runs
  3. JavaScript runs on one main thread, so a long task blocks it
  4. The CSSOM must be rebuilt on every function call
Show answer

Answer: C. JavaScript runs on one main thread, so a long task blocks it

The event loop processes work one task at a time on the same main thread that handles layout and paint, so one long-running task blocks everything until it finishes — clicks and scrolling pile up unhandled.

Q8. How do client-side and server-side rendering differ?

  1. Client-side rendering never uses JavaScript; server-side rendering never uses HTML
  2. They are two names for the same technique
  3. Server-side rendering only works without CSS
  4. Client-side builds content with JavaScript; server-side sends HTML
Show answer

Answer: D. Client-side builds content with JavaScript; server-side sends HTML

With server-side rendering the server sends ready-made HTML and content shows quickly (and is easy for crawlers to read); with client-side rendering the browser assembles the content from JavaScript, which is cheaper to host but appears later and can be harder to index.

Glossary

HTML
HyperText Markup Language, the language that describes a web page's structure and content by marking text as headings, paragraphs, lists, links, and other elements.
CSS
Cascading Style Sheets, the language that describes a page's presentation — colors, sizes, spacing, and layout — kept separate from its structure.
JavaScript
The programming language that runs in the browser to give a page behavior: responding to clicks, updating content, and talking to servers without reloading.
DOM
The Document Object Model, a live tree of nodes the browser builds from the HTML; every element is a node, and scripts can add, remove, or change nodes to update the page.
CSSOM
The CSS Object Model, the tree the browser builds by parsing all the page's CSS, recording the computed style that applies to each element.
render tree
The tree the browser builds by combining the DOM with the CSSOM, containing only the visible elements together with their computed styles — the list of things that will actually be drawn.
layout
The pipeline step, also called reflow, in which the browser computes the exact size and position of every element in the render tree.
reflow
Another name for layout; also used for the re-computation of geometry the browser must do when something changes an element's size or position after the page has loaded.
paint
The final rendering step, in which the browser fills in the pixels — text, colors, borders, and images — for the laid-out elements.
critical rendering path
The sequence of steps from receiving HTML and CSS to painting the first pixels; its length largely determines how fast a page feels.
event loop
The mechanism by which the browser runs JavaScript one task at a time from a queue on the main thread, running each task to completion before updating the screen and taking the next.
client-side rendering
Building a page's content in the browser with JavaScript after a nearly empty HTML file loads; cheap to host but slower to first paint and harder for crawlers to read.
server-side rendering
Assembling the finished HTML on the server for each request so content appears quickly and is easy for search engines to index, at the cost of more work per request.
render-blocking resource
A file, typically CSS or an ordinary script, that the browser must finish processing before it can paint, delaying when the user first sees content.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.