GooseUI

Components

Aspect Ratio

New

A wrapper with a fixed aspect ratio for pictures, video and embeds, so the page does not jump while they load. It is the CSS aspect-ratio property: a server component, no JavaScript. Direct img, video and iframe children fill the box.

Preview

Mountains at dawn

Installation

npx shadcn@latest add @gooseui/aspect-ratio
pnpm dlx shadcn@latest add @gooseui/aspect-ratio
yarn dlx shadcn@latest add @gooseui/aspect-ratio
bunx --bun shadcn@latest add @gooseui/aspect-ratio

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio"

export default function Example() {
  return (
    <AspectRatio ratio={16 / 9} className="rounded-lg">
      <img src="/photo.webp" alt="Mountains at dawn" />
    </AspectRatio>
  )
}

Examples

Common ratios

Pass a number or a string such as 16/9, 4/3 or 1.

Mountains at dawn
16/9
Mountains at dawn
4/3
Mountains at dawn
1
<AspectRatio ratio="16/9">…</AspectRatio>
<AspectRatio ratio="4/3">…</AspectRatio>
<AspectRatio ratio={1}>…</AspectRatio>

Fit: cover and contain

cover crops the picture to fill the box, contain shows all of it with free space around.

Mountains at dawn
cover
Mountains at dawn
contain
<AspectRatio ratio="16/9" fit="contain" className="bg-muted">
  <img src="/photo.webp" alt="" />
</AspectRatio>

Props

PropTypeDefaultDescription
rationumber | string1Width divided by height: a number or a string like 16/9
fit"cover" | "contain" | "fill""cover"How a direct img or video child fits the box
classNamestring-Additional CSS classes