網站導覽

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,就能在載入前保留指定的長寬比例。載入後,媒體會覆蓋整個區域。省略此設定時,媒體會維持原本的比例。兩種情況都請將 width 和 height 寫在 <img> 上,讓瀏覽器預留空間。

#加框

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-media可使用 img、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;  }}