Second in the series, after Flexbox & Grid

<html>

The language that gives a page its structure and its meaning. Every panel below is editable — change the markup and the browser responds.

No CSS is loaded in that preview. Everything you see is the browser's own default styling of your tags.

Part one: the parts

HTML is text with labels attached. The labels tell the browser what each piece of content is — a heading, a link, a list item — and everything else follows from that.

01 Anatomy of an element

Almost every element follows the same shape: an opening tag, some content, a closing tag. The opening tag can carry attributes that configure it.

Click any part of the example to find out what it's called and what it does. Learning these five names now saves a lot of confusion later, because error messages and documentation assume them.

A few elements have no content and no closing tag — <img>, <br>, <input>, <meta>. They're called void elements.

Pick a part of the tag above.

02 Everything nests

Elements go inside other elements, and that nesting is the whole structure of the document. The result is a tree: every element has one parent and any number of children.

The tree pane on the right is built from your markup exactly the way the browser builds it. Indent your code to match the nesting — the browser doesn't care, but you and everyone reading after you will.

The one rule that matters: tags close in the reverse order they opened. <p><em>…</em></p> is fine; crossing them over is not.

Try deleting a closing tag and watch the tree change shape.

03 The page skeleton

A real page has a fixed opening. Each line earns its place:

<!DOCTYPE html> tells the browser to use modern standards rather than an ancient compatibility mode. lang="en" tells screen readers and translators which language to pronounce.

<meta charset="utf-8"> prevents mangled accents and emoji. The viewport meta stops phones from pretending to be a desktop and zooming your page out.

Everything in <head> is information about the page. Everything in <body> is the page itself.

Type this skeleton from memory a few times. It's the one piece of HTML worth memorising.

04 Text that means something

Headings run from <h1> to <h6>. They aren't font sizes — they're rank. An <h2> is a subsection of the <h1> above it, and CSS can make it any size you want later.

<strong> means the content is important; <em> means it carries emphasis that changes the sentence's meaning. <b> and <i> are their appearance-only cousins — reach for them only when nothing is actually being stressed, like a taxonomic name.

Other useful ones: <blockquote> with <cite>, <code>, <time>, <abbr>, and <small> for legal fine print.

Never use a heading tag to make text big, and never use <br> to make a gap. Those are CSS's job.

05 Lists

Three kinds, and the difference is meaning rather than bullet shape.

<ul> is for items whose order doesn't matter. <ol> is for steps or rankings, where reordering would change the sense. <dl> pairs terms with descriptions using <dt> and <dd>.

Only <li> may sit directly inside a <ul> or <ol>. To nest a sublist, put the whole inner list inside an <li>.

Navigation menus are lists. Wrapping your nav links in a <ul> tells a screen reader how many links are coming.

06 Links

The <a> element plus an href is what makes the web a web. The value can be a full address, a path on your own site like /about, a file next to the current page like plants.html, or #section-id to jump within the page.

Write link text that makes sense read on its own. Many people navigate by pulling up a list of every link on the page, and a list of six identical "click here" entries tells them nothing.

If a link opens in a new tab with target="_blank", say so in the text. A tab appearing unannounced is disorienting.

07 Images and figures

<img> needs two things: src, the file to load, and alt, the text that stands in for the image when it can't be seen.

Write alt as a replacement, not a caption — describe what a person would miss. If the image is purely decorative, use an empty alt="", which tells screen readers to skip it. Leaving alt off altogether is different and worse: some screen readers then read out the filename.

Use <figure> with <figcaption> when the image needs a visible caption tied to it.

Setting width and height lets the browser reserve the right space before the file arrives, so the page stops jumping around as it loads.

Part two: meaning

Two pages can look identical and be completely different underneath. This part is about the difference, which is what search engines, screen readers, and your future self all read.

08 Semantic landmarks

A handful of elements label the big regions of a page: <header>, <nav>, <main>, <aside>, <footer>, plus <article> and <section> for the content itself.

