Write Meaningful, Accessible & Maintainable UI Components
โก What Is Semantic HTML?
Semantic HTML means using HTML elements that describe their meaning and role rather than their appearance.
Instead of throwing <div>s everywhere, you use elements like <header>, <main>, <section>, or <article> that tell browsers what the content represents.
// โ Non-semantic
<div className="card">
<div className="title">Product Details</div>
<div className="image"></div>
<div className="desc">A lightweight wireless headset.</div>
<div className="actions">
<div className="price">$199</div>
<div className="btn">Buy Now</div>
</div>
</div>
// โ
Semantic
<article className="product-card">
<h2>Product Details</h2>
<figure>
<img src="/headset.png" alt="Wireless Headset" />
<figcaption>Wireless Headset</figcaption>
</figure>
<p>A lightweight wireless headset.</p>
<footer>
<p><strong>$199</strong></p>
<button>Buy Now</button>
</footer>
</article>
๐ก Why Itโs Important (Especially in React)
React gives you flexibility โ and sometimes that leads to div soup.
Semantic HTML ensures our UI is accessible, structured, and SEO-friendly.
Benefits
- ๐ฆฎ Accessibility: Screen readers use semantics to understand structure.
- ๐ SEO: Search engines understand your content hierarchy.
- ๐ง Maintainability: Developers can read and extend your layout easily.
- โ๏ธ Default Behavior: Semantic tags have built-in accessibility and roles.
๐งฑ Where It Fits in React Projects
Semantic HTML applies in:
- Layout components (
<header>,<main>,<footer>) - Navigation (
<nav>,<ul>,<li>) - Articles, blog posts (
<article>,<section>) - Lists, cards, data displays (
<ul>,<ol>,<table>) - Forms and controls (
<form>,<input>,<label>) - Interactive UI (
<button>,<a>)
๐ท๏ธ Commonly Used Semantic Tags (With React Context)
| Tag | Purpose | React Usage / Tip |
|---|---|---|
<header> |
Top banner or page section header | Use once per layout or section |
<main> |
Unique page content | Only one per page |
<footer> |
Bottom info / site-wide links | Use per layout |
<section> |
Logical grouping of related content | Wrap related UI blocks |
<article> |
Self-contained content (blog, card) | Ideal for standalone info |
<aside> |
Side content (ads, recommendations) | Outside <main>
|
<nav> |
Navigation menus | Wrap site navigation |
<h1>โ<h6>
|
Headings hierarchy | One <h1> per page |
<p> |
Paragraph text | For readable content |
<a> |
Links | Use for navigation, not actions |
<button> |
Trigger actions | Use for events or interactions |
<ul>, <ol>, <li>
|
Lists | Use for repeated data or menus |
<form>, <input>, <label>
|
Forms | Always pair label with input |
<img> |
Image | Always include alt
|
<figure>, <figcaption>
|
Visual + caption | For product/image details |
<table> |
Tabular data | Donโt use for layout |
<strong>, <em>
|
Emphasis / importance | Carry meaning, not just style |
<code>, <pre>
|
Code snippets | Great for docs or dev tools |
๐ Example: Semantic + React + Lists
export default function BlogList({ posts }) {
return (
<main>
<section>
<h1>Latest Blog Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>
<article>
<h2>{post.title}</h2>
<p>{post.summary}</p>
<a href={`/posts/${post.slug}`}>Read more</a>
</article>
</li>
))}
</ul>
</section>
</main>
);
}
โ Uses:
-
<main>โ defines the core page content -
<section>โ groups related items -
<ul>/<li>โ represent a list -
<article>โ self-contained post -
<a>โ navigational link
โ ๏ธ Common Mistakes Developers Make
-
Div soup โ
<div>everywhere instead of meaningful tags. -
Skipping heading hierarchy โ
<h1>โ<h4>breaks structure. -
Using
<span>for layout โ use<span>only for inline styling. -
Using
<div>for buttons/links โ breaks accessibility & keyboard nav. - Missing labels / alt text โ inaccessible for assistive tech.
-
Using
<br>for spacing โ use CSS, not<br>.
๐งฉ When To Use <div>, <span>, and Other Non-Semantic Tags
Not everything must be semantic โ sometimes you need non-semantic tags for structure, grouping, or styling.
Hereโs when theyโre perfectly fine ๐
๐ข โ
Use <div> when:
- You just need a generic container to group multiple elements.
- Thereโs no meaningful semantic alternative.
- Itโs purely for styling or layout with CSS or frameworks (e.g., flexbox/grid).
- Used as a React component wrapper.
Example: Layout container
<div className="card">
<h2>Product Title</h2>
<p>$199</p>
</div>
โ Reason: The container itself isnโt meaningful โ it just groups UI for layout.
Example: Wrapper for grid or flexbox
<div className="grid">
<section>Left Panel</section>
<section>Right Panel</section>
</div>
โ
Reason: The <div> controls layout, not semantics.
๐ก โ
Use <span> when:
- You need to style a small inline part of text.
- It doesnโt form a separate block or structure.
Example:
<p>
Hello, <span className="highlight">Vishwanath</span>! Welcome back.
</p>
โ
Reason: The <span> adds emphasis or style inline, not a structural meaning.
๐ต Other non-semantic tags
| Tag | Usage | Example |
|---|---|---|
<i> |
For icons / foreign text | <i className="fa fa-star"></i> |
<b> |
Bold text (no semantic weight) | <b>Note:</b> Read carefully |
<small> |
Small print text | <small>Terms apply</small> |
<br> |
Line break (rare use) | <p>Line1<br/>Line2</p> |
<hr> |
Thematic separator |
<hr/> between sections |
๐ง Tip: Use <div> or <span> only if you canโt answer the question โ
โIs there a more meaningful element for this?โ
๐ง How To Improve Existing React Codebase
Audit rendered HTML
โ Check with DevTools โ Elements โ look for unnecessary<div>s.Refactor step by step
<div className="header"> โ <header>
<div className="footer"> โ <footer>
<div className="nav"> โ <nav>
Keep
<div>s only for layout wrappers
e.g., CSS grids, flex containers, or animation wrappers.Use lists and headings properly
Group repeated elements under<ul>/<ol>and keep heading order.Validate & test accessibility
Run W3C Validator or Lighthouse accessibility audits.
๐ง Quick Semantic Checklist for React Devs
โ
One <main> per page
โ
Proper heading order (h1 โ h2 โ h3)
โ
<ul> / <ol> for collections
โ
<button> for actions
โ
<a> for navigation
โ
<label htmlFor> for inputs
โ
<img alt=""> for all images
โ
<div> only when no semantic alternative
โ
<span> only for inline styling
๐ Final Thoughts
Semantic HTML isnโt โextra workโ โ itโs what makes your React code:
- Understandable
- Accessible
- SEO-friendly
- Future-proof
Using <div> or <span> isnโt wrong โ itโs about intent.
If an element adds structure or meaning, make it semantic.
If itโs just styling or grouping, <div> and <span> are fine.
๐ฃ๏ธ โUse
<div>when you mean a box.
Use semantic tags when you mean a thing.โ
Top comments (0)