<nav>: 導航區域元素

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

<nav> HTML 元素表示頁面中用於提供導航連結的部分,這些連結可以指向當前文件中的其他位置,也可以指向其他文件。導航區域的常見示例包括選單、目錄和索引。

試一試

<nav class="crumbs">
  <ol>
    <li class="crumb"><a href="#">Bikes</a></li>
    <li class="crumb"><a href="#">BMX</a></li>
    <li class="crumb">Jump Bike 3000</li>
  </ol>
</nav>

<h1>Jump Bike 3000</h1>
<p>
  This BMX bike is a solid step into the pro world. It looks as legit as it
  rides and is built to polish your skills.
</p>
nav {
  border-bottom: 1px solid black;
}

.crumbs ol {
  list-style-type: none;
  padding-left: 0;
}

.crumb {
  display: inline-block;
}

.crumb a::after {
  display: inline-block;
  color: black;
  content: ">";
  font-size: 80%;
  font-weight: bold;
  padding: 0 3px;
}

屬性

此元素僅包含全域性屬性

用法說明

  • 並非所有連結都必須包含在 <nav> 元素中。<nav> 僅用於主要的導航連結塊;通常,<footer> 元素中包含的連結列表不需要放在 <nav> 元素中。
  • 一個文件可以有多個 <nav> 元素,例如,一個用於站點導航,另一個用於頁面內導航。在這種情況下,可以使用 aria-labelledby 來提高可訪問性,請參閱 示例
  • 使用者代理,例如針對殘障使用者的螢幕閱讀器,可以使用此元素來確定是否應忽略導航內容的初始渲染。

示例

在此示例中,<nav> 塊用於包含一個無序列表(<ul>)的連結。透過適當的 CSS,這可以呈現為側邊欄、導航欄或下拉選單。

html
<nav class="menu">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

nav 元素的語義是提供連結。然而,nav 元素不一定必須包含列表,它也可以包含其他型別的內容。在這個導航塊中,連結是透過散文提供的。

html
<nav>
  <h2>Navigation</h2>
  <p>
    You are on my home page. To the north lies <a href="/blog">my blog</a>, from
    whence the sounds of battle can be heard. To the east you can see a large
    mountain, upon which many <a href="/school">school papers</a> are littered.
    Far up this mountain you can spy a little figure who appears to be me,
    desperately scribbling a <a href="/school/thesis">thesis</a>.
  </p>
  <p>
    To the west are several exits. One fun-looking exit is labeled
    <a href="https://games.example.com/">"games"</a>. Another more
    boring-looking exit is labeled <a href="https://isp.example.net/">ISP™</a>.
  </p>
  <p>
    To the south lies a dark and dank <a href="/about">contacts page</a>.
    Cobwebs cover its disused entrance, and at one point you see a rat run
    quickly out of the page.
  </p>
</nav>

技術摘要

內容類別 流內容區塊內容、可感知內容。
允許內容 流內容.
標籤省略 無,起始標籤和結束標籤都必須存在。
允許父級 任何接受流內容的元素。
隱式 ARIA 角色 navigation
允許的 ARIA 角色 不允許 role
DOM 介面 HTMLElement

規範

規範
HTML
# the-nav-element

瀏覽器相容性

另見