General vs. Semantic Tags in HTML: Why Structure Matters

okputu-e

Okputu Emmanuel Okputu

Posted on September 22, 2024

General vs. Semantic Tags in HTML: Why Structure Matters

Introduction

It’s common to begin learning HTML by relying heavily on general tags like <div> and <span> for structuring various parts of a webpage, such as sections, buttons, paragraphs, and containers. However, HTML serves as the skeleton of every front-end development project, much like the human body has visible, identifiable parts. To ensure that this structure is meaningful and easy to understand, both for humans and machines, it’s essential to use semantic HTML.

General vs Semantic Tags

General tags are non-semantic tags that do not convey specific meaning about their content. In contrast, semantic tags are designed to provide clear, meaningful context about the information they enclose. Additionally, the distinction between general and semantic tags can be made based on their intended usage in web development, where semantic tags enhance accessibility and improve search engine optimization by conveying the structure and significance of the content.

By definition, general tags include elements like <div> and <span>, which lack specific meaning regarding the content they enclose. In contrast, semantic tags—such as <menu>, <nav>, and <header>— are specifically designed to describe the role of the content they contain, providing both context and meaning.

Code Example (General Tags by Definition):

<div> <!-- non-semantic, should be nav -->
  <ul> <!-- non-semantic, should be menu -->
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</div>
Enter fullscreen mode Exit fullscreen mode

Code Example (Semantic Tags by Definition):

<nav> 
  <menu>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </menu>
</nav>
Enter fullscreen mode Exit fullscreen mode

By usage, general tags might include using <h1> and <h2> without adhering to the correct heading hierarchy, omitting labels for form inputs, or using a <div> to contain navigation links when a <nav> tag would be more appropriate. Similarly, employing a <div> or <ul> for menu items, rather than utilizing the <menu> tag, exemplifies the misuse of general tags. These practices can diminish the semantic value and accessibility of the markup.

Code Example (General Tags Misused by Usage):

<div>
  <h1>Main Heading</h1>
  <h1>Subheading (incorrect)</h1> <!-- Incorrect, should be h2 -->
  <div>
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Contact</a>
  </div> <!-- Incorrect, should use <nav> and <menu> instead -->
</div>
Enter fullscreen mode Exit fullscreen mode

In contrast, semantic usage involves applying tags correctly for their intended purposes. For example, using the tag to contain navigation links and ensuring that <h1>, <h2>, and other heading tags follow a proper hierarchical structure exemplifies semantic usage. It also includes employing the <menu> tag for list items intended for navigation or commands, rather than using a <div> or <ul>. This proper application enhances the clarity and accessibility of the content.

Code Example (Semantic Tags Correct by Usage):

<main>
  <h1>Main Heading</h1>
  <h2>Subheading</h2> <!-- Correct hierarchy -->
  <section>
    <p>Section content here...</p>
  </section>
</main>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By definition, general tags such as <div> and <span> lack specific meaning, whereas semantic tags like <header> and <nav> describe the role of the content. By usage, general tags can be misapplied when proper conventions—such as heading hierarchy or navigation grouping—are ignored. Semantic usage involves applying the correct tags for their intended functions to enhance readability, accessibility, and SEO.

Resources

Below are some resources that I am currently using to break into the tech world:

💖 💪 🙅 🚩
okputu-e
Okputu Emmanuel Okputu

Posted on September 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related