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
Dir
Presets
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.
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:
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.
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 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:
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:
altdescribes 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.
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 thefigurerole 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:
altdescribes the image, the caption adds context, and neither repeats the other. - Reading order follows the visual order. A
topcaption comes before the media in the DOM;bottomandoverlaycaptions 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
Canvasbackground andCanvasTexttext instead of the scrim. - Not interactive.
Figuretakes no focus. Links or buttons in aFigure.Captionare reachable as usual. - Right-to-left. Captions are
text-startand 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
AspectRatio
A box that keeps a width-to-height ratio, for media that can't hold one itself, like an embedded video, a map or a video element. Pure CSS aspect-ratio, so the shape is in the server HTML.
Gallery
A set of photos in a grid, masonry columns or justified rows. Every layout is plain CSS decided from each photo's size, so the server HTML is already the final layout.