Skip to content

shadcn.io is not affiliated with official shadcn/ui

Shadcn Aspect Ratio for React

Wrapper that maintains aspect ratio for content.

Landscape

Installation

bunx --bun shadcn@latest add https://kit.dev/r/radix/aspect-ratio.json

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio";
<AspectRatio className="w-full max-w-sm rounded-lg bg-muted" ratio={16 / 9}>
  <img
    alt="Photo"
    className="absolute inset-0 size-full rounded-lg object-cover grayscale dark:brightness-20"
    height={900}
    src="https://avatar.vercel.sh/shadcn1"
    width={1600}
  />
</AspectRatio>

Demos

Square

A square aspect ratio component using the ratio={1 / 1} prop. This is useful for displaying images in a square format.

Portrait

A portrait aspect ratio component using the ratio={9 / 16} prop. This is useful for displaying images in a portrait format.

Rtl

Right-to-left aspect ratio. See the RTL configuration guide for document direction.

API Reference

AspectRatio

PropType
ratio?number
AttributeType