HTML5 页脚元素和令人困惑的 w3.org 示例

HTML5 footer element and confusing w3.org example

我正在浏览关于 article 元素的 w3.org 页面,其中一个例子让我感到惊讶:

<article>
 <header>
  <h1>The Very First Rule of Life</h1>
  <p><time pubdate datetime="2009-10-09T14:28-08:00"></time></p>
 </header>
 <p>If there's a microphone anywhere near you, assume it's hot and
 sending whatever you're saying to the world. Seriously.</p>
 <p>...</p>
 <section>
  <h1>Comments</h1>
  <article>
   <footer>
    <p>Posted by: George Washington</p>
    <p><time pubdate datetime="2009-10-10T19:10-08:00"></time></p>
   </footer>
   <p>Yeah! Especially when talking about your lobbyist friends!</p>
  </article>
  <article>
   <footer>
    <p>Posted by: George Hammond</p>
    <p><time pubdate datetime="2009-10-10T19:15-08:00"></time></p>
   </footer>
   <p>Hey, you have the same first name as me.</p>
  </article>
 </section>
</article>

如您所见,评论信息(发帖人姓名和日期)位于每个评论开头的 footer 元素中。
根据 W3.org 4.3.8 The footer element 这是一个有效的用法,但这样使用它似乎很奇怪。

A footer typically contains information about its section such as who wrote it, links to related documents, copyright data, and the like.

没错,没有说它应该放在实际文章下面。

我会为这种用法使用 header 元素,但在 4.3.7 The header element 上它被精确地

A header typically contains a group of introductory or navigational aids.

但他们也谈到 footer 元素:

The primary purpose of these elements is merely to help the author write self-explanatory markup that is easy to maintain and style; they are not intended to impose specific structures on authors.

那么他们为什么要在示例中使用 footer 元素? header 元素不是更直观和语义化吗?

<section>
  <h1>Comments</h1>
  <article>
   <header>
    <p>Posted by: George Washington</p>
    <p><time pubdate datetime="2009-10-10T19:10-08:00"></time></p>
   </header>
   <p>Yeah! Especially when talking about your lobbyist friends!</p>
  </article>
  <article>
   <header>
    <p>Posted by: George Hammond</p>
    <p><time pubdate datetime="2009-10-10T19:15-08:00"></time></p>
   </header>
   <p>Hey, you have the same first name as me.</p>
  </article>
 </section>

有什么特别的原因吗?

这里的经验法则是 footer 应该用于标记有关封闭部分的元信息(在您的示例中,即每个评论的作者和发布时间)。另外,正如您所指出的,根据规范,不要求页脚必须位于该部分的 "the foot"。

相比之下,header用于封闭部分的介绍性内容,应至少包含一个标题元素。事实上,我认为排除标题元素是原始示例使用 footer 元素的主要原因。

为避免混淆,这并不意味着每个标题都应包含在 header 中,因为在这种情况下它不会添加任何语义值。

一般经验法则:如果它有 header 和一些附加信息(副标题、发布日期、目录、导航...),通常可以将其包含在 header 中。文档流中未出现在标题附近的任何其他 information/meta 部分数据(脚注、相关链接、作者信息、版权...)都可以包含在 footer.

参考:
- HTML5Doctor - Footer
- HTML5Doctor - Header

footer and header 之间的语义差异并不总是很清楚。在不清楚的情况下,重要的是你完全使用其中一种,而不是使用哪一种。

footerheader

的位置

位置无关紧要(只要在相关部分内)。由于 header 用于介绍性内容,因此它通常会出现在该部分的顶部附近。对于 footer,规范说明:

Footers don't necessarily have to appear at the end of a section, though they usually do.

甚至有可能 to use them multiple times(不一定是个好主意):

<article>
  <footer>…</footer>
  <header>…</header>
  <p>Hello world</p>
  <footer>…</footer>
  <header>…</header>
  <header>…</header>
</article>

footerheader

之间的语义差异
  • 根据规范的定义,元素代表什么:
    • footer: "a footer"
    • header: "introductory content"
  • 根据规范,元素 "typically" 包含的内容:
    • footer: "information about its section such as who wrote it, links to related documents, copyright data"
    • header: "group of introductory or navigational aids"
  • 根据规范中的示例,元素可能包含的内容:
    • footer:"Back to …" link,发布日期,导航,版权声明,link ToS,参考作者
    • header:介绍,title/heading,版本号,日期,link相关文档,版权声明,导航,状态信息,通知
  • WAI-ARIA 角色(仅在 body 节根的 footer/header 的情况下):
    • footercontentinfo role ("information about the parent document")
    • headerbanner role ("mostly site-oriented content, rather than page-specific content")