# AspectRatio (/docs/aspect-ratio)





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

```tsx
import { AspectRatio } from '@42-central/ui-react/aspect-ratio';

<AspectRatio ratio="16/9" className="w-full">
  <iframe src={embedUrl} title="Product tour" />
</AspectRatio>
```

<Callout type="info" title="For a single image, use Image's ratio">
  [`Image`](/docs/image) takes a `ratio` of its own. It reserves the same box, and it also paints the placeholder and crossfades the image in. Wrapping an `Image` in an `AspectRatio` only makes sense when the box holds more than the image, such as a play button or a badge on top of it. See [Layers](#layers).
</Callout>

## Playground [#playground]

<story.Playground />

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

<story.WithControl />

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

<Preview code="&#x22;<AspectRatio ratio=\&#x22;16/9\&#x22; className=\&#x22;w-full max-w-xl\&#x22;>\n  <video src={src} controls muted playsInline preload=\&#x22;metadata\&#x22; />\n</AspectRatio>&#x22;">
  <AspectRatio ratio="16/9" className="w-full max-w-xl">
    <video src="VIDEO" preload="metadata" />
  </AspectRatio>
</Preview>

With `asChild`, the ratio goes on the video itself, without a wrapper `<div>`:

```tsx
<AspectRatio asChild ratio="16/9">
  <video src={src} controls muted playsInline />
</AspectRatio>
```

## Embeds and maps [#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.

<Preview align="block" code="&#x22;<AspectRatio ratio=\&#x22;4/3\&#x22; className=\&#x22;w-full\&#x22;>\n  <iframe src={mapUrl} title=\&#x22;Map of 42 Paris\&#x22; loading=\&#x22;lazy\&#x22; className=\&#x22;border-0\&#x22; />\n</AspectRatio>&#x22;">
  <div className="mx-auto w-full max-w-md">
    <AspectRatio ratio="4/3" className="w-full">
      <iframe src="MAP_EMBED" title="Map of 42 Paris" loading="lazy" className="border-0" />
    </AspectRatio>
  </div>
</Preview>

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

<Preview code="&#x22;<AspectRatio asChild ratio=\&#x22;16/9\&#x22; className=\&#x22;w-full max-w-md\&#x22;>\n  <a href={videoUrl} aria-label=\&#x22;Play the video, 0:05\&#x22;>\n    <Image src={poster} alt=\&#x22;\&#x22; placeholder={blur} />\n    <span className=\&#x22;absolute inset-0 flex items-center justify-center\&#x22;>\n      <span className=\&#x22;rounded-full bg-black/60 p-3 text-white\&#x22;>\n        <Play aria-hidden />\n      </span>\n      <Badge className=\&#x22;absolute end-2 bottom-2\&#x22;>0:05</Badge>\n    </span>\n  </a>\n</AspectRatio>&#x22;">
  <AspectRatio ratio="16/9" className="w-full max-w-md">
    <a href="VIDEO" aria-label="Play the video, 0:05">
      <Image src="`${PHOTO.mountains}?w=800&q=70`" placeholder="BLUR.mountains" />

      <span className="absolute inset-0 flex items-center justify-center">
        <span className="rounded-full bg-black/60 p-3 text-white">
          <Play className="size-6" />
        </span>

        <Badge className="absolute end-2 bottom-2">
          0:05
        </Badge>
      </span>
    </a>
  </AspectRatio>
</Preview>

The image is decorative here (`alt=""`): the link's `aria-label` already says what it is.

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

<Preview align="start" code="&#x22;<AspectRatio ratio=\&#x22;1/1\&#x22; radius=\&#x22;none\&#x22; … />\n<AspectRatio ratio=\&#x22;1/1\&#x22; radius=\&#x22;xs\&#x22; … />\n<AspectRatio ratio=\&#x22;1/1\&#x22; radius=\&#x22;md\&#x22; … />\n<AspectRatio ratio=\&#x22;1/1\&#x22; radius=\&#x22;xl\&#x22; … />\n<AspectRatio ratio=\&#x22;1/1\&#x22; radius=\&#x22;full\&#x22; … />&#x22;">
  {["none", "xs", "md", "xl", "full"].map((radius) => (
      <AspectRatio key={radius} ratio="1/1" radius={radius} className="w-20">
        <div className="flex items-center justify-center bg-brand-500/15 text-xs dark:bg-brand-400/20">
          {radius}
        </div>
      </AspectRatio>
    ))}
</Preview>

## Right-to-left [#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](#layers), which sits in the bottom-end corner: bottom-right in a left-to-right page, bottom-left in a right-to-left one.

<Preview align="block" code="&#x22;<div dir=\&#x22;rtl\&#x22;>\n  <AspectRatio ratio=\&#x22;16/9\&#x22;>\n    <Image src={src} alt=\&#x22;\&#x22; />\n    <span className=\&#x22;absolute inset-0\&#x22;>\n      <Badge className=\&#x22;absolute end-2 bottom-2\&#x22;>0:05</Badge>\n    </span>\n  </AspectRatio>\n</div>&#x22;">
  <div className="grid w-full grid-cols-2 gap-3 sm:max-w-md">
    {["ltr", "rtl"].map((dir) => (
          <div key={dir} dir={dir} className="flex flex-col gap-1.5">
            <AspectRatio ratio="16/9" className="w-full">
              <Image src={`${PHOTO.lake}?w=480&q=70`} alt="" placeholder={BLUR.lake} />
              <span className="absolute inset-0">
                <Badge className="absolute end-2 bottom-2">0:05</Badge>
              </span>
            </AspectRatio>
            <span className="text-xs text-black/50 dark:text-white/50">dir="{dir}"</span>
          </div>
        ))}
  </div>
</Preview>

## API [#api]

<TypeTable
  type="{
  ratio: {
    type: &#x22;number | `${number}/${number}`&#x22;,
    required: true,
    description:
      &#x22;Width over height: 16 / 9 or \&#x22;16/9\&#x22;. A zero or malformed ratio is ignored, and the box takes its content's height.&#x22;,
  },
  radius: {
    type: &#x22;'none' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full'&#x22;,
    default: &#x22;'xs'&#x22;,
    description: &#x22;Corner radius, on the same scale as Image. The content is clipped to it.&#x22;,
  },
  asChild: {
    type: &#x22;boolean&#x22;,
    default: &#x22;false&#x22;,
    description: &#x22;Puts the ratio on your element instead of a wrapper <div>.&#x22;,
  },
  className: {
    type: &#x22;string&#x22;,
    description: &#x22;Applies to the box. Give it a width here.&#x22;,
  },
  style: {
    type: &#x22;CSSProperties&#x22;,
    description: &#x22;Applies to the box. aspect-ratio is merged in.&#x22;,
  },
  testId: {
    type: &#x22;string&#x22;,
    description: &#x22;Renders as data-testid on the box. See [Testing](/docs/testing).&#x22;,
  },
}"
/>

Every other `<div>` attribute is passed through.

## Accessibility [#accessibility]

* **No semantics of its own.** `AspectRatio` is a plain `<div>`: it adds no role, takes no focus and doesn't change what its children announce. Use `asChild` to make the box a link or a button, and give that element the accessible name.
* **Name your iframes.** Every `<iframe>` needs a `title`. Screen readers announce it when they reach the frame.
* **Caption your videos.** A `<video>` with speech needs a `<track kind="captions">`. `AspectRatio` doesn'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 [#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.
