|Kit

Figure

Media with a caption, as a real figure and figcaption. The caption sits below the media, above it, or over its bottom edge on a scrim that keeps text readable on any photo.

Figure renders a <figure> and, when you pass a caption, a <figcaption>. Put an Image, a video, an AspectRatio or any other media inside it. It's a Server Component, so it adds no JavaScript.

import { Figure } from '@42-central/ui-react/figure';
import { Image } from '@42-central/ui-react/image';

<Figure caption="Above the clouds, an hour after sunrise.">
  <Image src={photo.url} alt="Snow-capped peaks rising above a sea of clouds" width={320} height={213} />
</Figure>

Playground

Component

Loading preview

Props

Code

<Component caption="Above the clouds, an hour after sunrise." />

Full prop browser

Placement

placement puts the caption below the media (bottom, the default), above it (top) or over its bottom edge (overlay). A top caption also comes first in the DOM, so screen readers read it in the same order you see it.

Snow-capped peaks rising above a sea of clouds
placement="bottom": below the image.
placement="top": above the image.
Snow-capped peaks rising above a sea of clouds
Snow-capped peaks rising above a sea of clouds
placement="overlay": over the image.

Code

<Figure caption="…">…</Figure>
<Figure caption="…" placement="top">…</Figure>
<Figure caption="…" placement="overlay">…</Figure>

Sizing

The figure hugs its media: a 240px image makes a 240px figure, and the caption wraps to that width instead of stretching the figure. That's the contain: inline-size on the caption at work.

A fluid image has no width of its own to hug, so size the figure instead, and let the image fill it:

A mountain lake at dawn, with pine forest on both shores
A lake at dawn. A fluid figure: it takes the width of its container, and the image fills it.

Code

<Figure className="w-full" caption="…">
  <Image src={src} alt="…" ratio="21/9" className="w-full" />
</Figure>

Overlay

An overlay caption sits on a dark scrim over the media's bottom edge. The band behind the text is at least 60% black, which keeps white text above the WCAG AA ratio of 4.5:1 even over a pure white image. A gradient fades that band in above the text, so the edge doesn't show. The scrim is the same in light and dark mode, because it sits on the photo, not on the page.

A wooden farmhouse with a thatched roof, half buried in snow at the edge of a white forest
A farmhouse after a night of snow. The scrim keeps this legible over the brightest part of the photo.

Code

<Figure placement="overlay" caption="…">
  <Image src={snow} alt="…" width={480} height={320} />
</Figure>

The figure is clipped to its radius in this placement, so the scrim follows the media's corners. Both default to xs. If you change the image's radius, give the figure the same one.

Keep overlay captions short: a line or two. A long caption covers the photo it describes. Use bottom for anything longer.

Composed captions

Figure.Caption is the <figcaption> on its own, with the same styles and the same placements. Use it when the caption has structure of its own, like a credit with a link. Place it yourself: after the media for bottom and overlay, before it for top.

A mountain lake at dawn, with pine forest on both shores

A lake at dawn. Photo:

Unsplash

.

Code

<Figure>
  <Image src={src} alt="…" width={320} height={213} />
  <Figure.Caption>
    A lake at dawn. Photo: <a href="https://unsplash.com">Unsplash</a>.
  </Figure.Caption>
</Figure>

Figure.Caption takes asChild to render as your own element; the caption prop's styles go through classNames.caption.

Other media

Anything can be the figure's media. Here, a map in an AspectRatio:

42 Paris, 96 boulevard Bessières, 75017 Paris.

Code

<Figure className="w-full" caption="42 Paris, 96 boulevard Bessières.">
  <AspectRatio ratio="16/9">
    <iframe src={mapUrl} title="Map of 42 Paris" loading="lazy" className="border-0" />
  </AspectRatio>
</Figure>

Alt text and captions

They do different jobs, and screen readers read both, so never repeat one in the other:

  • alt describes the image for someone who can't see it: what's in it, in the words that matter here.
  • The caption adds context everyone gets: where, when, who took it, why it's on the page.
// ✗ The caption is read twice: once as alt, once as the caption.
<Figure caption="A lake at dawn.">
  <Image src={src} alt="A lake at dawn." />
</Figure>

// ✓ Each says something the other doesn't.
<Figure caption="Our first morning at the lake, after a night of rain.">
  <Image src={src} alt="A turquoise lake below steep, pine-covered mountains" />
</Figure>

When the caption already says everything the image shows, as with a chart whose caption gives the numbers, the image can be decorative: alt="".

Right-to-left

Captions use text-start and logical properties only, so they follow the direction: start-aligned on the left in a left-to-right page, on the right in a right-to-left one. The alt text and the caption are your content, so they come translated with the page.

Snow-capped peaks rising above a sea of clouds
Above the clouds, an hour after sunrise.
Snow-capped peaks rising above a sea of clouds
Above the clouds at sunrise.
قمم مغطاة بالثلوج فوق بحر من الغيوم
فوق الغيوم، بعد ساعة من شروق الشمس.
قمم مغطاة بالثلوج فوق بحر من الغيوم
فوق الغيوم عند الشروق.

Code

<div dir="rtl" lang="ar">
  <Figure caption="فوق الغيوم، بعد ساعة من شروق الشمس.">
    <Image src={src} alt="قمم مغطاة بالثلوج فوق بحر من الغيوم" width={208} height={156} />
  </Figure>
</div>

API

Figure

Prop

Type

Every other <figure> attribute is passed through. No asChild: the figure renders its caption beside its children, and a child element couldn't hold both.

Figure.Caption

Prop

Type

Accessibility

  • Real semantics. The root is a <figure> and the caption a <figcaption>, so the figure has the figure role and the caption is read as part of it, with the media. HTML-AAM also makes the caption the figure's accessible name, but not every browser does that yet (Chromium doesn't), so don't rely on it: the caption is still read either way.
  • Alt and caption do different jobs. See Alt text and captions: alt describes the image, the caption adds context, and neither repeats the other.
  • Reading order follows the visual order. A top caption comes before the media in the DOM; bottom and overlay captions come after it.
  • Overlay contrast. White text sits on a band at least 60% black, which is above 4.5:1 over any photo, including pure white. The scrim doesn't change with the theme, since it's over the image, not the page.
  • Forced colors. Windows High Contrast drops background images, so the overlay caption gets a solid Canvas background and CanvasText text instead of the scrim.
  • Not interactive. Figure takes no focus. Links or buttons in a Figure.Caption are reachable as usual.
  • Right-to-left. Captions are text-start and the layout uses logical properties only.

Internationalization

Figure renders no text of its own, so it has no translations prop. The caption and the media's alt text are your content: translate them with the rest of the page, for example from your message catalog.

<Figure caption={t('trip.caption', { place: photo.place })}>
  <Image src={photo.url} alt={t('trip.photoAlt', { place: photo.place })} ratio="3/2" />
</Figure>

Changelog

v0.20.0
  • New Figure with a caption or Figure.Caption placed bottom, top or overlay (a readable scrim over the photo); a Server Component that sizes to its media

    #347

On this page