导航

v2.1.0
媒体

图文块

带边缘处理和图注的图片或视频。

本页内容

图文块由图注和一张图片、一段视频或一幅绘图组成。媒体沿用表面的圆角,并在边缘内侧加一圈细线,让浅色图像的边界清晰可辨,也让深色图像在边缘处不显突兀。图注紧贴媒体下方,样式低调。适合放大查看的图片可在灯箱中打开。

图文块

<figure class="figure max-w(360) mx-auto">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 3 / 2">  <figcaption class="figure-caption">    Moonstone under a north light, the stone the light theme is named for.  </figcaption></figure>
<figure className="figure max-w(360) mx-auto">  <img className="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style={{ '--figure-ratio': '3 / 2' } as React.CSSProperties} />  <figcaption className="figure-caption">    Moonstone under a north light, the stone the light theme is named for.  </figcaption></figure>
<figure class="figure max-w(360) mx-auto">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 3 / 2">  <figcaption class="figure-caption">    Moonstone under a north light, the stone the light theme is named for.  </figcaption></figure>
<template>  <figure class="figure max-w(360) mx-auto">    <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 3 / 2">    <figcaption class="figure-caption">      Moonstone under a north light, the stone the light theme is named for.    </figcaption>  </figure></template>

为媒体设置 --figure-ratio,可在加载前确定显示区域的比例,加载后媒体会覆盖该区域。省略时,媒体保持自身比例。无论是否设置,都应在 <img> 上写明 width 和 height,让浏览器预留空间。

#带框

figure-framed 将媒体放入托盘,四周内缩 4px,像照片嵌在卡纸衬框里,图注也移到框内。托盘的圆角半径等于媒体的圆角半径加上内缩量,使圆角处的间距也保持一致。

带框

<figure class="figure figure-framed max-w(360) mx-auto">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 16 / 9">  <figcaption class="figure-caption">Figure 2. The same stone, framed.</figcaption></figure>
<figure className="figure figure-framed max-w(360) mx-auto">  <img className="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style={{ '--figure-ratio': '16 / 9' } as React.CSSProperties} />  <figcaption className="figure-caption">Figure 2. The same stone, framed.</figcaption></figure>
<figure class="figure figure-framed max-w(360) mx-auto">  <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 16 / 9">  <figcaption class="figure-caption">Figure 2. The same stone, framed.</figcaption></figure>
<template>  <figure class="figure figure-framed max-w(360) mx-auto">    <img class="figure-media" src="/demo/moonstone.svg" alt="A pale stone with a band of light across it" style="--figure-ratio: 16 / 9">    <figcaption class="figure-caption">Figure 2. The same stone, framed.</figcaption>  </figure></template>

#居中

figure-center 让图注居中并均衡分行,适用于正文栏中独立呈现的图文块。

居中

<figure class="figure figure-center max-w(240) mx-auto">  <img class="figure-media" src="/demo/portrait-2.svg" alt="Alan Turing" style="--figure-ratio: 1">  <figcaption class="figure-caption">Alan Turing, drawn for the avatar examples.</figcaption></figure>
<figure className="figure figure-center max-w(240) mx-auto">  <img className="figure-media" src="/demo/portrait-2.svg" alt="Alan Turing" style={{ '--figure-ratio': '1' } as React.CSSProperties} />  <figcaption className="figure-caption">Alan Turing, drawn for the avatar examples.</figcaption></figure>
<figure class="figure figure-center max-w(240) mx-auto">  <img class="figure-media" src="/demo/portrait-2.svg" alt="Alan Turing" style="--figure-ratio: 1">  <figcaption class="figure-caption">Alan Turing, drawn for the avatar examples.</figcaption></figure>
<template>  <figure class="figure figure-center max-w(240) mx-auto">    <img class="figure-media" src="/demo/portrait-2.svg" alt="Alan Turing" style="--figure-ratio: 1">    <figcaption class="figure-caption">Alan Turing, drawn for the avatar examples.</figcaption>  </figure></template>

#无障碍

  • 将 <figure> 与 <figcaption> 搭配使用,图注即为图文块的无障碍名称。
  • 图注不能替代 alt。替代文本描述图像展示的内容,图注说明图像为何出现在这里。若图注已涵盖全部信息,使用空的替代文本 alt="" 可避免重复朗读。
  • 媒体边缘内侧的细线仅作装饰,随配色方案变化。在强制颜色模式下,带框图文块使用系统轮廓线。

#类名

类名说明
figure图文组合,媒体在上,说明文字在下
figure-framed媒体置于托盘中,向内缩进 4px,说明文字位于边框内。与 figure 一同使用
figure-center用于在正文栏中独立呈现的 figure,图注居中,各行长度均衡。与 figure 一同使用
figure-mediaimg、video、svg 或任意块级元素,采用表面圆角,内沿绘制一圈细线
figure-caption媒体下方的小号低对比度说明文字

#自定义属性

属性默认值说明
--figure-radiusvar(--radius-surface)媒体的圆角
--figure-ratio媒体加载前按此宽高比预留空间,加载后铺满该区域。未设置时,媒体保持自身比例。在 figure-media 上设置

#源码

@master/ui/components/figure.css
/* * Figure: an image, a video or a drawing with its caption. * The media takes a surface's corner and a hairline ring inside its own edge, so a pale image does not * dissolve into the page and a dark one does not end in a hard cut. The caption sits close under it, quiet. */@layer components.ui {  .figure {    --figure-radius: var(--radius-surface);    display: grid;    gap: .625rem;    min-width: 0;    margin: 0;    /*     * Framed: the media rests in a tray, 4px in, like a print in a mat. The tray's corner is the media's     * corner plus that inset.     */    &.figure-framed {      --surface-color: var(--color-surface-tray);      padding: 4px;      border-radius: calc(var(--figure-radius) * var(--corner-scale) + 4px);      corner-shape: var(--corner-shape);      background: var(--color-surface-tray);      box-shadow: inset 0 0 0 .5px var(--color-line-hair), inset 0 1px 2px -1px var(--color-shade-2);      gap: 0;      .figure-media {        box-shadow: var(--shadow-raised);        outline: 0;      }      .figure-caption {        padding: .5rem .625rem .375rem;      }    }    /* Centered: for a figure that stands alone in a column of text. */    &.figure-center .figure-caption {      text-align: center;      text-wrap: balance;    }    @media (forced-colors: active) {      &.figure-framed {        outline: 1px solid CanvasText;        outline-offset: -1px;      }    }  }  /* Set --figure-ratio to hold a shape before the media loads; the media then covers that shape. */  .figure-media {    display: block;    width: 100%;    max-width: 100%;    height: auto;    aspect-ratio: var(--figure-ratio, auto);    border-radius: calc(var(--figure-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    object-fit: cover;    outline: .5px solid var(--color-line-image-strong);    outline-offset: -.5px;  }  .figure-caption {    margin: 0;    color: var(--color-text-muted);    font-size: .8125rem;    line-height: 1.5;    text-wrap: pretty;    overflow-wrap: anywhere;  }}