Aspect Ratio

Web/Mobile

Displays content within a desired ratio.

۱۶∶۹

Installation

pnpm dlx cubix-ui@latest add aspect-ratio --base aria

Usage

Import
import { AspectRatio } from "@/components/cubix/aspect-ratio"
Example
<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.

Structure
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

PropTypeDefaultDescription
rationumber-The width-to-height ratio of the container (e.g. 16 / 9 or 1).
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-Content displayed inside the ratio box. Position it with absolute inset-0 to fill the frame.