Aspect Ratio

Displays content within a desired ratio.

Installation

pnpm dlx cubix@latest add aspect-ratio

Usage

Import
import { AspectRatio } from "@/components/cubix/aspect-ratio"
Example
<AspectRatio ratio={16 / 9}>
  <div className="absolute inset-0 rounded-md bg-muted-foreground/25" />
</AspectRatio>

Examples

Square

Use ratio={1 / 1} for a square frame. This is useful for avatars and thumbnails.

Portrait

Use ratio={9 / 16} for a portrait frame. This is useful for stories and mobile previews.

API Reference

Note: Position the inner shape with absolute inset-0 so it fills the ratio box.

AspectRatio

A container that keeps a constant width-to-height ratio.

PropTypeDefaultDescription
rationumber-The width-to-height ratio of the container (e.g. 16 / 9 or 1 / 1).
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-Content displayed inside the ratio box, usually a shape or image.