HTML Semantics

Semantic tags give meaning to your web page structure — they tell the browser not just how to display something, but also "what this section is." This greatly helps with SEO, accessibility, and code readability.

What Is Semantics

"Semantics" means "meaning." You can build any layout with <div>, but the browser can't understand the container's purpose. Semantic tags communicate their purpose directly through their names:

Example

HTML
<header>
<h1>My Blog</h1>
<nav>
<a href="index.html">Home</a>
</nav>
</header>
<main>
<article>
<h2>Article Title</h2>
<p>Article content...</p>
</article>
<aside>Sidebar</aside>
</main>
<footer>
<p>&copy; 2026</p>
</footer>
▶ Try it Yourself
📌 Common Tags:<header> page header, <nav> navigation, <main> main content, <section> section, <article> article, <aside> sidebar, <footer> footer. They are all block-level containers that function like <div>, but their names describe their purpose.

Benefits of Semantics

Semantics offer three direct benefits: search engines understand your page structure more accurately; screen readers can jump to specific areas; developers understand the layout at a glance, without guessing what a <div> class name means.

Image/Content Container: <figure> and <figcaption>

<figure> wraps self-contained content like images, code blocks, or charts, while <figcaption> provides a caption for it:

Example

HTML
<figure>
<img src="/assets/images/tutorials/html-sales-chart.webp" alt="2026 sales data chart">
<figcaption>Figure 1: Quarterly sales comparison for 2026</figcaption>
</figure>

<figure>
<pre><code>console.log("Hello World");</code></pre>
<figcaption>Code example: printing Hello World</figcaption>
</figure>
▶ Try it Yourself

Time and Highlighting: <time> and <mark>

<time> makes dates and times machine-readable, and <mark> highlights search keywords or important text:

Example

HTML
<p>Published on <time datetime="2026-06-12">June 12, 2026</time></p>

<p>Search for: <mark>semantic tags</mark>,
results will be highlighted.</p>

<p><time datetime="2026-06-12T14:30:00+08:00">
2:30 PM tomorrow</time> the meeting will be held.</p>
▶ Try it Yourself

Collapsible Content: <details>and <summary>

<details> creates an expandable/collapsible interactive widget, and <summary> defines its visible heading. Pure HTML, no JavaScript needed:

Example

HTML
<details>
<summary>What are semantic tags?</summary>
<p>Semantic tags are HTML tags that use meaningful names
to describe content purpose, such as &lt;header&gt;,
&lt;nav&gt;, &lt;article&gt;, etc.</p>
</details>

<details open>
<summary>Already expanded panel (with open attribute)</summary>
<p>Adding the open attribute makes the panel expanded by default.</p>
</details>
▶ Try it Yourself
💡 More Semantic Tags:<blockquote> for quotes, <code> for code, <address> for contact info, <dialog> for dialogs. Everything can be semantic — find the most fitting tag before using a generic one.

❓ FAQ

Q How do semantic tags affect SEO?
A Search engines use semantic tags to understand page structure and content importance. <header> tells search engines "this is the page header," <main> indicates "this is core content," <article> means "this is a standalone article." Search engines give higher weight to content inside <main> and <article>. Additionally, semantic tags help search engines generate more accurate result snippets (displaying article titles, authors, publish dates).
Q What's the difference between <section> and <article>?
A <article> is independent, complete content that can be published or reused on its own (like blog posts, news articles, reviews). <section> is a thematic grouping of content, typically with a heading, but isn't standalone (like chapters, tab panels, product features). The test: if the content block makes sense on its own, use <article>; if it needs context to be understood, use <section>.
Q When should I use <div>?
A Use <div> only when no suitable semantic tag exists. Common scenarios: 1) Pure styling containers (wrapping elements for Flexbox layout); 2) JavaScript hooks (modals, popups); 3) Groupings with no semantic meaning. Rule: first look for semantic tags (header/nav/main/section/article/aside/footer), fall back to div when none fit.
Q How is <details> and <summary> browser compatibility?
A <details> and <summary> are HTML5 native collapsible panels, supported by all modern browsers (Chrome, Firefox, Safari, Edge) — excellent compatibility. Benefits: collapsible without JavaScript, supports keyboard navigation and screen readers. Limitations: limited styling customization (like arrow icons), complex interactions still need JavaScript. IE doesn't support it, but IE is discontinued — no need to worry.

📖 Summary

📝 Exercises

  1. Build a Skeleton: Use semantic tags to build a blog page structure and fill in some placeholder text.
  2. Compare the Experience: Use a screen reader to navigate a pure <div> page and a semantic page side by side, and observe the difference.
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