Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Structuring Content with Semantic HTML5

Recommended resources

Hello! Welcome to the first lesson in our HTML and CSS course.

Given your background in computer science and experience as a teacher, you're already familiar with the importance of writing clean, logical, and maintainable code. The same principles apply to building websites. In this lesson, we'll move beyond the basic HTML tags you already know and focus on how to use them to create a meaningful and well-organized document structure. This will not only improve your own development process but also give you a more robust framework for teaching these concepts to your students.

Our goal today is to master the use of semantic HTML5 elements. By the end of this lesson, you'll be able to structure a webpage using tags like <header>, <nav>, <main>, and <article> to represent the meaning of your content, not just its appearance.

What is Semantic HTML?

In programming, we use meaningful variable and function names to make our code understandable. Semantic HTML applies this same idea to web content. Instead of using generic containers like <div> for everything, we use HTML elements that explicitly describe the type of content they hold.

Consider the difference between a generic, non-semantic layout and a semantic one.

This image contrasts a layout built with generic `<div>` and `<span>` tags (left) against a layout using descriptive semantic tags like `<header>`, `<main>`, and `<article>` (right). Both can be styled to look identical, but the semantic version provides far more meaning.

Using semantic tags has three main benefits:

  1. Readability: Your code becomes easier for you and other developers to read and understand.
  2. SEO (Search Engine Optimization): Search engines like Google can better understand the structure and importance of your content, which can improve your site's ranking.
  3. Accessibility: Assistive technologies, such as screen readers for visually impaired users, can interpret the page structure and allow users to navigate the content more effectively.

To get a quick and clear overview of these benefits, let's start with a short video.

Why & When to Use Semantic HTML Elements over Divs

This video from ByteGrad provides a concise explanation of why semantic HTML is a best practice in modern web development.

Please watch the first part of the video, from the beginning to the three main reasons.


The Main Structural Elements of a Page

Most websites can be broken down into a few high-level sections. HTML5 provides specific elements for this structural "scaffolding."

  • <header>: Represents introductory content, typically a group of introductory or navigational aids. It often contains the website logo, the main heading (<h1>), and sometimes the primary navigation.
  • <nav>: Contains the main navigation links for the site.
  • <main>: Encompasses the dominant, unique content of a document. There should only be one <main> element per page.
  • <footer>: Represents the footer for its nearest sectioning content or for the whole page. It usually contains information like copyright notices, contact information, and links to related pages.

The following video provides a practical, step-by-step guide to implementing these core structural elements. The instructor, Dave Gray, refactors a simple HTML page, which is a great way to see these concepts in action.

Semantic HTML Tags | HTML5 Semantic Elements Tutorial

This segment of the "Semantic HTML Tags" tutorial will walk you through adding the <header>, <nav>, <main>, and <footer> elements to a document.

Watch the section from this demonstration. Pay close attention to how the existing content is wrapped in these new tags to give it structural meaning. Notice the brief mention of aria-label for the <nav> element—this is a good example of how semantics enhance accessibility.


Organizing Your Content: <article>, <section>, and <aside>

Once you have the main structure, you need to organize the content within the <main> element. This is where <article>, <section>, and <aside> come into play. Understanding the distinction between them is key to creating a truly semantic document.

  • <section>: This is a thematic grouping of content. Think of it as a chapter in a book. A section typically includes a heading. Your homepage could have sections for "Introduction," "News," and "Contact Info."

  • <article>: This represents a complete, self-contained piece of content that could, in principle, be distributed or reused independently. A blog post, a forum post, a news story, or a user comment are all excellent candidates for an <article>.

  • <aside>: This element is for content that is tangentially related to the content around it. It's often used for sidebars, pull quotes, or groups of advertising.

The distinction between <article> and <section> can sometimes be confusing. A good rule of thumb: could you syndicate this content in an RSS feed? If yes, it's probably an <article>. If it's just a grouping of related content on the page, it's likely a <section>. It's also common to have <section>s inside an <article> or <article>s inside a <section>.

