Skip to content
KALANBLON
Show contents

Chapter 1 of 7 · 20 min

Semantic HTML: every tag has a role

By the end of this chapter, you'll be able to

  • Divide a page with header, nav, main, article, section and footer
  • Organise headings h1 to h6 into a logical hierarchy
  • Write links and images that can be understood without seeing them
  • Explain how semantics helps accessibility and search engines

The main areas of a page

HTML offers tags whose name says their role. A browser displays them as plain blocks, but assistive technologies (screen readers, braille displays) use them to offer shortcuts: "go to main content", "list of headings", "navigation menu".

<header>
  <a href="/">Amina's Sewing Workshop</a>
  <nav aria-label="Main menu">
    <ul>
      <li><a href="/creations">Creations</a></li>
      <li><a href="/contact">Contact</a></li>
    </ul>
  </nav>
</header>

<main>
  <h1>Alterations and made-to-measure creations</h1>
  <article>
    <h2>This season's embroidered boubou</h2>
    <p>Light cotton, hand-made embroidery…</p>
  </article>
  <section>
    <h2>Prices</h2>
    <p>Hem: 3,000 CFA francs.</p>
  </section>
</main>

<footer>
  <p>Open Monday to Saturday, 9 am – 6 pm.</p>
</footer>
  • <header>: the header (logo, site title, often the menu);
  • <nav>: a block of navigation links. aria-label gives it a name, useful if there are several;
  • <main>: the main content, only one per page;
  • <article>: self-contained content, which would make sense shared on its own (a post, a product sheet);
  • <section>: a thematic part, with its own heading;
  • <footer>: the footer.

A <div> has no meaning: keep it for grouping elements purely for layout.

With only divs, the tree contains no landmark or heading: you have to listen to everything, from start to finish.

Step 5 of 5
Text description of the animation
  1. The browser reads the HTML.
  2. It builds the DOM, the tree of all the page's elements.
  3. Each semantic tag gets a role: main becomes the main landmark, h1 a level 1 heading.
  4. The user jumps straight to the content, or browses the list of headings like a table of contents.
  5. With only divs, the tree contains no landmark or heading: you have to listen to everything, from start to finish.

A hierarchy of headings

Headings <h1> to <h6> form the page's outline, like a book's chapters:

  • a single <h1>: the subject of the page;
  • <h2>s for the main parts, <h3>s inside an <h2>, and so on;
  • you don't skip a level (no <h4> directly under an <h2>).

You never choose a heading level for its size: size is CSS's job. Search engines also use this outline to understand what your page is about.

Links and images that can be understood without seeing them

A link must make sense out of context, because many users move from link to link. An image must have a text alternative:

<!-- Avoid -->
<p>For our prices, <a href="/prices">click here</a>.</p>
<img src="boubou.jpg">

<!-- Better -->
<p>See <a href="/prices">our alteration prices</a>.</p>
<img src="boubou.jpg" alt="Indigo blue boubou with gold embroidery at the collar" width="600" height="400">
<img src="pattern.svg" alt="">
  • alt describes what the image adds, not "image of…". For a purely decorative image, write alt="": the screen reader ignores it. With no alt attribute at all, it often reads the file name.
  • width and height reserve the image's space before it loads: the page doesn't "jump" on a slow connection.

Your turn

Make this page semanticHTML · CSS

Preview

The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the outline of a recipe page

A recipe page contains: the site name and a menu (Home, Recipes), the recipe title "Chicken mafé", a photo of the dish, an "Ingredients" part with a list, a "Method" part with numbered steps, and a footer with the legal notice. Write the semantic HTML skeleton, with the right heading hierarchy and a text alternative for the photo.

Hint 1
The recipe is self-contained content: an <article> inside <main>. Its two parts each have a level 2 heading.
Hint 2
The steps come in a precise order: that's a numbered list <ol>. The ingredients have no order: <ul>.
Show the solution and common mistakes
<header>
  <a href="/">World Kitchen</a>
  <nav aria-label="Main menu">
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/recipes">Recipes</a></li>
    </ul>
  </nav>
</header>
<main>
  <article>
    <h1>Chicken mafé</h1>
    <img src="mafe.jpg" alt="Mafé served with white rice, peanut sauce and pieces of chicken" width="800" height="533">
    <section>
      <h2>Ingredients</h2>
      <ul>
        <li>600 g of chicken</li>
        <li>150 g of peanut butter</li>
      </ul>
    </section>
    <section>
      <h2>Method</h2>
      <ol>
        <li>Brown the chicken.</li>
        <li>Add the diluted peanut butter.</li>
      </ol>
    </section>
  </article>
</main>
<footer>
  <a href="/legal-notice">Legal notice</a>
</footer>

Common mistakes:

  • Putting the site name in an <h1> on every page: the <h1> must describe the subject of this page, here the recipe.
  • Writing alt="photo" or alt="mafe.jpg": it tells someone who can't see the image nothing.

Summary

  1. 1A semantic tag says the role of a piece of content: header, nav, main, article, section, footer.
  2. 2A single h1, then h2, h3… without skipping a level: it's the page's outline.
  3. 3A link has explicit text; an image has an alt that says what it adds, or alt="" if it's decorative.
  4. 4A button is a <button>, navigation is an <a href>: never a clickable div.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.