Chrysolites Tech Academy

Introduction to Computer Science: Harvard CS50 · Course book

Chapter 6: From an Address to a Page

Pairs with Lesson 6: How the Web Works: HTTP, HTML and CSS

Typing an address into a browser and seeing a page appear a fraction of a second later feels like one step. In fact dozens of systems cooperate: your computer finds out where the site lives, sends a carefully formatted request across many networks, and receives a reply that the browser turns into text, pictures and layout. This chapter follows that journey. It explains how computers on the internet find each other, what a browser and a server say to each other, and the two languages, HTML and CSS, that describe what a page contains and how it looks.

Addresses on the internet

Every device connected to the internet needs an address, just as every house needs one for post to arrive. The system is called the Internet Protocol, IP, and an address in its original version, IPv4, is 32 bits, written as four numbers from 0 to 255 separated by dots, such as 192.168.1.20. Thirty-two bits allow 2 multiplied by itself 32 times, about 4.3 billion, addresses, which seemed plenty in the 1980s and is now far too few. The newer IPv6 uses 128 bits, enough for about 3.4 × 1038 addresses, more than could ever be used.

When your laptop joins a network, a service called DHCP automatically gives it an address to use, along with the address of the local router, the device that forwards data towards its destination. Routers are the junctions of the internet, much like sorting offices for post. Each one passes data to a neighbour that is closer to the destination, so a request may hop through a dozen routers between you and a distant server.

People prefer names to numbers, so the Domain Name System, DNS, acts as the internet's directory. When you type a name such as example.com, your computer first asks a DNS server which IP address that name belongs to, then sends its request to that address. The answer is remembered for a while, in what is called a cache, so the next visit to the same site skips the lookup and is faster.

Data does not travel in one piece. It is split into packets, each typically up to about 1,500 bytes, so a 1-megabyte image travels as roughly 700 packets. Packets may take different routes and arrive out of order. The Transmission Control Protocol, TCP, numbers them so they can be put back together, and asks for any lost packet to be sent again. TCP also uses port numbers to deliver data to the right program on a computer: web traffic conventionally uses port 80, and encrypted web traffic port 443.

HTTP: how browsers and servers talk

Once a connection is made, the browser and the server speak the HyperText Transfer Protocol, HTTP. The browser sends a short text request; the server sends back a response.

Browser Server GET / HTTP/1.1Host: example.com 1. request HTTP/1.1 200 OKContent-Type: text/html<!DOCTYPE html> … 2. response Headers come first, then a blank line, then the content itself.
Figure 6.1 — One HTTP exchange. The request names a method and a path; the response begins with a status code and headers describing what follows.

The request's first line contains a method, most often GET, meaning "please send me this", followed by the path of the page wanted and the version of HTTP. Further lines, called headers, add details such as which site is meant, since one server may host many. The response begins with a three-digit status code that summarises the outcome.

CodeMeaningWhen you see it
200OKThe page was found and is being sent
301Moved permanentlyThe page lives at a new address, given in a header; the browser follows it automatically
304Not modifiedThe browser's saved copy is still current, so nothing needs sending
401 / 403Unauthorised / ForbiddenYou must log in, or you are not allowed to see this
404Not foundNo page exists at that path
500Internal server errorSomething went wrong in the server's own code

A URL can carry information to the server in its query string, the part after a question mark. In the address www.example.com/search?q=cats, the path is /search and the parameter q has the value cats. A search engine reads that parameter and returns matching results. Data that should not appear in the address bar, such as a password, is sent instead with the POST method, inside the body of the request.

Browsers include developer tools that show every request a page makes. Opening them and reloading a page reveals that one visit typically triggers dozens of requests: one for the HTML, then more for each style sheet, script, font and image it refers to. Each has its own status code and headers. Watching this list is one of the best ways to understand, and to speed up, a slow page.

Plain HTTP is readable by anyone along the route. HTTPS wraps the same conversation in encryption, so that only the browser and the server can read it, and it uses a certificate to prove that the server really is the site it claims to be.

HTML: the structure of a page

The content of a web page is written in HTML, the HyperText Markup Language. HTML is not a programming language; it has no loops or conditions. It marks up text with tags that say what each part is.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>hello</title>
    </head>
    <body>
        <h1>Welcome</h1>
        <p>Visit the <a href="about.html">about page</a>.</p>
    </body>
</html>

Most tags come in pairs: an opening tag such as <p> and a closing tag such as </p>, with content between. Some tags carry attributes that add information: in <a href="about.html"> the attribute href gives the link's destination, and in <img src="cat.jpg" alt="A sleeping cat"> the alt attribute describes the image for people using screen readers. The head holds information about the page, such as its title; the body holds what is displayed.

Because tags nest inside one another, every page forms a tree, and browsers build exactly that tree in memory.

html headbodytitleh1p "hello""Welcome"text and a link
Figure 6.2 — A page as a tree. Each element is a node whose children are the elements nested inside it.

HTML also provides forms, which let users send information to a server, whether a search, a sign-up or a comment. A form with an action of /search, a method of GET and a text field named q produces exactly the query string seen above when submitted: the browser takes whatever was typed and requests /search?q=whatever.

Many other tags exist for particular purposes. Headings run from <h1>, the most important, down to <h6>. Lists use <ul> for bullets or <ol> for numbers, with each item in an <li>. Tables are built from <table>, with rows in <tr> and cells in <td>. Choosing tags by meaning rather than appearance, for example marking the main heading as <h1> instead of just making text large, helps search engines and screen readers understand the page. A tool called a validator checks that tags are properly nested and closed, catching errors that browsers would otherwise silently guess around.

CSS: how a page looks

HTML says what things are; CSS, Cascading Style Sheets, says how they should look. A CSS rule pairs a selector, which picks out elements, with properties and values.

body {
    font-family: sans-serif;
    text-align: center;
}

h1 {
    color: #0b5fff;
    font-size: 2rem;
}

.warning {
    background-color: #fff7e6;
}

The selector body styles the whole page; h1 styles every top-level heading; and .warning, beginning with a dot, styles any element given the attribute class="warning". Selectors can also target a single element by its id, written with #. Colours are often written in hexadecimal: #0b5fff is red 0x0B, green 0x5F and blue 0xFF, a strong blue.

CSS also controls layout. Properties such as margin and padding set the space around and inside each element, width and height set its size, and layout systems called flexbox and grid arrange elements in rows and columns. Rules inside a media query apply only on screens of a certain size, which is how one page can rearrange itself for a phone and a desktop, an approach known as responsive design.

Keeping style in a separate file, linked from the head of each page, means one change of colour or font updates an entire site. "Cascading" refers to the rules for resolving conflicts: when several rules apply to one element, the more specific one wins, and later rules override earlier ones of equal specificity.

Separate the layers. Content in HTML, appearance in CSS and, later, behaviour in a programming language. Keeping them apart makes pages easier to maintain and more accessible, because the underlying structure stays clear.

Check your understanding

  • Why did the internet need IPv6? (Answer: IPv4's 32 bits allow only about 4.3 billion addresses.)
  • What does DNS do?
  • Roughly how many 1,500-byte packets carry a 3-megabyte file? (Answer: about 2,000.)
  • Which status code means the page was not found, and which means the server's code failed? (Answer: 404 and 500.)
  • In /images?size=large&colour=red, what are the parameters? (Answer: size with value large, and colour with value red.)
  • Why should an image tag include an alt attribute?
  • In CSS, what is the difference between the selectors p, .note and #intro?

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.