Stop Using <div> for Everything: A Guide to Semantic HTML
We've all been there. You open a project, inspect the code, and it's just a sea of
<div> tags. <div id="header">,
<div class="nav">, <div class="article">. It works, sure. But
it's messy, hard to read, and bad for the web.
In this guide, I'm going to convince you to stop the "div soup" and start writing Semantic HTML. It's easier than you think, and the benefits are massive.
Why Should You Care?
You might be thinking, "If it looks the same to the user, why does it matter?" Here are three reasons:
- Accessibility: Screen readers rely on tags like
<nav>and<main>to help blind users navigate your site. A<div>tells them nothing. - SEO: Google is a robot. It reads your code, not your visual design. Semantic tags tell Google, "Hey, this is the main content," or "This is just a footer."
- Maintainability: Reading
<article>is much faster for a developer than hunting for a specific class name in a pile of divs.
The Alternatives (A Cheat Sheet)
Here are the most common <div> patterns I see, and what you should use instead.
1. The Header
Bad: <div id="header">
Good: <header>
Use this for the introductory content of a page or a section. It usually contains your logo and navigation.
2. The Navigation
Bad: <div class="menu">
Good: <nav>
Wrap your main links in this tag. You don't need it for every single link (like in a footer), but definitely for your primary menu.
3. The Main Content
Bad: <div id="content">
Good: <main>
There should be only one <main> tag per page. It wraps the unique content of that
specific page.
4. Articles and Sections
Bad: <div class="post">
Good: <article>
Use <article> for self-contained content that could stand alone (like a blog post
or a comment). Use <section> for grouping related content, usually with a
heading.
5. The Footer
Bad: <div id="footer">
Good: <footer>
Contains copyright info, contact links, and secondary navigation.
A Real World Example
Let's look at a before and after comparison.
Before (Div Soup)
<div id="header">
<div class="logo">My Site</div>
<div class="nav">...</div>
</div>
<div id="content">
<div class="post">
<div class="title">Hello World</div>
<div class="text">...</div>
</div>
</div>
<div id="footer">...</div>
After (Semantic Bliss)
<header>
<h1 class="logo">My Site</h1>
<nav>...</nav>
</header>
<main>
<article>
<h2>Hello World</h2>
<p>...</p>
</article>
</main>
<footer>...</footer>
Want to practice? Copy the "After" code above and paste it into our Live HTML Editor to see how it renders. (Spoiler: It looks the same, but the code is beautiful!)
Conclusion
You don't have to refactor your entire codebase today. But next time you reach for a
<div>, ask yourself: "Is there a better tag for this?" Your future self (and your
users) will thank you.