Let's return to Dave Gray's tutorial to see how he applies these tags.

Semantic HTML Tags | HTML5 Semantic Elements Tutorial

These segments demonstrate how to use <article>, <section>, and <aside> to further organize the page content.

First, watch the part covering article and section. Notice how he decides which to use and how he nests them. Then, watch the next segment on the aside and other elements, focusing on how <aside> is used for complementary, non-essential information.

To solidify your understanding, let's look at how these elements are used on real-world, professional websites. The ByteGrad video provides excellent examples.

Why & When to Use Semantic HTML Elements over Divs

This part of the video analyzes the code of the Stripe and MDN (Mozilla Developer Network) websites to show practical applications of semantic tags.

Watch from the Stripe example to see how <section> is used for thematic groupings. Then, watch the walkthrough of the MDN website, which provides a very clear use case for <article> (a self-contained technical article) and <aside> (a sidebar with related links).


The Place for Non-Semantic <div> and <span>

So, if we have all these meaningful tags, is there still a place for <div> and <span>? Absolutely.

You should use <div> (a block-level container) and <span> (an inline container) when no other semantic element is appropriate. Their primary use is for styling or scripting purposes. For example, if you need to group several elements together just to apply a specific CSS layout (like a flexbox or grid container) and that grouping has no inherent semantic meaning, a <div> is the perfect tool for the job.

It's a common mistake for beginners to overuse <div> tags, leading to what is sometimes called "div-itis." As a rule, always try to find a semantic element first.

The ByteGrad video provides a perfect example of a situation where a <div> is the correct choice.

Why & When to Use Semantic HTML Elements over Divs

This final clip shows a clear, practical example of when to use a <div> for purely layout purposes.

Please watch the section from this example. The key insight is that the elements being grouped have no semantic relationship; they are just positioned together visually.


Verifying Your Document Structure

How can you be sure you've structured your document correctly? You can use a document outliner tool. These tools parse your HTML and show you the heading and sectioning structure, which is invaluable for checking your work and ensuring it's logical and accessible.

The following video demonstrates a popular browser extension for this purpose.

Semantic HTML Tags | HTML5 Semantic Elements Tutorial

This segment shows how to use the "HTML5 Outliner" browser extension to visualize and check the semantic structure of your page.

Watch the final demonstration starting at this point. Installing and using a tool like this is a great practical habit to develop.

For a comprehensive text-based resource that you can refer back to, the Mozilla Developer Network (MDN) has an excellent guide.

Structuring documents - Learn web development | MDN

This MDN article provides a thorough reference on document structure, covering the purpose of each semantic element and when to use non-semantic wrappers.

I recommend you read from Basic sections of a document to understand the common components of a webpage. Then, review the list of HTML for structuring content to see the elements we've discussed. Finally, read the sections on non-semantic wrappers to reinforce your understanding of <div> and <span>.


Conclusion

In this lesson, we established the critical role of semantic HTML in modern web development. By using elements that describe their content, we create pages that are more readable for developers, better optimized for search engines, and more accessible to all users.

Key Takeaways:

  • Structure vs. Presentation: Semantic HTML is about structuring your content based on its meaning, not its visual appearance.
  • Core Scaffolding: Use <header>, <nav>, <main>, and <footer> to define the main regions of your page.
  • Content Organization: Use <article> for self-contained, distributable content and <section> for thematic groupings. Use <aside> for tangentially related content.
  • Strategic <div>s: Reserve non-semantic elements like <div> and <span> for when no semantic element fits, primarily for styling and layout purposes.
  • Verification: Use document outliner tools to check and validate your page's structure.

Now that you know how to give your content a meaningful structure, our next step will be to control its layout. In the next lesson, we will dive into the CSS box model, exploring how margin, padding, and border are used to control the spacing and size of your elements. This will be our first major step into the world of CSS layout.

End of Module,Exit lesson