Aspect Ratio
Web/MobileDisplays content within a desired ratio.
Installation
pnpm dlx cubix-ui@latest add aspect-ratioUsage
import { AspectRatio } from "@/components/cubix/aspect-ratio"
<AspectRatio ratio={16 / 9} className="w-full max-w-sm rounded-lg border bg-muted">
<div className="absolute inset-0 flex items-center justify-center text-caption text-muted-foreground">
۱۶∶۹
</div>
</AspectRatio>
Composition
Pass a numeric ratio (width divided by height). Position the inner shape with absolute inset-0 so it fills the box. Overflow is clipped.
AspectRatio (ratio)
└── children (absolute inset-0)Accessibility
Aspect Ratio is a layout container, not an interactive control. Give meaningful images an alt, and mark decorative fills with aria-hidden.
Examples
Square
Use ratio={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.
With media
Layer a decorative fill and a caption so media stays inside the ratio box.
API Reference
Note: The root renders a data-slot="aspect-ratio" attribute for targeting in tests and parent selectors. Position the inner shape with absolute inset-0 so it fills the ratio box. Use the same props on Base UI, React Aria, and Radix.
AspectRatio
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | number | - | The width-to-height ratio of the container (e.g. 16 / 9 or 1). |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
| children | React.ReactNode | - | Content displayed inside the ratio box. Position it with absolute inset-0 to fill the frame. |