Dashral Pro is hereSee pricing

Dashral

Aspect RatioNew

A box that keeps its shape as it resizes, for an image, a video or a map.

$ pnpm dlx shadcn@latest add @dashral/aspect-ratio
Mountains above a valley at dawn
16 / 9: the picture is cropped to fit, whatever its own size.

Installation

$ pnpm dlx shadcn@latest add @dashral/aspect-ratio

Adds 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

example.tsx
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

Ratios

ratio is width over height: 16 / 9, 4 / 3, 1, 3 / 4.

16 / 9
4 / 3
1 / 1
3 / 4

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).

PropTypeDefault
rationumber

Width over height: 4 / 3, 1 for a square, 3 / 4 for a portrait.

16 / 9
classNamestring

The box's classes, e.g. a radius or a background. An <img> or <video> without classes fills it, cropped to fit.

—
wrapperClassNamestring

The 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.