A wrapper with a fixed aspect ratio for pictures, video and embeds, so the page does not jump while they load. It is the CSS aspect-ratio property: a server component, no JavaScript. Direct img, video and iframe children fill the box.
Preview

Installation
Usage
import { AspectRatio } from "@/components/ui/aspect-ratio"
export default function Example() {
return (
<AspectRatio ratio={16 / 9} className="rounded-lg">
<img src="/photo.webp" alt="Mountains at dawn" />
</AspectRatio>
)
}Examples
Common ratios
Pass a number or a string such as 16/9, 4/3 or 1.



<AspectRatio ratio="16/9">…</AspectRatio>
<AspectRatio ratio="4/3">…</AspectRatio>
<AspectRatio ratio={1}>…</AspectRatio>Fit: cover and contain
cover crops the picture to fill the box, contain shows all of it with free space around.


<AspectRatio ratio="16/9" fit="contain" className="bg-muted">
<img src="/photo.webp" alt="" />
</AspectRatio>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | number | string | 1 | Width divided by height: a number or a string like 16/9 |
| fit | "cover" | "contain" | "fill" | "cover" | How a direct img or video child fits the box |
| className | string | - | Additional CSS classes |