<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> 元素。