HTML5 Semantic Elements Explained

By Maulik Paghdal

25 Jun, 2024

•  6 minutes to Read

HTML5 Semantic Elements Explained

Introduction

HTML5 introduced semantic elements to improve the readability and structure of web pages. These elements provide meaningful content organization, making it easier for both developers and browsers to understand the structure of a webpage. In this article, we’ll explore why semantic HTML matters, the key elements introduced in HTML5, and best practices for using them.

What Are Semantic Elements?

Semantic elements clearly describe their purpose in a way that is easy to interpret for both humans and machines. Unlike non-semantic elements (such as <div> and <span>), which do not convey any meaning about their content, semantic elements enhance clarity and structure.

Example: Semantic vs Non-Semantic HTML

❌ Non-Semantic HTML (Less Readable)

<div id="main">
    <div class="header">Welcome to My Blog</div>
    <div class="content">This is an article about HTML5.</div>
    <div class="footer">© 2025 My Blog</div>
</div>

✅ Semantic HTML (More Readable & Accessible)

<main>
    <header>Welcome to My Blog</header>
    <article>This is an article about HTML5.</article>
    <footer>© 2025 My Blog</footer>
</main>

The second example makes it clear that <header> contains the page heading, <article> holds the main content, and <footer> contains copyright information.

Why Use Semantic HTML?

Using semantic elements offers multiple advantages, including accessibility, SEO, and better maintainability.

1. Improved Accessibility

  • Semantic tags help screen readers and other assistive technologies interpret the content.
  • Users with disabilities can navigate web pages more efficiently.
  • Example: <nav> informs assistive technologies that it contains navigation links.

2. SEO Benefits

  • Search engines use HTML structure to understand web pages better.
  • Properly structured pages rank higher in search results.
  • Example: <article> helps search engines recognize blog posts or news articles.

3. Code Readability and Maintainability

  • Well-structured code is easier to understand and edit.
  • Developers can quickly identify the purpose of each section.
  • Example: <section> organizes related content into logical groups.

Common HTML5 Semantic Elements and Their Uses

ElementPurposeExample Usage
<header>Defines the top section of a page or sectionWebsite logo, navigation menu
<nav>Contains navigation linksMenus, breadcrumbs
<section>Groups related contentBlog sections, feature sections
<article>Represents an independent piece of contentBlog posts, news articles
<aside>Holds related informationSidebars, ads, related links
<footer>Defines the bottom sectionCopyright, social links
<main>Represents the main content of the pageExcludes headers, footers, and sidebars
<figure> & <figcaption>Groups media elements with captionsImages, charts, illustrations

Best Practices for Using Semantic HTML

✅ Use Semantic Elements Whenever Possible

  • Instead of <div class="header">, use <header>.
  • Instead of <div id="main">, use <main>.

✅ Ensure Proper Nesting

  • A <header> should not be placed inside <footer>.
  • <main> should contain only the main content (not navigation or sidebars).

✅ Enhance Accessibility with ARIA Attributes

  • Use aria-label when needed for extra clarity.
  • Example: <nav aria-label="Main navigation">.

✅ Avoid Overuse of <div> and <span>

  • Use <section> instead of <div class="section"> where applicable.

Common HTML5 Semantic Elements

Let's explore some of the key HTML5 semantic elements, with examples and best practices.

1. header

The <header> element defines the introductory content, usually appearing at the top of the page or section. It often contains navigation links, logos, and introductory information.

<header>
  <h1>Welcome to My Website</h1>
  <nav>
    <ul>
      <li><a href="#home">Home</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#contact">Contact</a></li>
    </ul>
  </nav>
</header>

2. nav

The <nav> element represents a block of navigation links. Use it to wrap the main site navigation or specific sub-navigation menus.

<nav>
  <ul>
    <li><a href="#section1">Section 1</a></li>
    <li><a href="#section2">Section 2</a></li>
  </ul>
</nav>

3. main

The <main> element encapsulates the primary content of a web page, excluding headers, footers, and navigation sections. There should only be one <main> tag per page to keep the main content accessible.

<main>
  <h2>About Our Company</h2>
  <p>We specialize in creating high-quality products.</p>
</main>

4. section

The <section> element represents a standalone section within a page, often grouped by related content. Each <section> may include its own header or other elements.

<section>
  <h2>Our Services</h2>
  <p>We offer web design, development, and marketing services.</p>
</section>

5. article

The <article> element is for self-contained content that can be independently distributed or reused, such as blog posts, news articles, or user comments.

