Chapter 14 of 26

Semantic HTML

Look at two ways of building the top of a web page:

<div class="top">
  <div class="logo">CS Portal</div>
  <div class="menu">...</div>
</div>
<header>
  <p>CS Portal</p>
  <nav>...</nav>
</header>

In a browser, both can look exactly the same. But the second one says what it is. A screen reader, a search engine, a browser's reader mode or a new developer joining your team can instantly tell "this is the page header, and this is the navigation". The first one only says "a box, inside a box".

Writing HTML that describes the meaning of content, not just its appearance, is called semantic HTML. In this chapter we'll learn the page-structure elements – <header>, <nav>, <main>, <article>, <section>, <aside>, <footer> and friends – and, more importantly, how to choose between them.

What is Semantic HTML?

Semantic HTML means using the HTML element whose meaning best matches the content – a heading for a heading, a list for a list, <nav> for navigation, <button> for a button – instead of generic containers styled to look right.

"Semantic" simply means "related to meaning". You have actually been writing semantic HTML since the beginning of this tutorial: <h1> to <h6> (from Headings & Paragraphs), <strong> and <em> (from Text Formatting), <ul>/<ol>, <table> and <label> all carry meaning. This chapter adds the elements that describe the big regions of a page.

The opposite is often called "div soup": a page built entirely from <div> and <span> (which we'll study in Block vs Inline Elements: div & span), where only class names hint at what anything is.


Why Semantics Matter

Who benefits

How

Screen reader users

Elements like <nav> and <main> become landmarks. Users can jump straight to "main content" or "navigation" with a single key instead of listening to the whole page.

Search engines

Clear structure helps crawlers understand which part is the main content and which is menus, ads or footers.

Browsers and tools

Reader mode, translation tools and voice assistants use the structure to extract the real content.

Developers

</article> is easier to read than the fifth </div> in a row. Less need for ids and classes just to understand the layout.

Keyboard users

Real <button> and <a> elements are focusable and work with Enter/Space automatically; a clickable <div> is not.

And it costs nothing: these elements need no extra code, and they have worked in all browsers for over a decade.


The Page Layout Elements

Here is how a typical page maps onto semantic elements:

┌─────────────────────────────────────────────┐
│ <header>   logo, site name                  │
│   <nav>    Home | Tutorials | Practice      │
├───────────────────────────────┬─────────────┤
│ <main>                        │ <aside>     │
│   <article>                   │  Related    │
│     <header> title, date      │  tutorials  │
│     <section> Part 1          │             │
│     <section> Part 2          │             │
│     <footer> author, tags     │             │
│   </article>                  │             │
├───────────────────────────────┴─────────────┤
│ <footer>   copyright, contact, links        │
└─────────────────────────────────────────────┘

Let's go through each element.

Introductory content for the page or for a section: logo, site title, a search box, the heading of an article with its date.

<header>
  <p><a href="/">CS Learning Portal</a></p>
  <nav>...</nav>
</header>

Don't confuse it with <head> (the invisible metadata section of the document) or with headings (<h1>–<h6>). A page can have several <header> elements – one for the site and one inside each article.

A block of major navigation links: the main menu, a table of contents, breadcrumbs, pagination.

<nav aria-label="Main">
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/tutorials">Tutorials</a></li>
    <li><a href="/practice">Practice</a></li>
  </ul>
</nav>

Not every group of links needs <nav>. A few links in the footer to "Privacy" and "Terms" can simply be a list. When a page has more than one <nav>, give each one a name with aria-label (e.g. "Main", "Breadcrumb") so screen reader users can tell them apart.

<main>

The dominant, unique content of the page – the part that changes from page to page. Everything repeated across the site (header, menus, footer, sidebars) stays outside it.

<main>
  <h1>Binary Search</h1>
  <p>Binary search finds an item in a sorted array ...</p>
</main>

Rules for <main>:

  • Only one visible <main> per page. (Extra ones are allowed only if they have the hidden attribute – used by some single-page apps.)

  • It must not be inside <article>, <section>, <aside>, <header>, <footer> or <nav>. The standard only allows it directly in <body>, or inside plain wrappers such as a <div> (or a <form> without an accessible name).

<article>

An <article> is a self-contained piece of content that would still make sense if you lifted it out and published it somewhere else.

Examples: a blog post, a news story, a forum post, a user comment, a product card, a tutorial chapter.

<article>
  <header>
    <h2>Understanding Recursion</h2>
    <p>Published <time datetime="2026-10-28">28 Oct 2026</time></p>
  </header>
  <p>Recursion is when a function calls itself ...</p>
  <footer>
    <p>Tags: C++, Functions</p>
  </footer>
</article>

Articles can be nested: the comments under a blog post are usually <article> elements inside the post's <article>, because each comment is a complete unit by itself, related to the post.

<section>

A <section> is a thematic grouping of content that normally has its own heading – like a chapter of a book or a tab of a settings page.

<article>
  <h1>Sorting Algorithms</h1>
  <section>
    <h2>Bubble Sort</h2>
    <p>...</p>
  </section>
  <section>
    <h2>Merge Sort</h2>
    <p>...</p>
  </section>
</article>

A good test: can you give this block a heading? If yes, <section> may fit. If you only need a box for styling (a coloured background, a flex container), use <div> instead – <section> is not a generic wrapper.

<aside>

Content that is related but secondary to the content around it – it could be removed without hurting the main text. Sidebars, "related tutorials", a glossary box, a pull-quote, an advertisement.

<aside>
  <h2>Related tutorials</h2>
  <ul>
    <li><a href="/dsa/linear-search">Linear Search</a></li>
    <li><a href="/dsa/two-pointers">Two Pointers</a></li>
  </ul>
</aside>

Information about its page or section: copyright, author, contact links, related links, tags. Like <header>, it can appear once for the whole page and again inside articles. It doesn't have to be at the visual bottom, though it usually is.

<footer>
  <p>© 2026 CS Learning Portal</p>
  <p><a href="/privacy">Privacy</a> · <a href="/contact">Contact</a></p>
</footer>

Choosing Between article, section, aside and div

This is where most confusion happens, so here's a decision flow you can follow:

Does the block make sense on its own,
if published somewhere else?
    yes → <article>
    no  ↓
Is it related but secondary / removable
(sidebar, note, ad)?
    yes → <aside>
    no  ↓
Is it a distinct topic that deserves a heading?
    yes → <section>
    no  ↓
Is it only needed for styling or scripting?
    yes → <div>

Content

Best element

Why

A blog post in a list of posts

<article>

Complete, shareable unit

"Features" part of a product landing page

<section>

A themed group with its heading

"You might also like" box

<aside>

Tangential to the main content

A wrapper to centre content with CSS

<div>

No meaning, only styling

A user review on a product page

<article>

Self-contained, has author and date

The main menu

<nav>

Major navigation


Landmarks: What Screen Readers See

Browsers map several of these elements to ARIA landmark roles. Assistive technologies list these landmarks so users can jump between them.

Element

Landmark role

Condition

<header>

banner

Only when it is not inside <article>, <aside>, <main>, <nav> or <section>

<nav>

navigation

Always

<main>

main

Always

<aside>

complementary

When it's at page level or directly in <main>; inside <article>, <aside>, <nav> or <section> only if it is given a name (e.g. aria-label)

<footer>

contentinfo

Same rule as <header>: only the page-level footer

<section>

region

Only if it has an accessible name (e.g. aria-labelledby pointing to its heading)

<form>

form

Only if it has an accessible name

<search>

search

Always

So an article's own <header> and <footer> are not landmarks – that's exactly right, because a page with twenty blog posts shouldn't have twenty "banner" landmarks. Because the native elements already carry these roles, you should not add role="main" to <main> or role="navigation" to <nav>. The chapter Accessibility Basics goes deeper into ARIA.


Headings Still Do the Real Work

Older tutorials (and some textbooks) describe an "HTML5 outline algorithm" where every <section> resets heading levels, so you could use <h1> everywhere. No browser or screen reader ever exposed that outline to users, and it was removed from the HTML standard in 2022.

The structure of your document is defined by the heading levels you write. Use one <h1> for the page's main title, then <h2>, <h3> ... in order, whether or not they are inside sections.

<!-- Good: levels reflect the hierarchy -->
<main>
  <h1>Operating Systems</h1>
  <section>
    <h2>Process Scheduling</h2>
    <section>
      <h3>Round Robin</h3>
    </section>
  </section>
</main>

Smaller Semantic Elements

<figure> and <figcaption>

A self-contained illustration – image, diagram, code listing, chart – with an optional caption. The caption belongs to the figure, which is clearer than a paragraph that just happens to sit below an image.

<figure>
  <img src="bst.png" alt="Binary search tree with root 8, left child 3 and right child 10" width="400" height="250">
  <figcaption>Figure 1: A binary search tree</figcaption>
</figure>

<time>

Marks a date or time so that machines can read it, using the datetime attribute in a standard format, while humans read the text inside.

<p>Results will be declared on <time datetime="2026-12-20">20 December 2026</time>.</p>
<p>Lab starts at <time datetime="14:00">2 pm</time>.</p>
<p>Duration: <time datetime="PT2H30M">2½ hours</time></p>

<address>

Contact information for the nearest <article> or the whole page – the author's email, a support phone number, the office location of the site owner. It is not for every postal address that appears in text (an address in a list of exam centres is just a paragraph).

<footer>
  <address>
    Questions? Write to <a href="mailto:help@example.com">help@example.com</a>
  </address>
</footer>

A newer element (supported in all major browsers since late 2023) that wraps a search or filter feature. It creates a search landmark, replacing the old <form role="search"> pattern.

<search>
  <form action="/search">
    <label for="q">Search tutorials</label>
    <input type="search" id="q" name="q">
    <button type="submit">Search</button>
  </form>
</search>

It is for the search controls, not for showing the results – results belong in <main>.

Text-level semantics (recap)

Semantics also apply inside sentences. You met most of these in Text Formatting:

Use

Instead of

Meaning

<strong>

<b> for important text

Strong importance

<em>

<i> for stressed words

Stress emphasis

<mark>

A yellow-background <span>

Highlighted / relevant text

<code>

A monospace <span>

Computer code

<abbr title="...">

Plain text

Abbreviation with its expansion


Before and After: Refactoring Div Soup

Here is a typical non-semantic page:

<div id="header">
  <div class="logo">CodeCamp</div>
  <div id="menu">
    <a href="/">Home</a> <a href="/courses">Courses</a>
  </div>
</div>
<div id="content">
  <div class="post">
    <div class="title">Learn Git in 30 Minutes</div>
    <div class="date">2 Nov 2026</div>
    <div class="text">Git is a version control system ...</div>
  </div>
</div>
<div id="sidebar">Popular posts ...</div>
<div id="footer">© 2026 CodeCamp</div>

And the same page, rewritten semantically:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Learn Git in 30 Minutes – CodeCamp</title>
</head>
<body>
  <header>
    <p><a href="/">CodeCamp</a></p>
    <nav aria-label="Main">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/courses">Courses</a></li>
      </ul>
    </nav>
  </header>

  <main>
    <article>
      <header>
        <h1>Learn Git in 30 Minutes</h1>
        <p>Posted on <time datetime="2026-11-02">2 Nov 2026</time></p>
      </header>
      <p>Git is a version control system that tracks changes to your files.</p>
      <section>
        <h2>Installing Git</h2>
        <p>Download Git from its official website and run the installer.</p>
      </section>
      <section>
        <h2>Your First Commit</h2>
        <p>Use <code>git add</code> and then <code>git commit</code>.</p>
      </section>
    </article>
  </main>

  <aside>
    <h2>Popular posts</h2>
    <ul>
      <li><a href="/posts/branches">Git Branches Explained</a></li>
    </ul>
  </aside>

  <footer>
    <p>© 2026 CodeCamp</p>
  </footer>
</body>
</html>

Open the semantic version in Chrome without any CSS and you see plain black text and blue underlined links: the "CodeCamp" link, a bulleted menu (Home, Courses), a large "Learn Git in 30 Minutes" heading, the date line, two smaller sub-headings with their paragraphs, the "Popular posts" heading with its one-item list, and the copyright line. Those visual differences from the div version come only from the headings and the list – the region elements themselves have almost no default styling: <header>, <main>, <article>, <section>, <aside> and <footer> are simply displayed as blocks, exactly like <div>. The difference is invisible to the eye but very visible to software: the second version exposes banner, navigation, main, complementary and contentinfo landmarks, a clear heading hierarchy and a machine-readable date.


Common Mistakes

  • Using <section> as a generic wrapper for styling. Use <div> when there's no theme or heading.

  • More than one visible <main>, or putting <main> inside <article> or <header>.

  • Wrapping every group of links in <nav>. Keep it for major navigation blocks.

  • Choosing elements for their look. Using <h3> because "it's the right size", or <blockquote> just to indent text. Pick the element by meaning; fix the look with CSS.

  • Confusing <header> with <head> or with heading elements.

  • Using <address> for any postal address in the content.

  • Adding redundant roles like <nav role="navigation">.

  • Making clickable <div>s instead of using <button> or <a>.


Interview Questions

1. What is semantic HTML and why is it important?

Using elements according to their meaning (<nav>, <article>, <button>, headings) rather than generic <div>s. It improves accessibility (landmarks, keyboard support), helps search engines and tools understand the page, and makes code easier to maintain.

2. Difference between <article> and <section>?

An <article> is self-contained and would make sense on its own (a post, a comment). A <section> is a thematic part of something larger and usually has a heading. Articles can contain sections, and sections can contain articles.

3. Difference between <section> and <div>?

<section> carries meaning – a themed group, typically with a heading. <div> has no meaning and is used purely for styling or scripting hooks.

4. Can a page have more than one <header> or <footer>? More than one <main>?

Yes to multiple headers and footers (e.g. one per article). Only one <main> may be visible; any additional ones must have the hidden attribute.

5. Is an article's <header> a banner landmark?

No. <header> maps to the banner role only when it isn't inside <article>, <aside>, <main>, <nav> or <section>. Inside those it's a plain grouping element.

6. Does using <section> change heading levels automatically?

No. The "outline algorithm" that suggested this was never implemented and was removed from the standard in 2022. Use correct <h1>–<h6> levels yourself.

7. What is the purpose of the datetime attribute on <time>?

It provides a machine-readable version (e.g. 2026-12-20) of a human-readable date like "20 December 2026", useful for search engines, calendars and scripts.


Conclusion

Semantic HTML describes what content is, not how it looks.

<header>  → intro / branding (page or section)
<nav>     → major navigation links
<main>    → the unique content of the page (one visible)
<article> → self-contained, shareable content
<section> → themed group with a heading
<aside>   → related but secondary content
<footer>  → info about the page or section
<figure>, <time>, <address>, <search> → smaller meaningful pieces
<div>     → only when nothing else fits
  • Semantic elements look almost the same as <div>s, but give structure to screen readers, search engines and developers.

  • Page-level <header>, <nav>, <main>, <aside> and <footer> become landmarks.

  • Headings define the document outline; sections don't change heading levels.

In the next chapter, Block vs Inline Elements: div & span, we'll see how elements are laid out on screen and when the meaning-free <div> and <span> are the right tools.