Telerik blogs

Let’s look at the basics of semantic HTML and see practical examples so you can start using it effectively.

In the vast world of web development, HyperText Markup Language (HTML) stands as one of the foundational languages that every web developer must grasp. It’s the skeleton of a webpage, defining its structure and content. However, not all HTML is created equal, and understanding the nuances of semantic HTML is crucial for creating accessible, SEO-friendly and maintainable websites.

In this article, we’ll take a look through the basics of semantic HTML, explain its importance and provide practical examples to help you start using it effectively.

HTML: The Bedrock of Modern Web UI Development

Before diving into semantic HTML, it’s essential to understand the role HTML plays in the broader context of web development. HTML serves as the foundational element for all webpages and is the fundamental building block upon which every website is constructed. When Tim Berners-Lee invented HTML in 1991, his goal was to create a language that allowed documents to be shared across a network of computers, which eventually evolved into the World Wide Web. Since then, HTML has undergone significant transformations, adapting to the needs of a rapidly growing digital world.

HTML’s primary purpose is to structure content on the web. It instructs the browser on how to render text, images and other multimedia elements, turning raw data into something visually meaningful and interactive. But HTML is not just about presentation; it’s also about semantics—providing context and meaning to content, which is where the evolution into semantic HTML comes into play.

What Is Semantic HTML?

Semantic HTML refers to the use of HTML elements that clearly describe their meaning in a human and machine-readable way. In simple terms, when we use semantic elements, we’re telling the browser and other technologies (like screen readers and search engines) what kind of content is inside the element.

For example, consider the following two HTML snippets:

<!-- Non-semantic HTML -->
<div id="header">
  <div class="logo">MyLogo</div>
  <div class="nav">Home | About | Contact</div>
</div>

<!-- Semantic HTML -->
<header>
  <h1>MyLogo</h1>
  <nav>
    <ul>
      <li>Home</li>
      <li>About</li>
      <li>Contact</li>
    </ul>
  </nav>
</header>

Both of these snippets will render similarly in a browser. However, the second example uses semantic HTML elements like <header>, <h1> and <nav>, which provide more context about the content they contain. This not only makes code more readable and easier to maintain but also improves accessibility and SEO.

Why Is Semantic HTML Important?

Accessibility

Accessibility is a key consideration when developing modern websites, and semantic HTML plays a vital role in making a website accessible to users with disabilities. For example, screen readers rely on semantic elements to understand the structure of a page and to provide meaningful navigation options to users. When we use semantic tags like <article>, <section>, <header> and <footer>, we help screen readers to better interpret the content, which enhances the user experience for individuals who rely on these technologies.

Consider a visually impaired user who uses a screen reader. If our webpage is structured with non-semantic <div> and tags, the screen reader might struggle to convey the structure and importance of the content. On the other hand, semantic HTML provides clear cues about the role of each piece of content, making the web more inclusive.

SEO (Search Engine Optimization)

Search engines like Google also benefit from semantic HTML. Semantic tags give additional context and meaning to the content, which search engines use to understand the hierarchy and context of a webpage. This understanding helps search engines to rank a webpage more accurately for relevant queries.

For example, the <article> tag indicates that the content inside it is a standalone piece of information, which might be important in the context of a search query. Similarly, the <header> and <footer> tags define the top and bottom sections of a page, helping search engines to better comprehend the structure of a site.

Code Maintainability

Writing clean, semantic HTML improves the maintainability of a codebase. When we use meaningful elements, other developers (or even your future self) can more easily understand the purpose of each section of the code. This can save time and reduce errors during updates and debugging.

Consider a large project where we need to update the navigation structure across multiple pages. If we’ve used a <nav> element consistently, we can quickly locate and modify the relevant sections. In contrast, if we’ve used non-semantic <div> elements with arbitrary classes, finding the right elements to update could be a more time-consuming and error-prone process.

Common Semantic HTML Elements

