Semantic elements are HTML tags that clearly describe their meaning to both the browser and the developer. They tell you what the content is about, not just how it looks.
What are Semantic Elements?
A semantic element clearly describes its purpose. For example, a <form> tag tells you there is a form. A <table> tag tells you there is a table.
Non-semantic elements: <div> and <span> – They tell you nothing about the content inside them.
Semantic elements: <form>, <table>, <article>, <nav>, <header>, <footer> – They clearly tell you what the content is.
Semantic Layout Elements
Here are the most important semantic elements for structuring a webpage:
<header>– Top of the page (logo, title, navigation)<nav>– Navigation links (menu)<section>– A group of related content<article>– Self-contained content (blog post, news item)<aside>– Sidebar content (related but not essential)<footer>– Bottom of the page (copyright, contact info)<main>– The main content of the page (should be unique)<figure>and<figcaption>– Images with captions
The section Element
The <section> element defines a group of related content. Each section should have its own heading (like an h1 or h2).
Think of sections like chapters in a book. Each chapter is a section with its own title and content.
The article Element
The <article> element is for self-contained content that could be distributed independently. Blog posts, news articles, forum posts, and product cards are perfect for article tags.
If you can take the content out of the page and it still makes sense by itself, use an article tag.
Nesting article Inside section (and vice versa)
You can nest articles inside sections or sections inside articles. Both are valid. The key is to think about what makes sense for your content.
- section inside article: A blog post (article) with multiple chapters (sections)
- article inside section: A section of a page that contains multiple blog posts (articles)
The header Element
The <header> element is for introductory content. It usually contains headings, logos, or navigation links. You can have multiple headers on a page (like one for the whole page and one for each article).
The footer Element
The <footer> element is for footer content at the bottom of a page or section. It usually contains copyright information, contact details, or links to related documents.
The nav Element
The <nav> element is for major navigation links. It tells the browser “these are the main menu links”. Screen readers can skip this section if the user already knows the navigation.
The aside Element (Sidebar)
The <aside> element is for content that is related to the main content but not essential. Think sidebars, pull quotes, or additional resources.
The figure and figcaption Elements
Use <figure> to wrap an image, diagram, or code snippet. Use <figcaption> to add a caption. This tells the browser that the image and caption belong together.
Complete Semantic HTML Page Example
Here is a complete webpage using semantic elements:
Quick Summary
- Semantic elements clearly describe their meaning to browsers and developers.
<section>– Groups related content with a heading.<article>– Self-contained content that could stand alone.<header>– Introductory content (headings, logos, navigation).<footer>– Footer content (copyright, contact info).<nav>– Major navigation links.<aside>– Sidebar content (related but not essential).<figure>and<figcaption>– Images with captions.<main>– The main content of the page (use only once).
What Just Happened? Let Me Explain
- Semantic HTML is not just about style. It is about meaning. Search engines and screen readers rely on it.
- Using non-semantic divs and spans everywhere makes your code harder to understand for both machines and humans.
- Semantic elements do not look different by default. You still need CSS to style them. But they provide structure and meaning.
- Think of semantic HTML as the foundation. CSS is the paint and decoration. JavaScript is the electricity and moving parts.
- All modern websites use semantic HTML. It is considered a best practice and often required for accessibility compliance.
HTML Semantic Elements Reference
Here are the semantic elements we covered in this chapter:
| Tag | Description |
|---|---|
<article>
| Defines independent, self-contained content |
<aside>
| Defines content aside from the main content (sidebar) |
<details>
| Defines additional details that can be opened/closed |
<figcaption>
| Defines a caption for a figure element |
<figure>
| Specifies self-contained content (images, diagrams) |
<footer>
| Defines a footer for a document or section |
<header>
| Specifies a header for a document or section |
<main>
| Specifies the main content of a document |
<mark>
| Defines marked or highlighted text |
<nav>
| Defines navigation links |
<section>
| Defines a section in a document |
<summary>
| Defines a heading for the details element |
<time>
| Defines a date or time |