Aspect ratio
FreeA box that keeps a fixed ratio so images and video never shift the layout.
pnpm dlx shadcn@latest add https://caliperui.site/r/aspect-ratio.jsonWhen to use #
Use it for
- Thumbnails, covers and embedded video.
- Skeletons that must match final media size.
Skip it when
- Content that should size itself.
Installation #
pnpm dlx shadcn@latest add https://caliperui.site/r/aspect-ratio.json
Adds its 2 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.
Usage #
This is the code behind the preview above. It is built from AspectRatio and AspectRatioImage.
import { AspectRatio } from "@/components/ui/aspect-ratio" export function WideBox() { return ( <div className="w-full max-w-sm"> <AspectRatio ratio={16 / 9} className="rounded-xl bg-muted"> <div className="absolute inset-0 grid place-items-center text-sm text-muted-foreground">16:9</div> </AspectRatio> </div> ); }
API reference #
AspectRatio
component
Also accepts the native props of its root element and a ref.
AspectRatioImage
component
No props of its own; it accepts the native props of its root element.
Accessibility #
It is built on native elements, so roles and focus come from the browser.
No custom key handling; Tab and the native activation keys do the work.
Motion #
Transitions use the shared duration and easing tokens. Under prefers-reduced-motion those tokens collapse to instant and travel distances go to zero, so state changes still happen without movement.