The Web FrontendVol. 2 · Part II
0 / 0

Shelves / The Web Frontend

FE·00

Start Here: How the Web Works

For readers who have never built a web page: what happens when you open a website, what HTML, CSS and JavaScript each do, the tools you need, your first three pages built and run step by step, how to read browser errors, and how to study the rest of this volume.

14 topics · ~14 min read · 15-question quiz
Updated Suggest a correction

Start here: how to use this volume

Two reading paths

This volume covers the whole of frontend engineering, from your first HTML tag to how browsers schedule rendering work and how large teams ship and monitor web apps. It's written for two kinds of reader at once:

  • If you're new to the web (or to programming), read this chapter first, then read each chapter's Start here section in order. Each one assumes only what earlier Start here sections taught. Type every example into a file and open it in your browser: you learn the web by watching what the browser does with your code. Take each quiz; come back to the deeper sections when the basics feel easy.
  • If you already build web apps, skip this chapter. Start each chapter with its Revision sheet at the end. If you can explain every line on it, take its quiz and use How deep to go to choose which internals sections and lab experiments to read.

Volume 1 (Foundations of Computing) is useful background, especially its chapters on networking (9) and data representation (6), but you don't need it to start here: every idea this volume relies on is explained when it first appears.

Mastery levels

Every chapter uses the same six levels, so you always know how deep you've gone:

Level Name You can… Example for a button
L1 Recognition define it and spot it "That's a <button> element"
L2 Working use it correctly write a button that does something when clicked
L3 Internals explain why it works explain how the click becomes an event the browser delivers to your code
L4 Debugging diagnose failures find why the click handler never runs
L5 Design choose between approaches decide between a <button> and a link, or between frameworks
L6 Production run it at scale keep the page fast, accessible and secure for millions of users

A beginner's first pass through this volume aims at L2 everywhere and L3 in the core chapters (HTML, CSS, JavaScript, the DOM). That's already enough to build real sites.

What happens when you open a website

Clients and servers

The web is a conversation between two kinds of program:

Interactive diagram: How one page load travels, and where the time goes. It needs JavaScript; the text around it says the same.
  • A client asks for things. Your browser (Chrome, Firefox, Safari, Edge) is the client.
  • A server answers. It's a program running on a computer somewhere, often in a data centre, waiting for requests.

When you type https://example.com/menu and press Enter, the browser finds the server's address (a DNS lookup, like looking up a phone number), opens a secure connection, and sends a short text request: "GET /menu". The server sends back a response: a status ("200 OK", or "404 Not Found") and a body, which for a web page is an HTML document. The browser reads that document, notices it needs more files (styles, scripts, images), requests each of those too, and builds the page you see.

Browser (client) builds the pageyou see Server sends filesand data GET /menu 200 OK + HTML document GET /style.css, /app.js CSS and JavaScript files GET /menu.json (from JS) JSON data
One page load is many request–response round trips between your browser and servers

A typical news site makes 50–150 such requests for one page. Frontend engineering is largely about what the browser does with those responses, and making that fast, correct and usable for everyone.

Frontend and backend

  • The frontend is everything that runs in the browser: the page's structure, its look, and its behaviour. That's this volume.
  • The backend is everything that runs on servers: storing data, checking passwords, talking to databases. That's Volume 3.
  • A full-stack engineer works on both. The line between them is the network: the frontend can only learn things from the backend by sending requests.

The three languages of the browser

Browsers understand three languages, and each has one job:

HTML what is on the pageheadings, paragraphs,links, images, formsthe frame and rooms CSS how it lookscolours, fonts, spacing,layout, animationpaint and furniture JavaScript what it doesclicks, data, updates,talking to serverswiring and switches
HTML is the structure, CSS is the presentation, JavaScript is the behaviour
  • HTML (HyperText Markup Language) describes what the content is: "this is a heading", "this is a list", "this is a button". It isn't a programming language; it has no loops or variables. Chapter 13.
  • CSS (Cascading Style Sheets) describes how it looks: "headings are dark orange", "these cards sit in a grid of three columns". Chapter 14.
  • JavaScript is a full programming language that makes the page do things: respond to clicks, fetch data, update what's shown. Chapter 15; TypeScript (chapter 16) adds type checking on top of it.

Keeping the three separate is the oldest good habit on the web: the same HTML can be restyled by changing only the CSS, and it still works (more slowly, less prettily) if the JavaScript fails to load.

What the browser does with them

Roughly, in order (chapter 18 covers the real details):

  1. Parse the HTML into a tree of objects in memory called the DOM (Document Object Model). Every element becomes a node you can find and change from JavaScript.
  2. Parse the CSS and work out which rules apply to which elements (the "cascade").
  3. Lay out the page: compute every box's size and position.
  4. Paint pixels and show them.
  5. Run JavaScript, which can change the DOM, and every change can trigger steps 2–4 again for the affected parts.