Now that we’ve covered why semantic HTML is important, let’s look at some of the most commonly used semantic elements and their purposes.

The <header> element represents introductory content or a group of navigational links. It typically contains headings, logos or other elements that are at the beginning of a section or page.

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

The <nav> element is used for defining a set of navigation links. This helps users and search engines easily identify the main navigation areas of your site.

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

Main

The <main> element is used to enclose the dominant content of the <body> of a document. The content inside <main> should be unique to the document and not include content that is repeated across documents like sidebars or footers.

<main>
  <article>
    <h2>Article Title</h2>
    <p>This is the main content of the article.</p>
  </article>
</main>

Article

The <article> element is intended to represent a self-contained composition in a document, page or website, which is designed to be independently distributed or repurposed, such as in syndication.

<article>
  <h2>Understanding Semantic HTML</h2>
  <p>Semantic HTML is essential for accessibility, SEO, and maintainability.</p>
</article>

Section

The <section> element represents a standalone section of content that typically comes with a heading. It’s used to group related content together.

<section>
  <h3>Introduction</h3>
  <p>This section introduces the topic.</p>
</section>

Aside

The <aside> element is intended for content that is related to the surrounding content but not essential to its primary message.

<aside>
  <h4>Related Information</h4>
  <p>This is additional information related to the main content.</p>
</aside>

The <footer> element signifies the end section of a page or content, often including details about the author, copyright information or related links.

<footer>
  <p>© 2024 MyWebsite. All rights reserved.</p>
</footer>

To see how all the above elements work in practice, let’s consider a simple webpage structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Webpage</title>
  </head>
  <body>
    <header>
      <h1>My Webpage</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>Understanding Semantic HTML</h2>
        <p>
          Semantic HTML is essential for accessibility, SEO, and
          maintainability.
        </p>
      </article>

      <section>
        <h3>Benefits of Semantic HTML</h3>
        <p>
          It helps with search engine optimization, accessibility, and code
          readability.
        </p>
      </section>
    </main>

    <aside>
      <h4>Did You Know?</h4>
      <p>Using semantic HTML can improve your site’s accessibility.</p>
    </aside>

    <footer>
      <p>© 2024 My Webpage. All rights reserved.</p>
    </footer>
  </body>
</html>

In the above example, we’ve used a variety of semantic elements to structure the webpage. Each element has a specific role, making the HTML more meaningful and easier to understand for both humans and machines. For a full list of roughly 100 semantic elements that exist in HTML, refer to the MDN HTML elements reference guide.

Wrap-up

Semantic HTML is more than just a buzzword; it’s a crucial aspect of modern web development. By using semantic elements, we can create content that is accessible, SEO-friendly and easier to maintain. Some best practices for writing semantic HTML include:

  • Use the right element for the job. Always try to use the most appropriate semantic element for the content you’re marking up. For example, use <article> for blog posts or news articles and <section> for grouping related content.
  • Avoid divitis. “Divitis” is a term used to describe the overuse of <div> elements. While <div> has its place, relying too heavily on it when more descriptive elements are available can make your code less readable and less semantic.
  • Keep accessibility in mind. When choosing elements, consider how they will affect users who rely on assistive technologies. Use landmarks (like <nav>, <main> and <footer>) to help these users navigate your content more easily.
  • Validate your HTML. Use tools like the W3C HTML Validator to check that your code is valid and follows best practices. This can help catch errors and ensure your semantic HTML is well-formed.

Whether you’re building a simple personal blog or a complex web application, understanding and applying semantic HTML will set a strong foundation for your project, making it more robust and user-friendly for all.


About the Author

Hassan Djirdeh

Hassan is a senior frontend engineer and has helped build large production applications at-scale at organizations like Doordash, Instacart and Shopify. Hassan is also a published author and course instructor where he’s helped thousands of students learn in-depth frontend engineering skills like React, Vue, TypeScript, and GraphQL.

Related Posts

Comments

Comments are disabled in preview mode.