<article>
  <h2>Latest News</h2>
  <p>HTML5 semantic elements enhance website structure and accessibility.</p>
</article>

6. aside

The <aside> element contains content that is related but not essential to the main content, like sidebars, related links, or advertisements.

<aside>
  <h2>Related Articles</h2>
  <ul>
    <li><a href="#article1">Understanding CSS Flexbox</a></li>
    <li><a href="#article2">Introduction to JavaScript ES6</a></li>
  </ul>
</aside>

The <footer> element represents the closing or footer section of a page or a specific section. It typically includes contact info, copyright details, or links to privacy policies.

<footer>
  <p>&copy; 2024 My Website. All rights reserved.</p>
</footer>

How to Use Semantic Elements Effectively

Using semantic elements correctly enhances both accessibility and search engine optimization (SEO). However, improper usage can lead to confusion and reduce the benefits. Here are some best practices for using semantic HTML effectively:

1. Avoid Overuse and Nesting Issues

  • Use only one <main> element per page.
  • Do not nest semantic elements unnecessarily. For example, avoid placing a <header> inside another <header>.
  • Ensure logical placement of elements. For instance, <nav> should be inside <header> or <footer>, not randomly placed inside <article>.

Correct Example

<header>
    <h1>Website Name</h1>
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
        </ul>
    </nav>
</header>

<main>
    <section>
        <h2>Blog Title</h2>
        <p>Blog content goes here...</p>
    </section>
</main>

Incorrect Example (Nesting mistakes)

<main>
    <main> <!-- Incorrect: Only one <main> should be used -->
        <article>
            <header>
                <header> <!-- Incorrect: Nested headers -->
                    <h1>Blog Title</h1>
                </header>
            </header>
        </article>
    </main>
</main>

2. Combine Semantic Elements with ARIA Roles

  • While semantic elements provide meaning, sometimes additional context is needed for assistive technologies.
  • Use ARIA (Accessible Rich Internet Applications) roles where necessary.

Example: Adding ARIA Roles

<nav role="navigation">
    <ul>
        <li><a href="#" role="menuitem">Home</a></li>
        <li><a href="#" role="menuitem">Contact</a></li>
    </ul>
</nav>

Example: Landmark Roles

<header role="banner">Website Header</header>
<main role="main">Main Content</main>
<footer role="contentinfo">Footer Content</footer>

3. Maintain a Proper Heading Hierarchy

  • Headings (<h1> to <h6>) define the page structure, and search engines prioritize them.
  • Maintain a logical order without skipping levels.
  • A page should only have one <h1>, typically for the page title.

Correct Example

<h1>Main Page Title</h1>  
<h2>Section Title</h2>  
<h3>Subsection Title</h3>

Incorrect Example (Skipping levels)

<h1>Main Page Title</h1>  
<h3>Section Title</h3> <!-- Incorrect: Skipping <h2> -->
<h5>Subsection Title</h5> <!-- Incorrect: Skipping <h4> -->

By following these best practices, you ensure that your semantic HTML is both accessible and SEO-friendly, leading to better user experience and maintainability. 🚀

Example: Using Semantic Elements in a Blog Layout

Here’s an example of a simple blog structure using semantic HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Blog</title>
</head>
<body>

    <header>
        <h1>My Blog</h1>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </header>

    <main>
        <article>
            <h2>HTML5 Semantic Elements Explained</h2>
            <p>Learn how semantic HTML improves web accessibility and SEO...</p>
        </article>
    </main>

    <aside>
        <h3>Related Articles</h3>
        <ul>
            <li><a href="#">Introduction to HTML</a></li>
            <li><a href="#">CSS Basics</a></li>
        </ul>
    </aside>

    <footer>
        <p>&copy; 2025 My Blog. All rights reserved.</p>
    </footer>

</body>
</html>

Conclusion

HTML5 semantic elements improve the accessibility, readability, and SEO of your web pages. By using tags that convey specific meanings, you create cleaner, more organized code that's easier to maintain. Start incorporating semantic elements in your projects, and you’ll notice the difference in structure and quality.

Happy coding!

Topics Covered

About Author

I'm Maulik Paghdal, the founder of Script Binary and a passionate full-stack web developer. I have a strong foundation in both frontend and backend development, specializing in building dynamic, responsive web applications using Laravel, Vue.js, and React.js. With expertise in Tailwind CSS and Bootstrap, I focus on creating clean, efficient, and scalable solutions that enhance user experiences and optimize performance.