# Figure (/docs/figure)





`Figure` renders a `<figure>` and, when you pass a `caption`, a `<figcaption>`. Put an [`Image`](/docs/image), a video, an [`AspectRatio`](/docs/aspect-ratio) or any other media inside it. It's a Server Component, so it adds no JavaScript.

```tsx
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 [#playground]

<story.Playground />

### Full prop browser [#full-prop-browser]

<story.WithControl />

## Placement [#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.

<Preview align="block" code="&#x22;<Figure caption=\&#x22;…\&#x22;>…</Figure>\n<Figure caption=\&#x22;…\&#x22; placement=\&#x22;top\&#x22;>…</Figure>\n<Figure caption=\&#x22;…\&#x22; placement=\&#x22;overlay\&#x22;>…</Figure>&#x22;">
  <div className="flex flex-wrap items-start justify-center gap-6">
    {[["bottom", "below"], ["top", "above"], ["overlay", "over"]].map(([placement, where]) => (
          <Figure key={placement} placement={placement} caption={`placement="${placement}": ${where} the image.`}>
            <Image
              src={`${PHOTO.mountains}?w=400&q=70`}
              alt="Snow-capped peaks rising above a sea of clouds"
              width={176}
              height={132}
              placeholder={BLUR.mountains}
            />
          </Figure>
        ))}
  </div>
</Preview>

## Sizing [#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:

<Preview align="block" code="&#x22;<Figure className=\&#x22;w-full\&#x22; caption=\&#x22;…\&#x22;>\n  <Image src={src} alt=\&#x22;…\&#x22; ratio=\&#x22;21/9\&#x22; className=\&#x22;w-full\&#x22; />\n</Figure>&#x22;">
  <Figure className="w-full" caption="A lake at dawn. A fluid figure: it takes the width of its container, and the image fills it.">
    <Image src="`${PHOTO.lake}?w=1200&q=70`" alt="A mountain lake at dawn, with pine forest on both shores" ratio="21/9" placeholder="BLUR.lake" className="w-full" />
  </Figure>
</Preview>

## Overlay [#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.

<Preview align="block" code="&#x22;<Figure placement=\&#x22;overlay\&#x22; caption=\&#x22;…\&#x22;>\n  <Image src={snow} alt=\&#x22;…\&#x22; width={480} height={320} />\n</Figure>&#x22;">
  <div className="flex justify-center">
    <Figure placement="overlay" caption="A farmhouse after a night of snow. The scrim keeps this legible over the brightest part of the photo.">
      <Image src="`${PHOTO.snow}?w=960&q=70`" alt="A wooden farmhouse with a thatched roof, half buried in snow at the edge of a white forest" width="480" height="320" placeholder="#e9ebee" />
    </Figure>
  </div>
</Preview>

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 [#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`.

<Preview code="&#x22;<Figure>\n  <Image src={src} alt=\&#x22;…\&#x22; width={320} height={213} />\n  <Figure.Caption>\n    A lake at dawn. Photo: <a href=\&#x22;https://unsplash.com\&#x22;>Unsplash</a>.\n  </Figure.Caption>\n</Figure>&#x22;">
  <Figure>
    <Image src="`${PHOTO.lake}?w=640&q=70`" alt="A mountain lake at dawn, with pine forest on both shores" width="320" height="213" placeholder="BLUR.lake" />

    <Figure.Caption>
      A lake at dawn. Photo:{" "}

      <a href="https://unsplash.com" className="underline underline-offset-2">
        Unsplash
      </a>

      .
    </Figure.Caption>
  </Figure>
</Preview>

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

## Other media [#other-media]

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

