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.
AspectRatio is a <div> with the CSS aspect-ratio property. Give it a width, and it takes the height that matches ratio. It's in the server HTML, so the page doesn't shift when the content arrives. There's no padding-top trick: the browser does the math.
Its direct children fill it. An <img> or <video> child is cropped to it with object-fit: cover. It's a Server Component, so it adds no JavaScript.
import { AspectRatio } from '@42-central/ui-react/aspect-ratio';
<AspectRatio ratio="16/9" className="w-full">
<iframe src={embedUrl} title="Product tour" />
</AspectRatio>Playground
Component
Dir
Presets
Props
Code
<Component ratio="16/9" />Full prop browser
Video
A <video> has the ratio of its file, but only once its metadata has loaded. Until then it's 300 by 150 pixels, and the page shifts when it resizes. AspectRatio holds the box from the first paint, and crops the video to it.
Code
<AspectRatio ratio="16/9" className="w-full max-w-xl">
<video src={src} controls muted playsInline preload="metadata" />
</AspectRatio>With asChild, the ratio goes on the video itself, without a wrapper <div>:
<AspectRatio asChild ratio="16/9">
<video src={src} controls muted playsInline />
</AspectRatio>Embeds and maps
An <iframe> has no ratio at all: it's 300 by 150 pixels until you size it. Embeds from video hosts, maps and slide decks all need a box like this one. Give every iframe a title: it's the iframe's accessible name.
Code
<AspectRatio ratio="4/3" className="w-full">
<iframe src={mapUrl} title="Map of 42 Paris" loading="lazy" className="border-0" />
</AspectRatio>Layers
Every direct child fills the box. To put something on top of the media, add it as another child with absolute inset-0, and lay out its content inside that layer. The box is the layer's containing block, and it clips the layer to its corners.
This is the one case where an Image goes inside an AspectRatio: the box holds the image and a layer over it. Here the whole box is a link, through asChild.
Code
<AspectRatio asChild ratio="16/9" className="w-full max-w-md">
<a href={videoUrl} aria-label="Play the video, 0:05">
<Image src={poster} alt="" placeholder={blur} />
<span className="absolute inset-0 flex items-center justify-center">
<span className="rounded-full bg-black/60 p-3 text-white">
<Play aria-hidden />
</span>
<Badge className="absolute end-2 bottom-2">0:05</Badge>
</span>
</a>
</AspectRatio>The image is decorative here (alt=""): the link's aria-label already says what it is.
Radius
radius uses the same scale as Image, and defaults to xs like every media component in the kit. The box clips its content to the corners, so an iframe or a video gets them too. Use radius="none" for a square edge.
Code
<AspectRatio ratio="1/1" radius="none" … />
<AspectRatio ratio="1/1" radius="xs" … />
<AspectRatio ratio="1/1" radius="md" … />
<AspectRatio ratio="1/1" radius="xl" … />
<AspectRatio ratio="1/1" radius="full" … />Right-to-left
AspectRatio uses logical properties only, so nothing about it changes with the direction. Layers you add should use logical utilities too (start-*, end-*, ps-*), like the badge in Layers, which sits in the bottom-end corner: bottom-right in a left-to-right page, bottom-left in a right-to-left one.
Code
<div dir="rtl">
<AspectRatio ratio="16/9">
<Image src={src} alt="" />
<span className="absolute inset-0">
<Badge className="absolute end-2 bottom-2">0:05</Badge>
</span>
</AspectRatio>
</div>API
Prop
Type
Every other <div> attribute is passed through.
Accessibility
- No semantics of its own.
AspectRatiois a plain<div>: it adds no role, takes no focus and doesn't change what its children announce. UseasChildto make the box a link or a button, and give that element the accessible name. - Name your iframes. Every
<iframe>needs atitle. Screen readers announce it when they reach the frame. - Caption your videos. A
<video>with speech needs a<track kind="captions">.AspectRatiodoesn't change that. - Clipped, not scrolled. The box uses
overflow: clip, so it never becomes a scroll container: a keyboard user can't end up scrolled inside it, and focus rings on the box itself aren't cut off. - Right-to-left. The box uses logical properties only.
Internationalization
AspectRatio renders no text, so it has no translations prop. An iframe's title, a video's captions, and any text in a layer are your content: translate them like the rest of the page.
Changelog
Image
An img that holds its box while it loads, shows a blurred, colored or skeleton placeholder, and crossfades in. Takes a CDN loader for responsive srcsets and falls back cleanly when loading fails.
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.