Aspect Ratio
Constrains its children to a desired width-to-height ratio — widescreen media, square thumbnails, or portrait crops.
- Status
- Since
0.10.0- Accessibility pattern
- presentational wrapper
Last updated
Install
Configure registry credentials first using the Quickstart. Before copying this item or any transitive registry dependency, verify the signed manifest and save the exact bytes for Aspect Ratio before installation. Retain the digest printed by this command.
pnpm exec vegastack-design verify aspect-ratioInstall with the pinned shadcn CLI. Bare shadcn does not verify VegaStack signature or integrity.
pnpm dlx shadcn@4.21.0 add @vegastack/aspect-ratioRun the exact offline --post-write command printed by the preflight, using its saved item path and independently retained --expected-integrity digest. Use the saved preflight for every transitive registry dependency too; stop on any mismatch before using the components.
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>;Examples
Square
A square aspect ratio using ratio={1 / 1}. Useful for displaying images in a square format.
Portrait
A portrait aspect ratio using ratio={9 / 16}. Useful for displaying images in a portrait format.
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.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
ratio* | number | — |
Accessibility
AspectRatiorenders 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 onealt=""so a screen reader skips it. - The element is
position: relative, so a filling child is positioned withabsolute inset-0rather than being stretched by the wrapper — nothing is clipped out of the reading order.
| Contract | States tested |
|---|---|
| Behaviour | default |
| Accessibility | native-or-base-ui-semantics, browser-accessibility-test |
| Visual | default |
Do / Don't
Deviations
Upstream's file plus packages/ui/upstream/patches/aspect-ratio.patch. Every hunk:
- DOC-2 —
cnis imported from@vegastack/design.