shadcn.io is an independent project built on shadcn/ui
On this page8 sections
Aspect Ratio keeps a box at one shape, its width divided by its height, at whatever width the layout gives it. Put an image, a video or an embedded map inside, and the page holds the right height before the media arrives, so the text under it doesn't jump when it loads.
It only shapes the box. To let people pick the crop of a photo themselves, use an Image Cropper.
Installation
Anatomy

Usage
import { AspectRatio } from "@/components/ui/aspect-ratio";ratio is the width divided by the height, and it defaults to 1, a square. Set the width on the element around the frame, and make the child cover it.
<div className="w-full max-w-sm">
<AspectRatio className="rounded-lg bg-muted" ratio={16 / 9}>
<img
alt="Photo"
className="absolute inset-0 size-full rounded-lg object-cover"
height={900}
src="https://avatar.vercel.sh/shadcn1"
width={1600}
/>
</AspectRatio>
</div>Demos
Square
ratio is 1 and the frame is capped at max-w-[12rem], so it shows a 12rem square, and object-cover crops the picture to fill it. A square suits avatars, album art and product tiles in a grid, where every cell should line up whatever shape the uploads are. Leaving ratio out gives the same square, since 1 is the default.
Portrait
ratio is 9 / 16 in a frame capped at max-w-[10rem], so the box is 10rem wide and about 17.8rem tall, and the picture is cropped to that shape. Use portrait for stories, phone screenshots and posters, which a landscape frame would cut down to a band across the middle.
RTL
A 16 by 9 frame in a figure with dir="rtl" and an Arabic caption centered under it. The width sits on the figure and the frame fills it. Direction changes nothing about the ratio or the picture, which isn't mirrored, so only the caption's text runs right to left. Use it to check a figure in an Arabic or Hebrew article.
FAQ
How do I set a 16:9 aspect ratio?
Pass ratio as the width divided by the height: 16 / 9 for widescreen video, 4 / 3 for a classic photo, 9 / 16 for a phone-shaped portrait. Leave it out and you get 1, a square.
The value is any number, so a ratio from your data works as well. An image stored at 1200 by 800 pixels takes 1200 / 800, which is 1.5, and the frame matches the upload exactly.
How do I make an image fill an Aspect Ratio without stretching it?
Position the image over the whole frame and let object-cover crop it, with absolute inset-0 size-full object-cover as in every demo. The frame is a positioned box, so the image's box matches it exactly, and object-cover scales the picture until it fills that box, trimming what overflows instead of distorting it. Use object-contain to show the whole picture with bands on two sides.
For rounded corners, round the image too, as the demos do, or add overflow-hidden to the frame so it clips whatever sits inside.
Why is my Aspect Ratio the wrong width or zero height?
The frame is a block div that takes its parent's width and gets its height from ratio. In a flex row, or a flex column that centers its items, it is only as wide as its content, and an image positioned with absolute has no width to give it, so the frame shrinks to nothing. Add w-full with a cap such as max-w-sm, or a fixed width like w-64, as every demo here does.
Why is my Aspect Ratio taller than the ratio I set?
A child in normal flow that is taller than the frame stretches it. The CSS aspect-ratio property lets a box grow to fit its content instead of overflowing, so a paragraph, an unpositioned video or an image sized only by its width and height attributes can push the height past the ratio. Position the child with absolute inset-0, or add overflow-hidden or min-h-0 to the AspectRatio, and the frame keeps its shape.
Can an Aspect Ratio hold a YouTube video or a map?
Yes, any element that fills the box works. Give the iframe or video the same absolute inset-0 size-full as the image in the demos, and a frame with ratio set to 16 / 9 keeps the player at that shape at every width. A video also takes object-cover to crop or object-contain to letterbox.
Give an embedded iframe a title that names what it shows, such as the video's name, because that title is what a screen reader announces for the frame.
How do I change an Aspect Ratio at a breakpoint?
ratio takes one number, but the shape is the CSS aspect-ratio property, so Tailwind's aspect classes override it. Add aspect-square md:aspect-video to the AspectRatio, and the frame is square on phones and 16:9 from the md breakpoint up. The class replaces the one the component builds from ratio, which then has no effect.
Does Aspect Ratio stop layout shift on a server-rendered page?
Yes, and that is most of its use. The shape is CSS on the frame itself, so the HTML from the server already has the right height before any script runs or any image loads, and the text below it stays where it is when the picture arrives.
This file is a client component. A React Server Component can still render it and pass the image in as children.
Should I use Aspect Ratio or Tailwind's aspect-video class?
They give the same box. The component is a div with relative and aspect-(--ratio), so your own div with relative aspect-video behaves the same way. The component pays off when the ratio comes from data, like an image's stored width and height, because ratio takes any number without an arbitrary class.
API Reference
AspectRatio
<div> data-slot="aspect-ratio"
ratio?number
Width divided by height, such as 16 / 9.
- Default
1
asChild?boolean
Use the provided child element as the default rendered element, combining their props and behavior.
className?string
Merged with the part's classes.
--ratiostring









