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:

  1. Find the shape. A whole page — <!DOCTYPE html>, <html>, <head>, <body>? A fragment, like nav.html? A stylesheet?
  2. 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.
  3. 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?
  4. 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:

LineWithout 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
viewporta phone shows the page at desktop width, shrunk

The parts of an element

<a href="about.html" class="action">About me</a>
PartHere
the opening tag<a …>
attributes — a name and a valuehref="about.html" and class="action"
the contentAbout 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 iteverybody sees it
above <body>inside <body>
about the page: title, charset, favicon, stylesheetthe top of the page: site name, logo, navigation
one per pageone 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

ElementIt 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
You readIt goes to
about.htmla page in the same folder
images/photo.webpa file in the images folder
../index.htmlup one folder
about.html#contactthe element with id="contact" on about.html
https://developer.mozilla.organother site — a full, absolute address
mailto:mei@example.comopens 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 manyas many elements as you likeone 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

SelectorPicks
pevery <p>
.quoteevery element with class="quote"
#site-footerthe one element with id="site-footer"
nav aevery <a> inside a <nav> — the space means "inside"
h1, pevery <h1> and every <p> — the comma means "and"
a:hovera link, while the mouse is over it

Values you will meet

ValueMeans
16px16 pixels
1.5rem1.5 × the page's base font size — usually 16px, so 24px
1.2em1.2 × the current font size
100%the full width of the parent
#c2410ca colour: red, green and blue, in hexadecimal
margin: 0 autonothing above and below, equal space left and right — it centres a box

Three ways to apply CSS

WhereLooks likeUse it?
inline<p style="color:red">no — one element, impossible to reuse, hard to override
internal<style> in the headfor 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

PropertyChanges
colorthe colour of the text
background-colorthe 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
marginspace outside
borderthe edge
paddingspace inside
contentyour 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; }
  1. Which text does the .intro rule make larger?
  2. What does display: flex do to the two links?
  3. In the .quote rule, which declaration makes space inside the card, and which draws a line?
  4. #site-footer sets background-color and color. Which one changes the text?
  5. Why does the stylesheet use #site-footer and not simply footer?
  6. Which selectors pick a class, and which an id? How does the HTML mark each one?
Show the answers
  1. Twenty quotes, collected by hand. — the only element with class="intro".
  2. It puts the children of the <nav> — the two links — side by side in a row, 1rem apart.
  3. padding makes the space inside; border-left draws a 4px line down the left edge.
  4. color changes the text. background-color colours the box behind it.
  5. footer would also pick the <footer> inside the quote — the one holding Tim Berners-Lee. The id picks only the site footer.
  6. .intro and .quote pick a class: class="intro", class="quote". #site-footer picks an id: id="site-footer".