Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Semantic HTML?

Semantic HTML is a term you might have come across if you've been learning web development. But what exactly does it mean, and why is it important? In this blog post, we are going to explain the concept of semantic HTML, its benefits, and show you some examples of how to use it in your code.

What is Semantic HTML?

To understand semantic HTML, let's first break down the term into its two parts: "semantic" and "HTML".

Semantic: A semantic element is one that carries meaning or conveys information about its contents. In the context of HTML, this means that a semantic element indicates the purpose of the content it wraps around.

HTML: Hypertext Markup Language (HTML) is the standard markup language for creating web pages. It uses various elements (also called tags) to structure and present content on a web page.

Put together, semantic HTML refers to using appropriate HTML elements to give meaning to the content they enclose, making it easier for both humans and machines to understand the structure and purpose of that content.

Why is Semantic HTML important?

Let's take a look at some of the benefits of using semantic HTML:

Accessibility: Semantic HTML improves the accessibility of your web pages for people who use assistive technologies like screen readers. Properly structured content with meaningful tags helps these tools better understand the content and present it in a more usable way for users with disabilities.

SEO: Search engines rely on the structure and meaning of your HTML to understand and rank your content. By using semantic HTML, you're helping search engines to better index your pages, which can improve your search engine ranking.

Maintainability: As a developer, well-structured and meaningful code is easier to read, understand, and maintain. When you or someone else needs to modify or update the code in the future, semantic HTML will make it easier to identify the purpose of each section of the content.

Cross-platform compatibility: By using standard semantic HTML elements, you're creating content that is more likely to be compatible with different browsers, devices, and future web standards.

Non-semantic vs Semantic HTML

Let's take a look at an example of non-semantic HTML and how we can improve it using semantic HTML elements.

Non-semantic HTML example

<div id="header">Header content</div>
<div id="nav">Navigation content</div>
<div id="main">
  <div id="article">Article content</div>
  <div id="sidebar">Sidebar content</div>
</div>
<div id="footer">Footer content</div>

In this example, we're using <div> elements for all sections of the content. While this code will display the content correctly, it doesn't provide any information about the purpose of each section, making it harder for humans and machines to understand the structure of the page.

Semantic HTML example

Now let's rewrite the above example using semantic HTML elements.

<header>Header content</header>
<nav>Navigation content</nav>
<main>
  <article>Article content</article>
  <aside>Sidebar content</aside>
</main>
<footer>Footer content</footer>

In this improved example, we replaced the non-semantic <div> elements with appropriate semantic elements, like <header>, <nav>, <main>, <article>, <aside>, and <footer>. This code provides a clearer understanding of the content structure and purpose, making it more accessible and better for SEO.

Common Semantic HTML Elements

Here's a list of some commonly used semantic HTML elements and their purpose:

  • <header>: Represents the header section of a web page or a section, usually containing a logo, site title, or navigation menu.
  • <nav>: Represents a section of a web page containing navigation links.
  • <main>: Represents the main content of a web page, excluding headers, footers, and sidebars.
  • <article>: Represents a self-contained piece of content that could be distributed and reused independently, like a news article or blog post.
  • <section>: Represents a generic section of content that is thematically related and can be treated as an independent unit.
  • <aside>: Represents content that is related to the main content but can be considered separate, like a sidebar or a pull quote.
  • <footer>: Represents the footer section of a web page or a section, usually containing copyright information, contact details, or additional navigation links.

Tips for using Semantic HTML

Choose the most appropriate element: When writing HTML, try to choose the most appropriate semantic element for the content you're working with. Sometimes, there might not be a perfect match, and that's okay. In those cases, you can use a generic element like <div> or <span> and add ARIA (Accessible Rich Internet Applications) attributes to provide additional information about the content.

Don't be afraid to use non-semantic elements: While it's essential to use semantic elements whenever possible, sometimes using non-semantic elements like <div> and <span> is necessary. Just make sure to balance their use with appropriate semantic elements to provide a clear structure for your content.

Validate your code: Use an HTML validator like the W3C HTML Validator to check your code for errors and ensure you're using semantic elements correctly.

By incorporating semantic HTML into your web development workflow, you'll create more accessible, SEO-friendly, and maintainable web pages. As you continue to learn programming, you'll discover that writing clean and meaningful code is a valuable skill that will benefit you and those who work with your code in the future.