Reading HTML and CSS
How to read code you did not write: what each part of a page is, which elements a CSS rule picks, and what it changes.
A method for code you did not write
Every time, in this order:
- Find the shape. A whole page —
<!DOCTYPE html>,<html>,<head>,<body>? A fragment, likenav.html? A stylesheet? - Match the pairs. Every
<tag>with its</tag>, every"with its partner, every{with its}. Indentation shows you the nesting — where it does not line up, look closer. - Name each part's job. Is it in the head (about the page) or in the body (the page)? Is it header, nav, main or footer?
- For each CSS rule, ask two questions. Which elements does the selector pick? What does each declaration change?
Reading HTML
The skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page title</title>
</head>
<body>
<!-- everything people see -->
</body>
</html>
Four lines that are not decoration. Each one prevents a real, visible failure:
| Line | Without it |
|---|---|
<!DOCTYPE html> | the browser falls back to 1990s behaviour |
lang="en" | a screen reader does not know which language to pronounce |
charset="UTF-8" | 中文 and emoji turn to nonsense |
viewport | a phone shows the page at desktop width, shrunk |
The parts of an element
<a href="about.html" class="action">About me</a>
| Part | Here |
|---|---|
| the opening tag | <a …> |
| attributes — a name and a value | href="about.html" and class="action" |
| the content | About me |
| the closing tag | </a> |
A few elements have no content and no closing tag: <img>, <meta>, <link>, <input>, <br>. They are complete on their own — </img> is an error.
Elements close in the reverse order they opened. The last one opened is the first one closed:
<p>Open <strong>every day</strong></p> <!-- ✓ -->
<p>Open <strong>every day</p></strong> <!-- ✗ -->
<head> is not <header>
<head> | <header> |
|---|---|
| nobody sees it | everybody sees it |
above <body> | inside <body> |
| about the page: title, charset, favicon, stylesheet | the top of the page: site name, logo, navigation |
| one per page | one per page — and one in each <article>, if you like |
The same trap waits at the bottom: <footer> is visible content, and there is no <foot>.
The four parts of a page
<body>
<header>
<nav> … </nav> <!-- where you can go -->
</header>
<main> … </main> <!-- why they came: the only part that changes per page -->
<footer> … </footer> <!-- the small print -->
</body>
What each element says
| Element | It says |
|---|---|
<nav> | these are navigation links |
<main> | this is the main content of the page |
<article> | this stands alone — a post, a product, a quote |
<section> | a themed part, with its own heading |
<aside> | only loosely related — a sidebar |
<header> · <footer> | an introduction · the small print |
<div> | nothing — the honest answer when nothing more specific fits |
<h1> to <h6> | an outline: one <h1>, and never skip a level |
<strong> · <em> | important · stressed — a screen reader says them differently |
<b> · <i> | bold and italic pixels, with no meaning |
<ul> | a list where the order does not matter |
<ol> | a list where the order is the information |
<dl> <dt> <dd> | pairs: a term, and its description |
<blockquote> · <cite> | a quotation · where it came from |
<table> <th> <td> | data in rows and columns — never page layout |
<label for="…"> | the name of a form field; its for matches the field's id |
<address> | contact details for the page |
Links and paths
| You read | It goes to |
|---|---|
about.html | a page in the same folder |
images/photo.webp | a file in the images folder |
../index.html | up one folder |
about.html#contact | the element with id="contact" on about.html |
https://developer.mozilla.org | another site — a full, absolute address |
mailto:mei@example.com | opens a mail program |
Relative paths for your own pages: they still work when the site moves to a server. Absolute addresses, with https://, only for other sites.
class and id
class="quote" | id="site-footer" | |
|---|---|---|
| How many | as many elements as you like | one element per page |
| In CSS | .quote | #site-footer |
| Also used by | — | links (#contact) and JavaScript (getElementById) |
Reading CSS
The parts of a rule
p {
color: #333;
font-size: 16px;
}
Selector — which elements: p. Property — what aspect: color. Value — what setting: #333. Each declaration ends with ;.
What a selector picks
| Selector | Picks |
|---|---|
p | every <p> |
.quote | every element with class="quote" |
#site-footer | the one element with id="site-footer" |
nav a | every <a> inside a <nav> — the space means "inside" |
h1, p | every <h1> and every <p> — the comma means "and" |
a:hover | a link, while the mouse is over it |
Values you will meet
| Value | Means |
|---|---|
16px | 16 pixels |
1.5rem | 1.5 × the page's base font size — usually 16px, so 24px |
1.2em | 1.2 × the current font size |
100% | the full width of the parent |
#c2410c | a colour: red, green and blue, in hexadecimal |
margin: 0 auto | nothing above and below, equal space left and right — it centres a box |
Three ways to apply CSS
| Where | Looks like | Use it? |
|---|---|---|
| inline | <p style="color:red"> | no — one element, impossible to reuse, hard to override |
| internal | <style> in the head | for a quick test |
| external | <link rel="stylesheet" href="style.css"> | yes — every page, one file to change, cached by the browser |
color is the text
| Property | Changes |
|---|---|
color | the colour of the text |
background-color | the colour of the box behind the text |
color: red; never makes a box red.
The box model
Every element is a box, in four layers:
| Layer | |
|---|---|
margin | space outside |
border | the edge |
padding | space inside |
| content | your text |
.quote {
padding: 1.5rem; /* space inside the card */
border-left: 4px solid #c2410c; /* a line down its left edge */
margin-bottom: 1.5rem; /* space between one card and the next */
}
DevTools shows this diagram for any element you click. Try it: the box model, live.
Flexbox and Grid
nav { display: flex; }
display: flex puts the children of the element side by side, in a row. Grid lines things up in columns and rows decided in advance. In the quote collection: Grid for the twenty cards, Flexbox inside each card. See both, live.
Practice: read a page
<header>
<nav>
<a href="index.html">Home</a>
<a href="quotes.html">Quotes</a>
</nav>
</header>
<main>
<p class="intro">Twenty quotes, collected by hand.</p>
<article class="quote">
<blockquote>
<p>This is for everyone.</p>
</blockquote>
<footer>
<cite>Tim Berners-Lee</cite>
</footer>
</article>
</main>
<footer id="site-footer">
<p>Built by Li Wei</p>
</footer>
nav { display: flex; gap: 1rem; }
.intro { font-size: 1.25rem; }
.quote { padding: 1.5rem; border-left: 4px solid #c2410c; }
#site-footer { background-color: #f3f4f6; color: #6b7280; }
- Which text does the
.introrule make larger? - What does
display: flexdo to the two links? - In the
.quoterule, which declaration makes space inside the card, and which draws a line? #site-footersetsbackground-colorandcolor. Which one changes the text?- Why does the stylesheet use
#site-footerand not simplyfooter? - Which selectors pick a class, and which an id? How does the HTML mark each one?
Show the answers
- Twenty quotes, collected by hand. — the only element with
class="intro". - It puts the children of the
<nav>— the two links — side by side in a row,1remapart. paddingmakes the space inside;border-leftdraws a 4px line down the left edge.colorchanges the text.background-colorcolours the box behind it.footerwould also pick the<footer>inside the quote — the one holding Tim Berners-Lee. The id picks only the site footer..introand.quotepick a class:class="intro",class="quote".#site-footerpicks an id:id="site-footer".