Aspect Ratio
Maintains a consistent width-to-height ratio for images, videos, and embeds, preventing layout shifts when pages load or resize.
406 B
•Updated on August 21, 2026
Installation
Bash
pnpm dlx shadcn@latest add @spaceui/primitives-aspect-ratioUsage
Code
import { AspectRatio } from '@/components/ui/aspect-ratio'Code
<AspectRatio ratio={16 / 9}>
<imgalt="Image" className="h-full w-full object-cover" />
</AspectRatio>API Reference
AspectRatio Props
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | - | The desired aspect ratio (e.g. 16 / 9, 1) |
className | string | - | Custom class name applied to the container |
...props | ComponentProps<'div'> | - | All native HTML div attributes |
Examples
4:3 standard aspect ratio
1:1 square aspect ratio
21:9 ultrawide aspect ratio
9:16 portrait aspect ratio
3:2 classic photography aspect ratio
4:5 social media portrait aspect ratio
16:10 computer monitor aspect ratio