HTMLHTML · Lesson 6 of 8

Semantic HTML

Semantic HTML means using the right element for the right job. Not just div and span everywhere. Browsers, search engines, and assistive technologies all understand semantic HTML.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Semantic HTML Example</title>
</head>
<body>

  <!-- Site header with navigation -->
  <header>
    <h1>My Blog</h1>
    <nav aria-label="Main navigation">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
        <li><a href="/contact">Contact</a></li>
      </ul>
    </nav>
  </header>

  <!-- Main content area -->
  <main>

    <!-- A self-contained article (could be shared independently) -->
    <article>
      <header>
        <h2>Learning Rust in 2024</h2>
        <p>By <address><a href="/author/alice">Alice</a></address>
           on <time datetime="2024-01-15">January 15, 2024</time></p>
      </header>

      <p>Rust's ownership model is unlike anything in most languages...</p>

      <!-- A section within the article -->
      <section>
        <h3>The Borrow Checker</h3>
        <p>At first, the borrow checker feels like an obstacle...</p>
      </section>

      <footer>
        <p>Tags: <a href="/tags/rust">Rust</a>, <a href="/tags/systems">Systems</a></p>
      </footer>
    </article>

  </main>

  <!-- Sidebar content related to, but not part of, main content -->
  <aside>
    <h2>Related Posts</h2>
    <ul>
      <li><a href="/post/2">Learning Go</a></li>
      <li><a href="/post/3">C vs C++</a></li>
    </ul>
  </aside>

  <!-- Site footer -->
  <footer>
    <p>&copy; 2024 My Blog. All rights reserved.</p>
  </footer>

</body>
</html>
◆ Note
Use <main> once per page for primary content. Use <article> for self-contained content that could be republished. Use <section> for thematic groupings within a page or article. Use <aside> for tangentially related content. Avoid <div> when a semantic element fits.