<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

瀏覽器相容性

另見