<figure>: 帶有可選標題的 Figure 元素

Baseline 已廣泛支援

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

<figure> HTML 元素表示一個獨立的、可能帶有可選標題的內容,標題使用 <figcaption> 元素指定。Figure、其標題和內容被作為一個整體來引用。

試一試

<figure>
  <img
    src="/shared-assets/images/examples/elephant.jpg"
    alt="Elephant at sunset" />
  <figcaption>An elephant at sunset</figcaption>
</figure>
figure {
  border: thin silver solid;
  display: flex;
  flex-flow: column;
  padding: 5px;
  max-width: 220px;
  margin: auto;
}

img {
  max-width: 220px;
  max-height: 150px;
}

figcaption {
  background-color: #222222;
  color: white;
  font: italic smaller sans-serif;
  padding: 3px;
  text-align: center;
}

屬性

此元素僅包含全域性屬性

用法說明

  • 通常,<figure> 是一個圖片、插圖、圖表、程式碼片段等,它被引用在文件的主流程中,但可以被移動到文件的其他部分或附錄中,而不會影響主流程。
  • 可以透過在 <figure> 元素內部(作為第一個或最後一個子元素)插入一個 <figcaption> 來為其關聯一個標題。在 figure 中找到的第一個 <figcaption> 元素會被顯示為 figure 的標題。
  • <figcaption> 為其父級 <figure> 提供了 可訪問名稱

示例

影像

html
<!-- Just an image -->
<figure>
  <img src="favicon-192x192.png" alt="The beautiful MDN logo." />
</figure>

<!-- Image with a caption -->
<figure>
  <img src="favicon-192x192.png" alt="The beautiful MDN logo." />
  <figcaption>MDN Logo</figcaption>
</figure>

結果

程式碼片段

html
<figure>
  <figcaption>Get browser details using <code>navigator</code>.</figcaption>
  <pre>
function NavigatorExample() {
  let txt = `Browser CodeName: ${navigator.appCodeName};\n`;
  txt += `Browser Name: ${navigator.appName};\n`;
  txt += `Browser Version: ${navigator.appVersion};\n`;
  txt += `Cookies Enabled: ${navigator.cookieEnabled};\n`;
  txt += `Platform: ${navigator.platform};\n`;
  txt += `User-agent header: ${navigator.userAgent};`;
  console.log("NavigatorExample", txt);
}
  </pre>
</figure>

結果

引言

html
<figure>
  <figcaption><b>Edsger Dijkstra:</b></figcaption>
  <blockquote>
    If debugging is the process of removing software bugs, then programming must
    be the process of putting them in.
  </blockquote>
</figure>

結果

詩歌

html
<figure>
  <p>
    Bid me discourse, I will enchant thine ear,<br />
    Or like a fairy trip upon the green,<br />
    Or, like a nymph, with long dishevelled hair,<br />
    Dance on the sands, and yet no footing seen:<br />
    Love is a spirit all compact of fire,<br />
    Not gross to sink, but light, and will aspire.<br />
  </p>
  <figcaption><cite>Venus and Adonis</cite>, by William Shakespeare</figcaption>
</figure>

結果

技術摘要

內容類別 流內容可感知內容
允許內容 一個 <figcaption> 元素,後跟 流內容;或者流內容後跟一個 <figcaption> 元素;或者流內容。
標籤省略 無,起始標籤和結束標籤都必須存在。
允許父級 任何接受 流內容 的元素。
隱式 ARIA 角色 figure
允許的 ARIA 角色 沒有 figcaption 後代:任何,否則沒有允許的角色
DOM 介面 HTMLElement

規範

規範
HTML
# the-figure-element

瀏覽器相容性

另見

  • <figcaption> 元素。