The DOM, not the HTML file, is what you see. JavaScript can add, change and remove elements after the file arrives, so "View Source" (the original file) and the Elements panel in DevTools (the live DOM) often differ.

Your toolbox

Tools you need

Tool What it's for Choose
A modern browser with DevTools running and inspecting pages Chrome or Firefox (both have excellent DevTools; Edge is Chrome's engine)
A code editor writing files VS Code (free); it highlights HTML/CSS/JS and has a live-preview extension
A local web server serving files over http://localhost python3 -m http.server (already installed on Mac/Linux) or npx serve
Node.js (LTS) running JavaScript outside the browser, and the build tools in later chapters the LTS release from nodejs.org, or via nvm/fnm
A terminal running servers and tools the one from Volume 1, chapter 1

Open DevTools with F12 (or Ctrl+Shift+I; Cmd+Option+I on a Mac). The panels you'll use from day one:

  • Elements (Firefox: Inspector): the live DOM and the CSS rules applied to the selected element. You can edit both and see the change instantly; nothing is saved to your file.
  • Console: error messages, and a prompt where you can type JavaScript that runs inside the page.
  • Network: every request the page made, with its status, size and time.
  • Later: Sources (debugger, breakpoints), Performance, Application (storage), Lighthouse (audits).

Your first three pages

Page 1: a document

Create a folder web-start and in it a file hello.html:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first page</title>
</head>
<body>
  <h1>Hello, web!</h1>
  <p>This is my first page. It has a <a href="https://developer.mozilla.org">link to MDN</a>.</p>
</body>
</html>

Double-click it, or drag it onto a browser window. Read it line by line:

  • <!doctype html> tells the browser "use modern standards mode" (without it, browsers emulate 1990s bugs).
  • <html lang="en"> is the root element; lang tells screen readers and translators which language the text is in.
  • <head> holds information about the page: the character encoding (utf-8, Volume 1 chapter 6) and the <title> shown on the browser tab.
  • <body> holds what's shown: a top-level heading <h1>, a paragraph <p>, and a link <a> whose href says where it goes.
  • Most elements are a start tag, content, and an end tag: <p>…</p>. Elements nest inside each other like boxes, and that nesting is the tree the browser builds.

Checked in Chromium: the tab title is My first page and the heading text is Hello, web!.

Page 2: adding style and behaviour

counter.html counts cups of chai. It has all three languages in one file: HTML for the content, a <style> element with CSS, and a <script> element with JavaScript:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Chai counter</title>
  <style>
    body   { font-family: system-ui, sans-serif; max-width: 30rem; margin: 2rem auto; }
    h1     { color: #8a4b08; }
    button { font-size: 1.25rem; padding: 0.5rem 1rem; border-radius: 0.5rem; }
  </style>
</head>
<body>
  <h1>Cups of chai today</h1>
  <p>You've had <strong id="count">0</strong> cups.</p>
  <button id="more" type="button">One more cup</button>
  <script>
    let cups = 0;                                         // the state lives in a JavaScript variable
    const countEl = document.getElementById("count");     // find elements in the page
    const button  = document.getElementById("more");
    button.addEventListener("click", () => {              // run this function on every click
      cups = cups + 1;
      countEl.textContent = cups;                         // update the page to match the state
      if (cups >= 3) countEl.style.color = "crimson";
    });
  </script>
</body>
</html>
  • CSS rules have a selector (h1: which elements) and declarations (color: #8a4b08: property and value). #8a4b08 is a colour in hex: red 138, green 75, blue 8 (Volume 1 chapter 6).
  • The script finds two elements by their id, then registers an event listener: a function the browser will call every time the button is clicked. This is the core pattern of all interactive web code: keep state, listen for events, update the page to match the state. React, Angular and every other framework (chapters 21–23) are sophisticated versions of exactly this.

Clicked three times in Chromium (automated with Playwright, which this volume's labs use throughout):

Text
after 3 clicks: You've had 3 cups. | colour: rgb(220, 20, 60)
h1 colour: rgb(138, 75, 8)

crimson is rgb(220, 20, 60); the browser reports every colour in the same rgb form whatever you wrote.

Reading your first error

Move the script into the <head>, above the button, and it breaks:

HTML
<head>
  <script>
    document.getElementById("more").addEventListener("click", () => alert("hi"));
  </script>
</head>
<body>
  <button id="more">Click</button>
</body>

The Console shows:

Text
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

Read it like a sentence. Something was null (JavaScript's "no object here"), and the code tried to read its addEventListener property. The only thing before .addEventListener is document.getElementById("more"), so that returned null: no element with that id exists yet, because the browser runs a plain <script> as soon as it reaches it, before parsing the rest of the page. Three standard fixes, in order of preference: put scripts at the end of <body>; add the defer attribute to an external script (<script src="app.js" defer>), which waits until the document is parsed; or use type="module", which defers automatically. Chapter 12 explains script loading fully.

This error, "cannot read properties of null/undefined", is the single most common JavaScript error you'll see. It always means "the thing on the left of the dot didn't exist when this line ran".

Page 3: a tiny app with data from a server

Real pages get data from servers as JSON (JavaScript Object Notation), a text format for lists and objects. Make a folder site with three files:

HTML
<!-- site/index.html -->
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Menu</title><script type="module" src="app.js"></script></head>
<body><h1>Today's menu</h1><ul id="menu"></ul></body>
</html>
JavaScript
// site/app.js
const response = await fetch("menu.json");          // ask the server for data
const items = await response.json();                // parse the JSON text into JavaScript objects
const list = document.getElementById("menu");
for (const item of items) {
  const li = document.createElement("li");
  li.textContent = `${item.name}: ₹${item.price}`;
  list.append(li);
}
JSON
[{"name": "Masala dosa", "price": 90}, {"name": "Idli (2)", "price": 50}, {"name": "Filter coffee", "price": 30}]

fetch sends an HTTP request from JavaScript; await waits for the answer without freezing the page (chapter 15 explains how). Now open index.html by double-clicking it, and the list stays empty. The Console says:

Text
Access to script at 'file:///…/site/app.js' from origin 'null' has been blocked by CORS policy:
Cross origin requests are only supported for protocol schemes: chrome, chrome-untrusted, data, http, https.

A page opened from a file:// address has no proper origin (its origin is null), and browsers refuse to load modules or fetch data for it, for security reasons explained in chapters 12 and 19. The fix is to serve the folder over HTTP, the way a real site is served:

Text
$ cd site
$ python3 -m http.server 8000
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [29/Oct/2026 08:02:08] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [29/Oct/2026 08:02:08] "GET /app.js HTTP/1.1" 200 -
127.0.0.1 - - [29/Oct/2026 08:02:08] "GET /menu.json HTTP/1.1" 200 -

Open http://localhost:8000/ and the list appears:

Text
Masala dosa: ₹90
Idli (2): ₹50
Filter coffee: ₹30

The server's log shows the three requests from the diagram at the top of this chapter: the document, then the script it referenced, then the data the script fetched. From now on, always develop through a local server, never by double-clicking files.

Now open the Elements panel: the <ul> contains three <li> elements. Then View Source (Ctrl+U): the <ul> is empty. That's the DOM-versus-HTML difference from earlier, seen with your own eyes.

How to study the web

A routine that works

  1. Read one topic of a Start here section.
  2. Type the example (don't paste), serve it, open it.
  3. Break it on purpose: delete a closing tag, misspell a CSS property, call a function that doesn't exist. Watch what the browser does. HTML and CSS forgive mistakes silently (the browser guesses or ignores), while JavaScript stops with an error. Learning what each does when wrong is half of debugging.
  4. Inspect it in DevTools: the element, its styles, the Console, the Network panel.
  5. Answer Check yourself without looking, then take the quiz.
  6. Revisit after a few days: rebuild the example from memory.

Use MDN Web Docs (developer.mozilla.org) as your reference: it's accurate, maintained by browser vendors and others, and shows browser support for every feature. When a feature is newer, check its Baseline label on MDN ("Widely available" means all major browsers have supported it for at least 2½ years).

Warm-up exercises

Do these before chapter 13. Each takes under an hour.

  1. About me: a page with a heading, a photo (<img src alt>), two paragraphs, a list of hobbies and a link. Validate it at validator.w3.org.
  2. Restyle it: without changing the HTML, give it a page width, a font, colours and spacing using only a <style> block.
  3. Dark mode button: add a button that toggles a class on <body> (document.body.classList.toggle("dark")), and CSS for body.dark.
  4. Tip calculator: two <input type="number"> fields (bill, tip %) and a result that updates on every input event. What happens if a field is empty? Make it show nothing rather than NaN.
  5. Menu from JSON: extend page 3 so each item also has a vegetarian flag, shown with a 🟢 or 🔴 marker, and add a checkbox that hides non-vegetarian items.

Glossary

  • Browser / client: the program that requests and displays web pages.
  • Server: a program that answers requests.
  • URL: a web address, such as https://example.com/menu?day=mon.
  • HTTP / HTTPS: the request–response protocol of the web; HTTPS is HTTP over an encrypted connection.
  • HTML element: a piece of content marked by tags, such as <p>…</p>; attribute: extra information on a tag, such as href or id.
  • DOM: the live tree of objects the browser builds from HTML, which JavaScript reads and changes.
  • CSS rule: a selector plus declarations (h1 { color: red; }).
  • Event / event listener: something that happened (a click, a key press) / a function the browser calls when it does.
  • JSON: a text format for data, based on JavaScript's object syntax.
  • DevTools: the browser's built-in inspection and debugging tools.
  • Origin: scheme + host + port, the browser's unit of trust (chapter 12).
  • localhost: your own computer, as seen from itself.