Aspect ratio
A media frame that keeps its ratio and shows a skeleton until the image loads.
ratio takes a number, "16/9" or "4:3". A direct child img, video, picture or iframe fills the frame; anything else is positioned by you.
Ratios
import { AspectRatio } from "@/components/ui/aspect-ratio"
const ratios = ["16/9", "4:3", 1] as const
export default function AspectRatioRatios() {
return (
<div className="grid w-full grid-cols-3 items-end gap-4">
{ratios.map((ratio) => (
<figure key={ratio} className="flex flex-col gap-2">
<AspectRatio ratio={ratio} className="rounded-md bg-muted text-muted-foreground">
<svg
viewBox="0 0 100 100"
preserveAspectRatio="none"
aria-hidden="true"
className="absolute inset-0 size-full"
>
<path d="M0 0 100 100M100 0 0 100" fill="none" stroke="currentColor" strokeOpacity="0.4" vectorEffect="non-scaling-stroke" />
</svg>
</AspectRatio>
<figcaption className="text-center font-mono text-xs text-muted-foreground">{ratio}</figcaption>
</figure>
))}
</div>
)
}
With an image
import { AspectRatio } from "@/components/ui/aspect-ratio"
const chart = `data:image/svg+xml,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 320">
<defs>
<linearGradient id="g" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#5b45f5"/>
<stop offset="1" stop-color="#191919"/>
</linearGradient>
</defs>
<rect width="320" height="320" fill="url(#g)"/>
<path d="M0 240 60 200 120 220 180 150 240 170 320 90V320H0Z" fill="#fff" fill-opacity=".16"/>
<path d="M0 240 60 200 120 220 180 150 240 170 320 90" fill="none" stroke="#fff" stroke-width="3"/>
</svg>`
)}`
export default function AspectRatioWithAnImage() {
return (
<AspectRatio ratio="21/9" className="max-w-md rounded-xl">
<img src={chart} alt="Tasks closed per week, rising over six weeks" />
</AspectRatio>
)
}
Loading and failed
import { FileAlertIcon } from "@keyline-icons/react"
import { AspectRatio } from "@/components/ui/aspect-ratio"
const broken = "data:image/png;base64,AAAA"
export default function AspectRatioLoadingAndFailed() {
return (
<div className="grid w-full max-w-md grid-cols-3 gap-4">
<AspectRatio ratio="4/3" className="rounded-md">
<img alt="A cover that has not arrived yet" />
</AspectRatio>
<AspectRatio ratio="4/3" className="rounded-md">
<img src={broken} alt="A cover that failed to load" />
</AspectRatio>
<AspectRatio ratio="4/3" className="rounded-md" fallback={<FileAlertIcon aria-hidden="true" />}>
<img src={broken} alt="A cover that failed to load, with its own fallback icon" />
</AspectRatio>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/aspect-ratiobunx --bun shadcn@latest add @khaeli/aspect-ratioInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
- @keyline-icons/react ^2.2.0
Accessible name
- The frame has no role. The image inside carries the name: give it an
alt, oralt=""when it is decoration. - While the image loads the frame has
aria-busy="true". The skeleton and the fallback icon are decoration, so a failed image is still announced by itsalt.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/aspect-ratio.json