They come with built-in roles, listed in the pane on the right. Screen reader users jump straight between these — the equivalent of glancing at a page and seeing where things are. Swap them all for <div> and that list empties out.

Use exactly one <main>, holding the content unique to this page. The nav and footer that repeat on every page belong outside it.

A <section> only becomes a landmark when it has a name — give it an aria-label or a heading referenced by aria-labelledby.

09 div and span

These two mean nothing at all, and that's the point. Use them when you need a box to hang styling or scripting on and no element with actual meaning fits.

<div> is the block-level one: it starts on a new line and fills the width. <span> is inline: it sits inside a line of text without breaking it.

The question to ask before typing <div> is simply whether a more specific element exists. Usually one does. A wrapper purely for layout, though, is a perfectly good div — that's exactly what it's for.

Compare the two versions on the right in the landmarks pane. They render identically and one of them is invisible to assistive technology.

10 The heading outline

Your headings form a table of contents whether you meant them to or not. The pane on the right extracts it, the same way assistive technology does.

Start at <h1> and don't skip levels on the way down. Jumping from <h2> to <h4> implies a missing section and leaves a hole in the outline — try it and the pane will flag it.

Going back up a level is normal and fine: that's just the next section starting.

A quick self-test for any page you build: read only the headings aloud. If the page still makes sense, the structure is sound.

11 Tables

Tables are for data with two dimensions — rows and columns that both mean something. They are not for page layout; that was 1998 and we have grid now.

A good table has a <caption> saying what it contains, a <thead> and <tbody>, and header cells marked as <th> rather than <td>.

The critical attribute is scope. scope="col" says a header labels its column, scope="row" its row. That's what lets a screen reader announce "Water, twice weekly" instead of reading out a bare, contextless number.

12 Forms and labels

Every input needs a <label>, connected by matching the label's for to the input's id. This isn't decoration: it's what makes clicking the label focus the field, and what makes the field announce its own name.

Placeholder text is not a label. It vanishes the moment someone starts typing, exactly when they might want to check what they're filling in.

Pick the right type and you get validation and the correct phone keyboard for free — email, tel, number, date, search.

Group related controls such as radio buttons in a <fieldset> with a <legend>, and always give <button> an explicit type.

13 What the browser repairs

HTML never throws an error. Hand a browser something impossible and it silently builds the closest legal tree it can, then carries on.

That sounds generous until your CSS stops matching and you can't see why. The markup on the right is invalid — a <div> can't live inside a <p>, and the tags cross over. Compare what you typed with the tree you actually got.

The third block is a different case: a <p> inside a <ul> is invalid, but the parser has no rule to repair it, so it stays exactly as typed and nothing warns you. Only a validator catches that kind.

The fix is always to write the nesting you meant, not to work around the repair.

Paste your own markup into the W3C validator to catch these before they confuse you.

Part three: putting it to work

One page of markup with six things wrong with it, and a checklist that updates as you type.

14 Your turn

The markup on the right renders acceptably and is structurally poor. Fix it until every item in the checklist lights up.

Work through it from the outside in: get the landmarks and the single <h1> in place first, then the list, then the image and the form field.

Each item corresponds to something from a lesson above. If one won't go green, the lesson number is in the wording.

A pocket reference

The elements worth knowing by name, with what they're for rather than what they look like.

ElementUse it when
<main>Wrapping the content unique to this page. One per page.
<article>A piece that would still make sense on its own — a post, a card, a comment.
<section>A themed chunk of a page that has a heading.
<nav>A set of navigation links, not every group of links.
<aside>Content related to the main thread but not part of it.
<figure>An image, diagram or snippet referenced from the text, usually captioned.
<button>Something happens on this page. Use <a> when you go somewhere.
<label>Always, next to every form control.
<time>A date or duration, with a machine-readable datetime attribute.
<div>Nothing more specific fits and you need a box.