Layout · astro-ui · AspectRatio

AspectRatio

Reserves a fixed width/height ratio for media — no layout shift.

AspectRatio wraps a single child (img, video, iframe, or div) in a box with a fixed CSS aspect-ratio and stretches the child to fill it. Drop media in and it covers the box without layout shift — object-fit stays in your hands.

Features

  • CSS aspect-ratio — number (1.78) or string ("16 / 9")
  • Child stretches to fill; object-fit stays consumer-controlled
  • Overflow hidden — no media bleeding past the box
  • Zero JS, no layout shift

Usage

---
import { AspectRatio } from "astro-ui";
---

<AspectRatio ratio="16 / 9">
  <img src="hero.jpg" alt="Hero" class="object-cover" />
</AspectRatio>

Demos

Common ratios

Landscape scene in a 16:9 box
Scenery in a 4:3 box
Square composition
<AspectRatio ratio="16 / 9">
  <img src="…" alt="" class="object-cover" />
</AspectRatio>
<AspectRatio ratio="4 / 3">…</AspectRatio>
<AspectRatio ratio={1}>…</AspectRatio>

Widescreen

Cinematic panorama in a 21:9 box
Desktop viewport in a 16:10 box
<AspectRatio ratio="21 / 9">…</AspectRatio>
<AspectRatio ratio="16 / 10">…</AspectRatio>

Portrait

Vertical video in a 9:16 box
Social post in a 4:5 box
<AspectRatio ratio="9 / 16">…</AspectRatio>
<AspectRatio ratio="4 / 5">…</AspectRatio>

Photo

Classic composition in a 3:2 box
Product shot in a 1:1 box
<AspectRatio ratio="3 / 2">…</AspectRatio>
<AspectRatio ratio={1}>…</AspectRatio>

API

Props

PropTypeDefaultDescription
rationumber | string1CSS aspect-ratio value: number (1.78) or string ("16 / 9").
classstring—Merged via cn(); Tailwind classes welcome.
idstring—Element id.

Slots

SlotDescription
defaultMedia stretched to fill the box.

Accessibility

Implemented accessibility features — semantics, ARIA wiring, and keyboard behavior that ship with the component.

  • Purely presentational wrapper — media keeps its own alt text
  • No role or label of its own; nothing extra for assistive tech

FAQ

Why doesn't my image fill the box?

The child is stretched to 100%×100% — add object-cover (or object-contain) on the img so it fills without distortion.

Changelog

Version history mentioning AspectRatio.

v0.1.0 — 2026-11-11
added
  • Layout (7): Card, AspectRatio, ScrollArea, Separator, Accordion, Collapsible, StickySurface.