Skip to main content

Semantic HTML

In our previous lessons, we used tags like <div> and <span>. These are non-semanticโ€”they tell the browser nothing about the content inside them.

Semantic HTML uses tags that clearly describe their meaning to both the browser and the developer.

1. Why Semantics Matter?โ€‹

Imagine looking at a newspaper where every font size was exactly the same. You wouldn't know what the headline is, where the article starts, or where the footer is.

The Three Pillars of Semantics:โ€‹

  1. Accessibility: Screen readers use these tags to help visually impaired users navigate your site (e.g., "Skipping to main content").
  2. SEO (Search Engine Optimization): Google gives higher rankings to pages where the important content is wrapped in semantic tags.
  3. Readability: Itโ€™s much easier for a teammate to read <header> than <div class="top-part-of-site">.

2. The Semantic Structureโ€‹

Instead of using <div> for everything, we use specific tags for specific areas of the page.

3. Key Semantic Tagsโ€‹

TagPurpose
<header>The introductory content (logo, search, nav).
<nav>A block of navigation links.
<main>The unique, central content of the document.
<footer>The "bottom" information (legal, social links).

4. Comparison: Div Soup vs. Semanticโ€‹

The "Div Soup" (Bad Practice)โ€‹

<div id="header">
<div id="nav">...</div>
</div>
<div id="content">
<div class="post">...</div>
</div>
<div id="footer">...</div>

Semantic HTML (CodeHarborHub Standard)โ€‹

<header>
<nav>...</nav>
</header>
<main>
<article>...</article>
</main>
<footer>...</footer>

Let's Practiceโ€‹

Refactor your current index.html project. Wrap your title and navigation in a <header>, put your main lists and text inside a <main> tag, and add a small <footer> with your name and the current year.

The "Article" Test

If you can take a piece of content, put it on a completely different website, and it still makes sense on its own, it should probably be an <article>. If it only makes sense as part of the page it's on, use a <section>.

Accessibility

When you use a <button> tag instead of a <div> with a click listener, you automatically get keyboard support (users can hit "Enter" to click). This is the power of using the correct semantic tag!