Skip to content
Component installs need the registry setup— the Base UI shadcn project, the @vegastack namespace and the Cloudflare Access service token.
VegaStack Design

Aspect Ratio

Constrains its children to a desired width-to-height ratio — widescreen media, square thumbnails, or portrait crops.

Status
stable
Since
0.10.0
Accessibility pattern
presentational wrapper

Last updated

A scenic landscape

Install

Add Aspect Ratio from the VegaStack registry. The CLI verifies the item's integrity hash before writing it.

pnpm dlx shadcn@latest add @vegastack/aspect-ratio

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio";

<AspectRatio ratio={16 / 9}>
  <img
    src="/photo.jpg"
    alt="Photo"
    className="absolute inset-0 size-full object-cover"
  />
</AspectRatio>;
A scenic landscape

Examples

Square

A square aspect ratio using ratio={1 / 1}. Useful for displaying images in a square format.

A scenic landscape

Portrait

A portrait aspect ratio using ratio={9 / 16}. Useful for displaying images in a portrait format.

A scenic landscape

RTL

The box reserves space with aspect-ratio and a CSS custom property, neither of which has a writing direction — so a right-to-left document gets the identical geometry, and only the surrounding caption and content flow mirror.

A scenic landscape
Beautiful landscape
منظر طبيعي جميل
منظر طبيعي جميل

API Reference

PropTypeDefaultDescription
ratio*number

Accessibility

  • AspectRatio renders a plain <div> and contributes nothing to the accessibility tree — it is geometry, not semantics.
  • The accessible name belongs to the child. Give a meaningful image an alt, and a decorative one alt="" so a screen reader skips it.
  • The element is position: relative, so a filling child is positioned with absolute inset-0 rather than being stretched by the wrapper — nothing is clipped out of the reading order.
ContractStates tested
Behaviourdefault
Accessibilitynative-or-base-ui-semantics, browser-accessibility-test
Visualdefault

Do / Don't

Do
Pass the ratio as an expression — ratio={16 / 9} — so the intent stays readable.
Don't
Set a fixed height on the wrapper; that is the one thing the ratio is there to compute.

Deviations

Upstream's file plus packages/ui/upstream/patches/aspect-ratio.patch. Every hunk:

  • DOC-2cn is imported from @vegastack/design.

On this page