html accessibility web-standards frontend

Understanding Semantic HTML

Semantic HTML is about using the right tag for the job. It improves accessibility, SEO, and maintainability.

What is Semantic HTML?

Compare these two approaches:

❌ Non-Semantic

<div class="header">
  <div class="nav">
    <div class="nav-item">Home</div>
  </div>
</div>
<div class="main">
  <div class="article">
    <div class="title">My Article</div>
    <div class="content">...</div>
  </div>
</div>

✅ Semantic

<header>
  <nav>
    <a href="/">Home</a>
  </nav>
</header>
<main>
  <article>
    <h1>My Article</h1>
    <p>...</p>
  </article>
</main>

Key Semantic Elements

Document Structure

<header>  <!-- Top of page or section -->
  <nav>   <!-- Navigation links -->
    <ul>
      <li><a href="/">Home</a></li>
    </ul>
  </nav>
</header>

<main>    <!-- Primary content -->
  <article> <!-- Self-contained content -->
    <section> <!-- Thematic grouping -->
      <h2>Section Title</h2>
    </section>
  </article>
  
  <aside>   <!-- Tangentially related content -->
    <h3>Related Links</h3>
  </aside>
</main>

<footer>  <!-- Footer information -->
  <p>&copy; 2025</p>
</footer>

Text Content

<h1> to <h6>  <!-- Headings (hierarchical) -->
<p>           <!-- Paragraphs -->
<blockquote>  <!-- Quoted content -->
<figure>      <!-- Self-contained content -->
  <img src="chart.png" alt="Sales chart">
  <figcaption>Q4 Sales Data</figcaption>
</figure>
<pre>         <!-- Preformatted text -->
<code>        <!-- Code snippets -->

Inline Text

<strong>  <!-- Important (bold) -->
<em>      <!-- Emphasis (italic) -->
<mark>    <!-- Highlighted text -->
<time>    <!-- Time/date -->
  <time datetime="2025-11-03">November 3, 2025</time>
<abbr>    <!-- Abbreviation -->
  <abbr title="HyperText Markup Language">HTML</abbr>

Why It Matters

1. Accessibility

Screen readers use semantic HTML to navigate:

<!-- Screen reader can announce: "navigation, 5 links" -->
<nav>
  <a href="/">Home</a>
  <a href="/about">About</a>
  <a href="/blog">Blog</a>
  <a href="/contact">Contact</a>
  <a href="/faq">FAQ</a>
</nav>

Users can jump between landmarks:

2. SEO Benefits

Search engines understand structure:

<article>
  <h1>Main Topic</h1>        <!-- Most important -->
  <h2>Subtopic 1</h2>        <!-- Secondary -->
    <h3>Detail A</h3>        <!-- Tertiary -->
    <h3>Detail B</h3>
  <h2>Subtopic 2</h2>
</article>

3. Maintainability

Semantic HTML is self-documenting:

<!-- Clear purpose without classes -->
<header>
  <nav aria-label="Primary navigation">
    ...
  </nav>
</header>

Forms

Semantic forms are crucial for accessibility:

<form>
  <fieldset>
    <legend>Personal Information</legend>
    
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>
    
    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>
    
    <label for="country">Country:</label>
    <select id="country" name="country">
      <option value="">Select...</option>
      <option value="us">United States</option>
      <option value="uk">United Kingdom</option>
    </select>
  </fieldset>
  
  <button type="submit">Submit</button>
</form>

Lists

Use the right list type:

Unordered Lists

<ul>  <!-- No particular order -->
  <li>Apples</li>
  <li>Bananas</li>
  <li>Oranges</li>
</ul>

Ordered Lists

<ol>  <!-- Specific order matters -->
  <li>Preheat oven</li>
  <li>Mix ingredients</li>
  <li>Bake for 30 minutes</li>
</ol>

Description Lists

<dl>  <!-- Term/definition pairs -->
  <dt>HTML</dt>
  <dd>HyperText Markup Language</dd>
  
  <dt>CSS</dt>
  <dd>Cascading Style Sheets</dd>
</dl>

ARIA When Needed

Use ARIA attributes to enhance semantics:

<!-- Current page indicator -->
<nav>
  <a href="/" aria-current="page">Home</a>
  <a href="/about">About</a>
</nav>

<!-- Expandable content -->
<button aria-expanded="false" aria-controls="details">
  Show Details
</button>
<div id="details" hidden>
  Additional information...
</div>

<!-- Loading state -->
<button aria-busy="true">
  Loading...
</button>

Common Mistakes

❌ Skipping Heading Levels

<h1>Title</h1>
<h3>Subtitle</h3>  <!-- Should be h2 -->

❌ Using div for Everything

<div class="button">Click me</div>  <!-- Use <button> -->
<div onclick="...">Link</div>       <!-- Use <a> -->
<a href="...">Click here</a>        <!-- Bad -->
<a href="...">Read our privacy policy</a>  <!-- Good -->

❌ Missing alt Text

<img src="chart.png">               <!-- Bad -->
<img src="chart.png" alt="2024 sales showing 20% growth">  <!-- Good -->

Testing Semantic HTML

Use these tools:

  1. Browser DevTools - Check document outline
  2. WAVE - Web accessibility evaluation tool
  3. axe DevTools - Accessibility testing
  4. Screen Reader - NVDA (Windows), VoiceOver (Mac)

Keyboard Navigation

Semantic HTML enables keyboard navigation:

The HTML5 Outline Algorithm

Structure content logically:

<body>
  <header>
    <h1>Site Name</h1>
  </header>
  
  <main>
    <article>
      <h1>Article Title</h1>  <!-- h1 in article context -->
      <section>
        <h2>Section</h2>
      </section>
    </article>
  </main>
</body>

Conclusion

Semantic HTML is:

Start using semantic HTML today. Your users, search engines, and future self will thank you.

Resources

Write semantic HTML. Make the web better. 🌐