Skip to content

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.json
Ratio 16:9, the space is reserved before the image loads.

Screenshots, 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.

A dark analytics dashboard in glass16 : 9
Analytics dashboard
A light glass login form4 : 3
Login form
A refraction lens over a gradient1 : 1
Refraction lens
2:14
Product tour · 21 : 9 video poster
Maya ChenDesign lead
Profile card · 1 : 1

Files to copy

3 folders · 1 file · 4 packages
From theme/src/
  • components/aspect-ratio/
  • lib/cn.ts
  • styles/
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.

A 4:3 placeholder: the space is reserved before the image loads.

Props

PropTypeDefaultDescription
rationumber1Width divided by height: 16 / 9, 4 / 3, 1 (default) for a square.

Keyboard

KeysAction
TabNot focusable: it is a plain container. Anything focusable inside it keeps its own tab stop.

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

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .