Semantic HTML
Semantic means "having meaning". Semantic HTML uses elements that describe what content is, not how it looks. A <div> says nothing; a <nav> says "this is navigation".
Why it matters
- Accessibility: screen readers let users jump straight to the
<main>content, the<nav>, or the next heading. - SEO: search engines understand your page structure better.
- Maintainability:
<article>is far easier to read than<div class="post-wrapper-2">. - Free features: reader mode, browser outlines and keyboard behaviour all rely on semantics.
"Div soup" vs semantic HTML
html
<!-- ✗ Div soup -->
<div class="header">
<div class="logo">MySite</div>
<div class="menu">…</div>
</div>
<div class="content">
<div class="post">…</div>
</div>
<div class="footer">…</div>The landmark elements
Preview
| Element | Use it for |
|---|---|
<header> |
Introductory content for the page or a section (logo, title, nav) |
<nav> |
Major navigation link blocks |
<main> |
The main content: one per page, unique to that page |
<article> |
Self-contained content that makes sense on its own (blog post, product card, comment) |
<section> |
A thematic group of content, normally with a heading |
<aside> |
Related but secondary content (sidebar, callout) |
<footer> |
Footer for the page or a section |
Choosing the right element
Ask in order:
- Is it navigation? →
<nav> - Could it be shared on its own (syndicated, embedded)? →
<article> - Is it a themed group with a heading? →
<section> - Is it tangential to the main content? →
<aside> - None of the above, and it's just for styling? →
<div>(that's fine!)
<div> and <span> aren't bad. They're the right choice when no semantic element fits: wrappers for layout and styling hooks.
Other meaningful elements
Preview
Buttons vs links
This is one of the most common mistakes in real codebases:
- Link
<a href>: goes somewhere (a new URL or page). - Button
<button>: does something (submits, opens a menu, toggles, deletes).
html
<!-- ✗ Never: not keyboard accessible, no role, no focus -->
<div onclick="save()">Save</div>
<!-- ✓ Free keyboard support (Enter/Space), focus, and screen-reader role -->
<button type="button" onclick="save()">Save</button>Practice
Convert this div soup into semantic HTML (keep the content the same):
Preview
✎ Check your understanding
1.How many <main> elements should a page normally have?
2.Clicking an element deletes an item. Which element should it be?