Skip to content

HTML 语义标签

学习语义 HTML 标签那个描述内容 meaning for accessibility and SEO.

你将学到

  • 什么语义 HTML 是 and 为什么它 matters
  • 键语义标签: 请求头, nav, 主要, article, 小节, aside, footer
  • 如何语义 improve accessibility for screen readers
  • 如何语义 markup boosts 搜索 engine 理解

概念讲解

HTML Semantic Tags

Semantic HTML means using tags that describe the *meaning* of their content, not just how it looks. A <div> tells you nothing — it is a generic container. But <article> says "this is a self-contained piece of content," and <nav> says "this is navigation." Semantic tags give your markup meaning that browsers, screen readers, and search engines can act on.

The Core Semantic Tags

  • <请求头> — introductory content or a group of navigation links, typically at the top of a page or section.
  • <nav> — a section of navigation links. Not every group of links is <nav> — reserve it for major navigation blocks.
  • <主要> — the dominant content of the page. There should be exactly one <主要> per page.
  • <article> — a self-contained composition: a blog post, a news story, a product card. It makes sense on its own.
  • <小节> — a thematic grouping of content, usually with a heading. Use it to divide a page into chapters.
  • <aside> — content tangentially related to the main content, like a sidebar or a pull quote.
  • <footer> — closing content: copyright, contact info, related links.

Why Semantics Matter

Screen readers use semantic tags to navigate. A user can jump straight to the <nav> or skip to <主要> content. Without semantics, the page is an undifferentiated sea of <div>s, and assistive technology cannot help the user move efficiently. Search engines also use semantic structure to understand what your page is about, which can improve rankings.

Divs vs. Semantic Tags

You will still use <div> — it is the right choice when no semantic tag fits. The rule is simple: use a semantic tag when one exists, fall back to <div> only when it does not. Replacing every <div> with a semantic tag is not the goal; using the *right* tag for the *right* content is.

示例代码

              <!DOCTYPE html>
<html lang="en">
  <body>
    <header>
      <h1>My Blog</h1>
      <nav>
        <a href="/">Home</a>
        <a href="/about">About</a>
        <a href="/archive">Archive</a>
      </nav>
    </header>

    <main>
      <article>
        <h2>Why Semantic HTML Matters</h2>
        <p>Semantic tags describe content meaning.</p>
        <section>
          <h3>Accessibility</h3>
          <p>Screen readers navigate by landmarks.</p>
        </section>
      </article>

      <aside>
        <h3>Related Posts</h3>
        <ul><li>HTML Forms 101</li><li>CSS Selectors</li></ul>
      </aside>
    </main>

    <footer>&copy; 2024 My Blog</footer>
  </body>
</html>
            

每个标签描述它的 role: 请求头 for 顶部 banner, nav for 链接, 主要 for 主要内容, article for post, 小节 for sub-主题, aside for related 内容, and footer for 关闭信息.

动手试一试

常见错误

错误写法

Using <div> for 一切相反的 语义标签.

正确做法

Use <请求头>, <nav>, <主要>, <article>, <小节>, <aside>, <footer> 哪里它们 fit. Reserve <div> for 当否 语义标签 applies.

错误写法

有更多 than one <主要> 元素每 page.

正确做法

那里必须是 精确地 one <主要> 每 page. 它 wraps dominant 内容. Sidebars and 请求头 live 之外它.

相关速查表

练习 HTML 语义标签

6 个练习

练习 HTML 语义标签

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误