Skip to main content
Saved

Pattern Fundamentals Beginner

Semantic HTML

Using HTML elements for the job they were made for gives you accessibility and SEO benefits without writing extra code.

Examples
native web
Read
4 min

The problem

I once audited a site where everything was a div, including the buttons, the navigation, the main content, and the footer. There was nothing in the markup to tell a screen reader which part was which, so the whole page sounded like one long, unstructured blob.

The pattern

Each HTML element has a job, and using each element for its job sounds obvious, but we forget to do it all the time.

Problem

Screen reader users move around a page by jumping between landmarks, such as the main content or the navigation, but when everything is a div, there are no landmarks. Users have to listen through your entire page from the top to find what they need.

Those div buttons don’t work with a keyboard either, because Tab skips past them and Enter does nothing. Screen readers don’t announce them as interactive, so users don’t know they can click them.

Solution

<button> is for clickable actions, <a> for links, <nav> for navigation, <main> for primary content, and <header> and <footer> for the top and bottom of a page or section. <article> is for standalone content, <aside> for secondary content, and <h1> through <h6> for heading hierarchy. <table> is for tabular data, not layout, and <form> with proper <label> elements is for inputs.

If you use the right element, the browser handles the accessibility for you. Screen readers announce a <button> as “button”, and the button responds to the Enter and Space keys, without any JavaScript or ARIA.

A <div> or a <span> is still the right choice for a wrapper that’s only there for layout or styling. If no element describes the content, wrapping it in a <section> or an <article> doesn’t add anything useful.

Example

Semantic Document Structure

The Problem’s page had markup like this. The class names tell a developer which part is which, but a screen reader finds no landmarks and no headings in it.

<div class="header">
  <div class="title">Site Title</div>
  <div class="nav">...</div>
</div>
<div class="content">...</div>
<div class="sidebar">...</div>
<div class="footer">...</div>

The version below uses semantic elements for the same structure, and the comments show which landmark each one becomes. A page with two <nav> elements gives screen reader users two landmarks that are both announced as “navigation”, so each one gets an aria-label to tell them apart. Those two labels are the only ARIA in the page.

<body>
  <!-- A header directly inside body is the banner landmark -->
  <header>
    <h1>Site Title</h1>
    <!-- Navigation landmark, announced as "Primary navigation" -->
    <nav aria-label="Primary">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
      </ul>
    </nav>
  </header>

  <!-- Main landmark -->
  <main>
    <article>
      <!-- A header inside an article isn't a landmark. It groups the article's title and date -->
      <header>
        <h2>Article Title</h2>
        <time datetime="2026-01-01">January 1, 2026</time>
      </header>
      <p>Introduction paragraph...</p>
      <h3>First Section</h3>
      <p>Section content...</p>
      <figure>
        <img src="chart.png" alt="Sales data visualization">
        <figcaption>Q4 2025 Sales Performance</figcaption>
      </figure>
    </article>
  </main>

  <!-- Complementary landmark -->
  <aside>
    <h2>Popular Posts</h2>
    <ul>
      <li><a href="/post-1">First Post</a></li>
    </ul>
  </aside>

  <!-- A footer directly inside body is the contentinfo landmark -->
  <footer>
    <p>&copy; 2026 Company Name</p>
    <nav aria-label="Footer">
      <a href="/privacy">Privacy</a>
      <a href="/terms">Terms</a>
    </nav>
  </footer>
</body>

Semantic Interactive Elements

Native elements come with keyboard and screen reader support, and you’d need a lot of code to copy that support in a div-based version.

<!-- Div button requires extensive ARIA and JavaScript -->
<div role="button" tabindex="0" onclick="handleClick()"
     onkeydown="handleKeyPress(event)">Submit</div>

<!-- Semantic button works automatically -->
<button type="submit">Submit</button>

<!-- Div link breaks keyboard nav and right-click -->
<div class="link" onclick="navigate('/page')">Go to page</div>

<!-- Semantic link has full browser features -->
<a href="/page">Go to page</a>

Semantic Form Elements

With proper form markup, you get built-in validation, and screen readers can announce the labels and groups.

<form>
  <label for="username">Username</label>
  <input id="username" type="text" required>

  <fieldset>
    <legend>Shipping Address</legend>
    <label for="street">Street</label>
    <input id="street" type="text">
    <label for="city">City</label>
    <input id="city" type="text">
  </fieldset>

  <button type="submit">Submit</button>
  <button type="button">Cancel</button>
</form>

Semantic Data Presentation

In a table with proper headers, screen readers can announce which headers each cell belongs to. The <details> and <summary> elements give you an accessible section that users can expand and collapse.

<table>
  <caption>Monthly Sales Report</caption>
  <thead>
    <tr>
      <th scope="col">Month</th>
      <th scope="col">Revenue</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">January</th>
      <td>$50,000</td>
    </tr>
  </tbody>
</table>

<details>
  <summary>Advanced Settings</summary>
  <p>Additional configuration options...</p>
</details>

Benefits

  • Screen readers announce “navigation landmark” or “button” without you adding any ARIA attributes.
  • Search engines can parse your content structure from the headings and landmarks, which helps SEO.
  • Keyboard navigation works by default, and buttons respond to Enter and Space without custom JavaScript.
  • Reader mode and text-to-speech features can parse your document structure correctly.
  • You write less code, because a <button> does in one line what takes about twenty lines with a div, role="button", tabindex, and keyboard handlers.
  • The code documents itself, because <nav> tells you it’s navigation, and <div class="nav-wrapper"> doesn’t tell you much.

Tradeoffs

  • You have to learn HTML properly. There are dozens of semantic elements, and it takes time to learn when to use each one.
  • Default browser styles often clash with your designs, and buttons come with borders and backgrounds you’ll need to reset.
  • Some elements have rules you might not expect. For example, <address> is for contact information about the page or its nearest <article>, and not for any postal address that appears in the content.
  • Heading levels follow hierarchy rules that can conflict with visual design, so you can’t jump from <h1> to <h4> just because you want smaller text.
  • Many CSS frameworks are built around div-based structures, and third-party components render divs that you can’t change without forking them.
  • If your codebase is already div soup, retrofitting semantic HTML is painful, and it’s easier to start with the right elements than to fix them later.
  • You can’t tell whether the markup is semantically correct by looking at the page in a browser, so you need accessibility audits and screen reader testing to check it.

Summary

If you use HTML elements for the job they were made for, you get accessibility and SEO benefits without extra code. A <button> works with keyboards and screen readers automatically, but a styled <div> needs a lot of JavaScript and ARIA to get close to the same behavior. I suggest picking the right element first, and only adding ARIA where HTML doesn’t have an element for the job.

See also

Next in Fundamentals Separation of Concerns Splitting code into separate layers for data, logic, presentation, and styling makes an app easier to change.

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.