View fulldotdev/ui on GitHub

Aspect Ratio

Displays content within a desired ratio.

Installation

npx shadcn@latest add @fulldev/aspect-ratio

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio"
<AspectRatio ratio={16 / 9}>
  <img src="/image.jpg" alt="Landscape" class="size-full object-cover" />
</AspectRatio>

The child controls its own crop. Use object-cover to fill the area.

Square

A square aspect ratio using ratio={1 / 1}.

Portrait

A portrait aspect ratio using ratio={9 / 16}.

RTL

API Reference

Source: src/components/ui/aspect-ratio

PropTypeDefaultDescription
rationumberrequiredWidth divided by height, such as 16 / 9.

class, style, and other div attributes are forwarded. String and object style values are merged with the internal --ratio variable.