Hello! Welcome back to your front-end programming course.
In our last lesson, we learned how to create links, the fundamental building blocks of navigation. We discussed how an anchor tag <a> with an href attribute can connect pages within our site or link to external websites.
Today, we'll build on that by learning how to give our web pages a meaningful structure. This lesson addresses the learning outcome: Apply semantic HTML elements to structure page content including header, nav, main, section, article, and footer.
As a designer, you're an expert at creating visual hierarchy and structure to guide a user's eye. Semantic HTML does something very similar for the browser, for search engines, and for assistive technologies like screen readers. It provides a meaningful, logical map of the page's content, which is a cornerstone of accessibility and good user experience.
1. What is Semantic HTML?
The word "semantic" means "relating to meaning." In HTML, this means using tags that describe the meaning or purpose of the content they contain, rather than just how they look.
For example, in previous lessons, we've seen the <div> element. A <div> is a generic container—it doesn't tell us anything about its contents. Using only <div>s to structure a page leads to what developers call "div soup": a nested, confusing mess of containers that is hard for developers to maintain and difficult for assistive technologies to interpret.
Semantic HTML provides a clear alternative.
To start, let's watch a short video that explains why this is so important.
Why & When to Use Semantic HTML Elements over Divs
This video from ByteGrad, 'Why & When to Use Semantic HTML Elements over Divs', clearly explains the three main benefits of using semantic tags.
Please watch the first 46 seconds of the video, which cover the core reasons for using semantic HTML.
As the video highlights, the key benefits are:
- Accessibility: Screen readers can identify sections like "main content" or "navigation," allowing users to navigate the page efficiently.
- 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.
- Readability & Maintainability: Your code becomes easier for you and other developers to read and understand.
2. The Building Blocks of Page Layout
Most websites share a common structure. Think about any blog, news site, or e-commerce page you've visited. You'll typically find a header at the top, a main content area, and a footer at the bottom. HTML5 provides specific semantic elements to define these sections.
Let's explore these elements in detail.
Structuring documents - Learn web development | MDN
The MDN Web Docs article 'Structuring documents' is an excellent and authoritative guide. We'll use it to get clear definitions of the main layout elements.
Please read the section titled 'HTML layout elements in more detail'. It provides concise definitions for <main>, <article>, <section>, <aside>, <header>, <nav>, and <footer>.
Key Elements Summary:
<header>: Contains introductory content for a page or section. Typically includes a logo, site title, and the main navigation.<nav>: Used specifically for major navigational links. This is where the links we created in the last lesson would go!<main>: The dominant, unique content of the page. There should only be one<main>element per page.<footer>: Contains content for the end of a page. Usually includes copyright information, contact details, and secondary links.<section>: Groups related content together. Think of it as a chapter in your page's story (e.g., "About Us," "Our Services"). It's good practice for each section to have a heading.<article>: For self-contained content that could be distributed independently, like a blog post, a news story, or a product listing.<aside>: For content that is tangentially related to the main content, like a sidebar with related links or an author bio.
3. Seeing Semantic HTML in Action
Theory is one thing, but seeing how these elements are used on real websites makes the concepts much clearer.
The following video segments analyze the code of major websites like Stripe and MDN to show you how these elements are applied in practice.
Why & When to Use Semantic HTML Elements over Divs
Let's return to the ByteGrad video. These next segments provide excellent real-world examples and clarify the difference between section, article, and aside.
Please watch from 00:46 to 10:19. Pay attention to: Basic Structure (00:46 - 02:15): How <header>, <main>, <footer>, and <nav> form the page's skeleton. Section vs. Article (02:15 - 04:09): The key distinction is whether the content is self-contained. Stripe Website Analysis (04:09 - 07:11): Notice how they use <section> to group related marketing content. MDN Website Analysis (07:11 - 10:19): A great example of using <main>, <article>, and <aside> for a technical document.
4. Case Study: Structuring a Retail Homepage
Now, let's walk through a practical example from start to finish. Imagine you're designing and building a homepage for an online store. How would you structure it semantically?
The following article provides a step-by-step guide for converting a page built with non-semantic <div>s into a well-structured, accessible page using semantic HTML. This directly connects to your work as a designer, translating a layout into meaningful code.
Building Accessible and Well-Structured Websites with ...
This article, 'Building Accessible and Well-Structured Websites with Semantic HTML', provides a perfect case study for applying what we've learned.
Please read from the section 'Step-by-Step Example: Building an Accessible Retail Homepage' through to the end of section '5. Footer: Additional Navigation and Links'. Focus on how each part of the retail page (header, banner, promotions, footer) is mapped to a specific semantic tag.
5. Practice: Refactor a Simple Blog Post
It's time to apply your knowledge. Below is the HTML for a simple blog post, structured using only <div> elements. Your task is to refactor it using the semantic elements we've discussed today.
Instructions:
- Copy the "Before" code into a text editor or an online code playground like CodePen.
- Replace the
<div>tags with the most appropriate semantic elements (<header>,<nav>,<main>,<article>,<section>,<footer>). - Compare your result with the "After" code provided below.
Before (using only <div>s):
<body>
<div class="main-header">
<h1>My Awesome Blog</h1>
<div class="main-nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div class="main-content">
<div class="blog-post">
<h2>My First Post</h2>
<p>This is the first paragraph of my blog post. It's very exciting!</p>
<p>Here is another paragraph, continuing the story.</p>
</div>
<div class="comments">
<h3>Comments</h3>
<p><strong>Jane Doe:</strong> Great post!</p>
</div>
</div>
<div class="page-footer">
<p>© 2024 My Awesome Blog</p>
</div>
</body>
Click to see the "After" solution
After (using semantic HTML):
<body>
<header>
<h1>My Awesome 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>My First Post</h2>
<p>This is the first paragraph of my blog post. It's very exciting!</p>
<p>Here is another paragraph, continuing the story.</p>
</article>
<section class="comments">
<h3>Comments</h3>
<p><strong>Jane Doe:</strong> Great post!</p>
</section>
</main>
<footer>
<p>© 2024 My Awesome Blog</p>
</footer>
</body>
In the solution, notice that the blog post itself is an <article> because it's a self-contained piece of content. The comments are a <section> because they are a related group of content but are part of the larger page context, not a distributable item on their own.
Conclusion
Great job! You've taken a significant step in learning how to write professional, modern HTML. By structuring documents semantically, you create websites that are more accessible, discoverable, and maintainable. This is a skill that directly bridges the gap between a visual design and a robust, high-quality technical implementation.
Key Takeaways:
- Semantic HTML uses tags like
<header>,<nav>,<main>,<article>,<section>, and<footer>to describe the meaning of content. - This practice is crucial for accessibility, SEO, and code readability.
- An
<article>is for self-contained, distributable content, while a<section>is for thematically grouping content on a page. - Non-semantic elements like
<div>should only be used for styling or grouping when no semantic element is appropriate.
In our next lesson, we'll continue building out our pages by learning how to embed images using the <img> element. You'll see how to place a logo in your <header> or an illustration in your <article>, adding the visual layer to the strong semantic structure you've just learned to build.
Can't find a good explanation? Sign up and we'll make it for you
Sign up