shadcn.io is not affiliated with official shadcn/ui
Aspect Ratio examples · Standard
1:1 Aspect Ratio (Square) for React
A 1:1 square frame for social media posts, profile images, and balanced grid layouts.
Squares work everywhere. This example sets ratio to 1 on AspectRatio, so the frame is exactly as tall as it is wide and reserves its height before the content loads. The width comes from the parent, capped here with max-w-md, and the height always matches it. Use it for feed posts, profile pictures, product thumbnails, and album art, where neither orientation should dominate.
Preview
Installation
FAQ
How is the square set?
ratio is 1 on AspectRatio, width divided by height. The height matches the width. The centered label reads "1:1".
Where does the frame's width come from?
The parent is w-full max-w-md. The height follows that width. Give the wrapper a width, because a ratio with no width can collapse. max-w-md caps the width, and the height matches it.
How does the label fill the frame?
The child is a div with size-full, flex items-center justify-center, rounded-md, and bg-muted. The span is text-sm text-muted-foreground. Those classes are on the child, so the frame's shape stays on ratio.
How does ratio become a height?
The ratio is stored in the --ratio custom property, and the aspect-ratio utility reads that variable.
How do I put an image or a video in this frame?
Put one child in AspectRatio and make it fill the box. The Aspect Ratio usage places the image absolutely, inset on every side, at full size, with object-cover. This file's child is the muted "1:1" label.
Related
- 4:3 Aspect Ratio (Traditional)A 4:3 traditional frame for classic
- 16:9 Aspect Ratio (Widescreen)A 16:9 widescreen frame for video
- 9:16 Aspect Ratio (Vertical/Mobile)A 9:16 vertical frame for mobile stories
- 3:2 Aspect Ratio (Photography)A 3:2 photography frame for camera
- 2:1 Aspect Ratio (Panoramic)A 2:1 panoramic frame for wide landscape
- 21:9 Aspect Ratio (Ultrawide)A 21:9 ultrawide frame for cinematic