Components

Aspect Ratio

A box that keeps its width-to-height ratio while its width changes. Exactly shadcn's; the hand-made part is what surrounds it: a photograph on a Polaroid, a print taped at its corners, a map pinned to the desk.

Install

terminal
npx shadcn@latest add @wobko/aspect-ratio

First time here? Add the registry to components.json first. How to install

Examples

Ratios

The same width, four ratios: 16:9, 4:3, 1:1 and 3:4, each a small print with a white border. Change the width of the container and each box keeps its shape.

A Polaroid

A square picture in a photo-stock Paper with the wide white border at the bottom and a line of handwriting. Two strips of washi hold it to the desk; the caption stays upright while the print lies a little crooked.

Taped at the corners

A 4:3 print with no border, held by four strips of masking tape laid across its corners. The tape is placed with className (absolute, a little outside, turned), like any element.

A photo album

Thumbnails that stay 4:3 whatever the columns do, each with its caption underneath. Resize the window: the grid reflows and every picture keeps its shape.

A real image, cropped to fit

The shadcn demo: a 16:9 box with an <img> inside it, `object-cover` so the picture fills the box and is cropped, never squashed. The same picture in a square shows what gets cut. Swap the data URI for your own photograph.

API reference

AspectRatio

A Radix AspectRatio: a relatively positioned wrapper with padding-bottom set from the ratio, and an absolutely positioned inner div (which receives className, data-slot and your children). It draws nothing: put an img with h-full w-full object-cover in it, or any content, and frame it with Paper and Tape.

PropTypeDefaultDescription
rationumber1Width divided by height: 16 / 9, 4 / 3, 1.

Accessibility

  • Layout only: no role, no name, no keyboard behavior. Give the image inside it real alt text, or an empty alt if it is decoration.
  • The frames in the examples (a Polaroid's white border, tape, a pin) are decoration and hidden from assistive tech; the caption is real text.
  • A ratio box has no minimum height, so it collapses to nothing in a container that has no width: give the parent one.

Source

The files the CLI copies into your project.

Loading the source