Get Started
Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Card
- Checkbox
- Collapsible
- Combobox
- Context Menu
- Date Picker (Lite)
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP (Lite)
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
import { AspectRatio } from "@/components/ui/aspect-ratio"
export default function AspectRatioDemo() {
return (
<AspectRatio ratio={16 / 9} class="w-full max-w-sm rounded-lg bg-muted">
<img
src="https://avatar.vercel.sh/shadcn1"
alt="Photo"
class="absolute inset-0 size-full rounded-lg object-cover grayscale dark:brightness-20"
/>
</AspectRatio>
)
}Installation
pnpm dlx shadcnui-hono-jsx@latest add aspect-rationpx shadcnui-hono-jsx@latest add aspect-ratioyarn dlx shadcnui-hono-jsx@latest add aspect-ratiobunx shadcnui-hono-jsx@latest add aspect-ratioUsage
import { AspectRatio } from "@/components/ui/aspect-ratio"<AspectRatio ratio={16 / 9}>
<Image src="..." alt="Image" class="rounded-md object-cover" />
</AspectRatio>Square
A square aspect ratio component using the ratio={1 / 1} prop. This is useful for displaying images in a square format.
import { AspectRatio } from "@/components/ui/aspect-ratio"
export function AspectRatioSquare() {
return (
<AspectRatio ratio={1 / 1} class="w-full max-w-[12rem] rounded-lg bg-muted">
<img
src="https://avatar.vercel.sh/shadcn1"
alt="Photo"
class="absolute inset-0 size-full rounded-lg object-cover grayscale dark:brightness-20"
/>
</AspectRatio>
)
}Portrait
A portrait aspect ratio component using the ratio={9 / 16} prop. This is useful for displaying images in a portrait format.
import { AspectRatio } from "@/components/ui/aspect-ratio"
export function AspectRatioPortrait() {
return (
<AspectRatio
ratio={9 / 16}
class="w-full max-w-[10rem] rounded-lg bg-muted"
>
<img
src="https://avatar.vercel.sh/shadcn1"
alt="Photo"
class="absolute inset-0 size-full rounded-lg object-cover grayscale dark:brightness-20"
/>
</AspectRatio>
)
}RTL
To enable RTL support in shadcn/ui, see the RTL configuration guide.
// This example sets dir and uses useTranslation because this site is not RTL.
// In an RTL app you won't need them; see the RTL guide.
import {
type Translations,
useTranslation,
} from "@/components/language-selector"
import { AspectRatio } from "@/components/ui/aspect-ratio"
const translations: Translations = {
en: {
dir: "ltr",
values: {
caption: "Beautiful landscape",
},
},
ar: {
dir: "rtl",
values: {
caption: "منظر طبيعي جميل",
},
},
he: {
dir: "rtl",
values: {
caption: "נוף יפה",
},
},
}
export function AspectRatioRtl() {
const { dir, t } = useTranslation(translations, "ar")
return (
<figure class="w-full max-w-sm" dir={dir}>
<AspectRatio ratio={16 / 9} class="rounded-lg bg-muted">
<img
src="https://avatar.vercel.sh/shadcn1"
alt="Photo"
class="absolute inset-0 size-full rounded-lg object-cover grayscale dark:brightness-20"
/>
</AspectRatio>
<figcaption class="mt-2 text-center text-sm text-muted-foreground">
{t.caption}
</figcaption>
</figure>
)
}Notes
- Accepts
classinstead ofclassName. - Upstream documentation: shadcn/ui Aspect Ratio. See also Differences from shadcn/ui.
API Reference
AspectRatio
The AspectRatio component displays content within a desired ratio.
| Prop | Type | Default | Required |
|---|---|---|---|
ratio | number | - | Yes |
className | string | - | No |
For more information, see the Base UI documentation.