liveBook · Manning

Semantic markup

As explained in Vanilla Web

The practice of using HTML elements according to their meaning to represent a document's content and structure.

Start an interface with the content and its structure, not the layout you want on screen. Semantic markup uses HTML elements to express what content means and how its parts relate. CSS can then control appearance, while JavaScript adds behavior. section 3.4 section 2.1.5

Choose elements by purpose:

  • Use <header>, <main>, <nav>, and <footer> for applicable page regions; use <section> and <article> to group content where they fit. section 3.4.2
  • Use headings, paragraphs, and lists for the content they describe, rather than anonymous containers with classes that merely suggest meaning. section 3.4.2
  • Prefer native controls such as <button> for interaction. They provide semantics, keyboard interaction, and focus behavior without recreating those features. section 3.5.7

Replace anonymous containers

Instead of <div id="header"><div class="title">Coffee Roasters</div></div>, use <header><h1>Coffee Roasters</h1></header>. Put navigation links in <nav> and, when they form a list, mark them up with <ul> and <li>. The elements communicate their roles without relying on class names to explain the document. section 3.4.2

Listing 3.3 A simple markup following semantic HTML best practices
<header>
  <h1>Coffee Roasters</h1>
</header>
<main>
<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/products">Products</a></li>
  </ul>
</nav>
</main>

A short page fragment shows a heading, main region, and list of navigation links expressed with native elements. section 3.4.2

A <div> remains useful when no semantic container fits; the problem is making it the default for everything. Meaningful elements make the document easier to navigate and maintain and give assistive technology and crawlers more context. Use ARIA to fill gaps, not to replace an element that already expresses the intended role. Begin with useful HTML that renders before JavaScript, then enhance it. section 3.4.2 section 3.5 section 14.7.2

How Vanilla Web covers it

How deep it goes
Defines the practice and compares element choices with div-heavy markup; does not teach HTML from scratch.
Examples
Contrasts a div-heavy page fragment with a header, heading, main region, and list of navigation links.
What it assumes
Assumes basic familiarity with HTML elements rather than teaching HTML from scratch.
What you can do after
Apply it by choosing native elements for page regions, content, and controls before adding CSS, JavaScript, or ARIA.
How it connects
Connects semantic HTML to accessibility, ARIA, CSS, progressive enhancement, and discoverability.

Also explained in

Compare how 7 books explain semantic markup →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.