<Preview align="block" code="&#x22;<Figure className=\&#x22;w-full\&#x22; caption=\&#x22;42 Paris, 96 boulevard Bessières.\&#x22;>\n  <AspectRatio ratio=\&#x22;16/9\&#x22;>\n    <iframe src={mapUrl} title=\&#x22;Map of 42 Paris\&#x22; loading=\&#x22;lazy\&#x22; className=\&#x22;border-0\&#x22; />\n  </AspectRatio>\n</Figure>&#x22;">
  <div className="mx-auto w-full max-w-md">
    <Figure className="w-full" caption="42 Paris, 96 boulevard Bessières, 75017 Paris.">
      <AspectRatio ratio="16/9">
        <iframe src="MAP_EMBED" title="Map of 42 Paris" loading="lazy" className="border-0" />
      </AspectRatio>
    </Figure>
  </div>
</Preview>

## Alt text and captions [#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.

```tsx
// ✗ 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 [#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.

<Preview align="block" code="&#x22;<div dir=\&#x22;rtl\&#x22; lang=\&#x22;ar\&#x22;>\n  <Figure caption=\&#x22;فوق الغيوم، بعد ساعة من شروق الشمس.\&#x22;>\n    <Image src={src} alt=\&#x22;قمم مغطاة بالثلوج فوق بحر من الغيوم\&#x22; width={208} height={156} />\n  </Figure>\n</div>&#x22;">
  <div className="grid w-full gap-6 sm:grid-cols-2">
    <div dir="ltr" lang="en" className="flex flex-wrap items-start justify-center gap-4">
      <Figure caption="Above the clouds, an hour after sunrise.">
        <Image src="`${PHOTO.mountains}?w=480&q=70`" alt="Snow-capped peaks rising above a sea of clouds" width="208" height="156" placeholder="BLUR.mountains" />
      </Figure>

      <Figure placement="overlay" caption="Above the clouds at sunrise.">
        <Image src="`${PHOTO.mountains}?w=480&q=70`" alt="Snow-capped peaks rising above a sea of clouds" width="208" height="156" placeholder="BLUR.mountains" />
      </Figure>
    </div>

    <div dir="rtl" lang="ar" className="flex flex-wrap items-start justify-center gap-4">
      <Figure caption="فوق الغيوم، بعد ساعة من شروق الشمس.">
        <Image src="`${PHOTO.mountains}?w=480&q=70`" alt="قمم مغطاة بالثلوج فوق بحر من الغيوم" width="208" height="156" placeholder="BLUR.mountains" />
      </Figure>

      <Figure placement="overlay" caption="فوق الغيوم عند الشروق.">
        <Image src="`${PHOTO.mountains}?w=480&q=70`" alt="قمم مغطاة بالثلوج فوق بحر من الغيوم" width="208" height="156" placeholder="BLUR.mountains" />
      </Figure>
    </div>
  </div>
</Preview>

## API [#api]

### Figure [#figure]

<TypeTable
  type="{
  caption: {
    type: &#x22;ReactNode&#x22;,
    description: &#x22;The caption, rendered in a <figcaption>. Adds context to the media: it isn't its alt text.&#x22;,
  },
  placement: {
    type: &#x22;'bottom' | 'top' | 'overlay'&#x22;,
    default: &#x22;'bottom'&#x22;,
    description: &#x22;Below the media, above it (first in the DOM too), or over its bottom edge on a scrim.&#x22;,
  },
  radius: {
    type: &#x22;'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'&#x22;,
    default: &#x22;'xs'&#x22;,
    description: &#x22;Corner radius, on Image's scale. An overlay caption is clipped to it: match the media's.&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the <figure>. The figure hugs its media; size it here for fluid media.&#x22;,
  },
  classNames: {
    type: &#x22;{ root?, caption? }&#x22;,
    description: &#x22;Per-part class overrides. caption applies to the caption prop's <figcaption>.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the <figure>. See [Testing](/docs/testing).&#x22;,
  },
}"
/>

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 [#figurecaption]

<TypeTable
  type="{
  asChild: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description: &#x22;Renders as your element, with the caption's styles merged in.&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the <figcaption>.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the <figcaption>.&#x22;,
  },
}"
/>

## Accessibility [#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-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 [#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.

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