A box that keeps its shape as it resizes, for an image, a video or a map.
$ pnpm dlx shadcn@latest add @dashral/aspect-ratioInstallation
$ pnpm dlx shadcn@latest add @dashral/aspect-ratioAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/aspect-ratio.json
Usage
import { AspectRatio } from "@/components/dashral/aspect-ratio"
<AspectRatio ratio={16 / 9} className="rounded-xl bg-muted">
<img src={post.cover} alt={post.coverAlt} />
</AspectRatio>Using the npm package? Import from @dashral/ui/components/aspect-ratio instead, here and in the examples; the rest is the same.
Examples
Blog post cards
Covers of every size line up at 3 / 2, so the titles start on one line.
API reference
AspectRatio
Also takes every prop of Radix Aspect Ratio(opens in a new tab).
| Prop | Type | Default |
|---|---|---|
ratio | numberWidth over height: 4 / 3, 1 for a square, 3 / 4 for a portrait. | 16 / 9 |
className | stringThe box's classes, e.g. a radius or a background. An <img> or <video> without classes fills it, cropped to fit. | — |
wrapperClassName | stringThe wrapper that holds the ratio, e.g. a width (it's full width by default). | — |
Accessibility
- Only a box: the picture or video inside needs its own alt text or label.