React Aspect Ratio Component
A React aspect ratio box that keeps images, video and maps at a fixed width-to-height ratio, preventing layout shift. Use AspectRatio to reserve the space of an image, a video poster or a map before it loads, so the page does not jump when the content arrives.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/aspect-ratio.jsonScreenshots, a video poster and a profile card
ratio is width divided by height. Images fill the box (size-full object-cover); overlays are positioned inside it. Every tile keeps its ratio at any width, so nothing jumps while images load.
16 : 9
4 : 3
1 : 12:14
Maya ChenDesign lead
import { AspectRatio } from "@/components/sheenui/aspect-ratio/aspect-ratio";
import { Button } from "@/components/sheenui/button/button";
// Package build (after the first npm release, or a packed tarball): import { AspectRatio, Button } from "@sheenui/theme";
// This example also uses button: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json
export function Example() {
return (
<div className="grid w-full gap-4 sm:grid-cols-3">
<figure className="m-0 flex flex-col gap-2">
<AspectRatio ratio={16 / 9} className="rounded-sheen-xl border border-sheen-border">
<img src="/img/glassmorphism-dashboard-react-aurora-dark-768.webp" alt="A dark analytics dashboard in glass" className="size-full object-cover" />
</AspectRatio>
<figcaption className="text-sm font-medium">Analytics dashboard</figcaption>
</figure>
<figure className="m-0 flex flex-col gap-2">
<AspectRatio ratio={4 / 3} className="rounded-sheen-xl border border-sheen-border">
<img src="/img/glassmorphism-login-form-react-liquid-light-768.webp" alt="A light glass login form" className="size-full object-cover" />
</AspectRatio>
<figcaption className="text-sm font-medium">Login form</figcaption>
</figure>
<figure className="m-0 flex flex-col gap-2">
<AspectRatio ratio={21 / 9} className="rounded-sheen-xl border border-sheen-border bg-sheen-accent-soft">
<div className="absolute inset-0 grid place-items-center">
<Button size="icon" variant="glass" aria-label="Play the product tour">▶</Button>
</div>
</AspectRatio>
<figcaption className="text-sm font-medium">Product tour</figcaption>
</figure>
</div>
);
}Files to copy
3 folders · 1 file · 4 packages
- From
theme/src/ components/aspect-ratio/lib/cn.tsstyles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/aspect-ratio.jsonRegistry item: aspect-ratio. The styles need the Tailwind v4 glass UI setup.- The examples also use
components/button/components/spinner/hooks/{use-latest.ts, use-magnetic.ts, use-media-query.ts, use-preferences.ts}motion/{gate.ts, tokens.ts}lib/{form.ts, state-light.tsx}
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
A figure with a caption
Reserve the space first, then fill it. Put the AspectRatio inside a figure so the caption belongs to it; the box keeps its ratio whatever the width of the figure.
import { AspectRatio } from "@/components/sheenui/aspect-ratio/aspect-ratio";
// Package build (after the first npm release, or a packed tarball): import { AspectRatio } from "@sheenui/theme";
export function Example() {
return (
<figure className="flex w-64 flex-col gap-2">
<AspectRatio ratio={4 / 3} render={<div />} className="rounded-sheen-lg bg-gradient-to-br from-violet-400 to-teal-300" />
<figcaption className="text-sm text-sheen-fg-muted">A 4:3 placeholder: the space is reserved before the image loads.</figcaption>
</figure>
);
}Props
Keyboard
Accessibility
- No role of its own. Give the media inside it text: alt on an image, a title or caption on a video or map.
- It reserves space, which prevents layout shift (CLS) while content loads.
- Pass render to use another element, such as a figure. className and ref go on the box.
When to use the aspect ratio
- Use AspectRatio for a 16:9 video poster, a square product photo or a map tile in a card grid, so every cell keeps its shape and nothing moves while the images load.
- Pass ratio as width divided by height: 16 / 9 for video, 4 / 3 for photos, 1 for avatars and product tiles. The box keeps that ratio at any width.
- Fill the box with size-full object-cover on the image, and give the box rounded corners: it clips its children.
- Skip it for content whose height should follow its text. A card with copy should size itself, not hold a ratio.
- Pair it with Skeleton as the loading state, with Carousel slides that must share one height, and with figure and figcaption through the render prop.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .