Sobes.tech
Junior

Why can't we use regular div tags instead of section and header tags?

sobes.tech AI

Answer from AI

Using semantic tags <section> and <header> instead of the universal <div> improves accessibility and code readability.

  1. Accessibility: Screen readers and other assistive technologies use semantic tags to understand the content structure. <header> indicates an introductory part (e.g., logo, navigation), <section> is a thematic block. <div> by itself does not carry semantic meaning.

  2. SEO (Search Engine Optimization): Search engines better index content structured with semantic tags, which can improve ranking.

  3. Code readability and maintainability: Semantic tags self-document the structure. A developer reading the code immediately understands the purpose of the block without delving into styles or comments.

In summary: <div> is suitable for grouping elements when semantics are not important (e.g., for styling). <section> and <header> carry meaningful content and make web pages more understandable both for machines and humans.

Example:

<!-- Semantically correct structure -->
<header>
  <h1>Site Name</h1>
  <nav>
    <ul>
      <li><a href="#">Link</a></li>
    </ul>
  </nav>
</header>

<section>
  <h2>Section Title</h2>
  <p>Section content...</p>
</section>

<!-- Example of using div, which does not convey semantics -->
<div> <!-- header? section? unknown -->
  <h1>Site Name</h1>
  <div> <!-- nav? -->
    <ul>
      <li><a href="#">Link</a></li>
    </ul>
  </div>
</div>

<div> <!-- section? -->
  <h2>Section Title</h2>
  <p>Section content...</p>
</div>