Semantic HTML

Meaningful HTML5 elements for better structure

What is Semantic HTML?

Semantic HTML uses elements that clearly describe their meaning and purpose to both browsers and developers. Instead of using generic <div> and <span> for everything, semantic elements tell browsers, screen readers, and search engines exactly what type of content they contain.

Think of it like labeling boxes when moving: "Kitchen Supplies" is more helpful than "Box #3". Semantic HTML does the same for your webpage structure.

Non-Semantic vs Semantic Comparison

❌ Non-Semantic (Bad)

<div id="header">
  <div id="logo">Logo</div>
  <div id="nav">
    <div>Home</div>
    <div>About</div>
  </div>
</div>

<div id="content">
  <div class="post">
    <div class="title">Title</div>
    <div class="text">...</div>
  </div>
</div>

<div id="sidebar">...</div>
<div id="footer">...</div>

Everything is a div—no meaning!

✓ Semantic (Good)

<header>
  <h1>Logo</h1>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>
</header>

<main>
  <article>
    <h2>Title</h2>
    <p>...</p>
  </article>
</main>

<aside>...</aside>
<footer>...</footer>

Clear, meaningful structure!

Semantic Page Layout

Here's how semantic elements create a well-structured page:

<header> - Site header, logo, main navigation
<main> - Primary page content
<article> - Self-contained content
<section> - Thematic grouping
<aside> - Sidebar
<footer> - Site footer, copyright, links
<header>
  <h1>Site Name</h1>
  <nav>
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/about">About</a></li>
    </ul>
  </nav>
</header>

<main>
  <article>
    <h2>Article Title</h2>
    <section>
      <h3>Section Heading</h3>
      <p>Content...</p>
    </section>
  </article>

  <aside>
    <h3>Related Links</h3>
    <!-- Sidebar content -->
  </aside>
</main>

<footer>
  <p>© 2024 Company Name</p>
</footer>

Key Semantic Elements Explained

<header>

Introductory content or navigation. Can be used for the page header OR for headers within articles/sections.

<header>
  <h1>My Website</h1>
  <nav>...</nav>
</header>

<nav>

Navigation links. Use for major navigation blocks (not for every single link).

<nav aria-label="Main navigation">
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
  </ul>
</nav>

<main>

The dominant content of the page. Only ONE per page! Skip repetitive content like headers/footers.

<main>
  <h1>Page Title</h1>
  <!-- Main content here -->
</main>

<article>

Self-contained content that could stand alone: blog posts, news articles, forum posts, comments.

<article>
  <h2>Blog Post Title</h2>
  <time datetime="2024-01-15">Jan 15, 2024</time>
  <p>Article content...</p>
</article>

<section>

Thematic grouping of content. Usually has a heading. Use when there's no more specific element.

<section>
  <h2>Our Services</h2>
  <p>Content about services...</p>
</section>

<aside>

Content tangentially related to main content: sidebars, pull quotes, related links.

<aside>
  <h3>Related Articles</h3>
  <ul>...</ul>
</aside>

<footer>

Footer for page or section. Copyright, contact info, related links.

<footer>
  <p>© 2024 Company</p>
  <nav>...</nav>
</footer>

Special Semantic Elements

<figure> & <figcaption>

Self-contained content like images, diagrams, code snippets with optional captions.

Sales chart
Q4 2024 Sales Performance
<figure>
  <img src="chart.png" alt="Sales chart" />
  <figcaption>Q4 2024 Sales Performance</figcaption>
</figure>

<!-- Works for code too! -->
<figure>
  <pre><code>const x = 10;</code></pre>
  <figcaption>JavaScript variable declaration</figcaption>
</figure>

<details> & <summary>

Interactive accordion/collapsible content—no JavaScript required!

Click to expand

This content is hidden until you click the summary!

Another expandable section

You can have multiple details elements.

<details>
  <summary>Click to expand</summary>
  <p>Hidden content revealed on click.</p>
</details>

<!-- Open by default -->
<details open>
  <summary>Starts expanded</summary>
  <p>Content visible initially.</p>
</details>

<time>

Machine-readable dates and times for better SEO and accessibility.

Published:

Meeting at:

<!-- Date only -->
<time datetime="2024-12-25">Christmas Day</time>

<!-- Time only -->
<time datetime="14:30">2:30 PM</time>

<!-- Full datetime -->
<time datetime="2024-01-01T00:00:00Z">
  New Year 2024
</time>

<mark>

Highlighted or marked text (like a highlighter pen).

Search results for "HTML": HTML is the foundation of the web.

<p>Most important: <mark>This part</mark></p>

💡 Why Semantic HTML Matters

  • 🔍 SEO: Search engines better understand your content structure and rank you higher
  • ♿ Accessibility: Screen readers can navigate and understand your page properly
  • 📱 Consistency: Semantic elements have predictable default behaviors across browsers
  • 🛠️ Maintainability: Code is self-documenting—easier for you and other developers
  • 🎯 Future-proof: New devices and technologies can better interpret semantic markup

✓ When to Use What

  • <header>: Logo, site title, main navigation
  • <nav>: Primary site navigation, breadcrumbs, table of contents
  • <main>: Primary page content (only ONE per page!)
  • <article>: Blog posts, news articles, user comments, forum posts
  • <section>: Chapters, tabbed content, thematic groupings
  • <aside>: Sidebars, related links, pull quotes, advertising
  • <footer>: Copyright, authorship, related links
  • <div>: Only when no semantic element fits (purely for styling/layout)