<header>:Header 元素

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

<header> HTML 元素表示介绍性内容,通常是一组介绍性或导航辅助内容。它可能包含一些标题元素,但也可以包含徽标、搜索表单、作者姓名和其他元素。

试一试

<header>
  <a class="logo" href="#">Cute Puppies Express!</a>
</header>

<article>
  <header>
    <h1>Beagles</h1>
    <time>08.12.2014</time>
  </header>
  <p>
    I love beagles <em>so</em> much! Like, really, a lot. They’re adorable and
    their ears are so, so snugly soft!
  </p>
</article>
.logo {
  background: left / cover
    url("/shared-assets/images/examples/puppy-header.jpg");
  display: flex;
  height: 120px;
  align-items: center;
  justify-content: center;
  font:
    bold calc(1em + 2 * (100vw - 120px) / 100) "Dancing Script",
    fantasy;
  color: #ff0083;
  text-shadow: black 2px 2px 0.2rem;
}

header > h1 {
  margin-bottom: 0;
}

header > time {
  font: italic 0.7rem sans-serif;
}

用法说明

<header> 元素未嵌套在 分区内容<main> 或具有与这些元素隐含 ARIA 角色相同的 ARIA 角色的元素内时,<header> 元素与站点范围的 banner 地标角色具有相同的含义。它定义了一个全局站点标题,通常包括徽标、公司名称、搜索功能,以及可能的全局导航或标语。它通常位于页面的顶部。

否则,当嵌套在上述元素内时,它将失去地标状态,并表示围绕它的部分的介绍性或导航辅助内容的组。它通常包含围绕它的部分的标题(h1h6 元素)和可选的副标题,但这 **不是** 必需的。

历史用法

<header> 元素最初存在于 HTML 的最早期,用于表示标题。它可以在 第一个网站 中看到。在某个时候,标题变成了 <h1><h6>,这使得 <header> 可以自由地承担不同的角色。

属性

此元素仅包含全局属性

无障碍

<header> 元素处于 <body> 元素 的上下文中时,它定义了一个 banner 地标。

当放置在 <article><main><section><nav><aside> 或具有与这些元素隐含 ARIA 角色相同的 ARIA 角色的元素内时,<header> 元素将具有 generic 角色,而不再被视为地标。在这种情况下,它不能使用 aria-labelaria-labelledby 进行标记。

示例

html
<header>
  <h1>Main Page Title</h1>
  <img src="mdn-logo-sm.png" alt="MDN logo" />
</header>

结果

文章标题

html
<article>
  <header>
    <h2>The Planet Earth</h2>
    <p>
      Posted on Wednesday, <time datetime="2017-10-04">4 October 2017</time> by
      Jane Smith
    </p>
  </header>
  <p>
    We live on a planet that's blue and green, with so many things still unseen.
  </p>
  <p><a href="https://example.com/the-planet-earth/">Continue reading…</a></p>
</article>

结果

技术摘要

内容类别 流内容可感知内容
允许内容 流内容,但不包含 <header><footer> 后代元素。
标签省略 无,起始标签和结束标签都必须存在。
允许父级 任何接受 流内容 的元素。请注意,<header> 元素不得是 <address><footer> 或另一个 <header> 元素的后代。
隐式 ARIA 角色 banner,或者如果它是 articleasidemainnavsection 元素的后代,或者是一个具有 articlecomplementarymainnavigationregion 角色的元素,则为 generic
允许的 ARIA 角色 grouppresentationnone
DOM 接口 HTMLElement

规范

规范
HTML
# the-header-element

浏览器兼容性

另见