Aspect Ratio
Constrains content to a desired width-to-height ratio.
Made by shadcnPowered by
Installation
Loading component...
When to use
Use AspectRatio when content (images, video embeds, maps, placeholders) must keep a fixed proportion regardless of the container width. The component reserves the correct height up front, which prevents layout shift while media loads.
Usage
import { AspectRatio } from "@/components/redpanda-ui/aspect-ratio"<AspectRatio ratio={16 / 9} className="overflow-hidden rounded-lg bg-muted">
<img
alt="Photo of a redpanda"
className="h-full w-full object-cover"
src="/images/redpanda.jpg"
/>
</AspectRatio>Anatomy
The AspectRatio component is a plain container styled with the CSS aspect-ratio property:
AspectRatio
├── Base element (div, relative positioning)
├── --ratio custom property - set from the ratio prop
├── aspect-(--ratio) - CSS aspect-ratio driven sizing
└── children - fill the box (use h-full w-full or absolute inset-0)The container is relative, so children can use absolute inset-0 (or h-full w-full) to fill it. Add overflow-hidden plus a radius on the AspectRatio itself when clipping media to rounded corners.
Examples
Common Ratios
Loading component...
<AspectRatio ratio={1}>{/* square */}</AspectRatio>
<AspectRatio ratio={4 / 3}>{/* standard */}</AspectRatio>
<AspectRatio ratio={16 / 9}>{/* widescreen */}</AspectRatio>Props
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | — | Width-to-height ratio of the container (e.g. 16 / 9) |
className | string | — | Additional CSS classes |
testId | string | — | Value applied to data-testid for testing |
All other props are forwarded to the underlying div element.
Accessibility
- AspectRatio is purely presentational and adds no semantics or keyboard interactions.
- Provide meaningful
alttext on images rendered inside the container.
Credits
- We take our inspiration from Shadcn UI for the aspect ratio component.
Recent changes
- patchv1.2.0Pin shipped dependency floors to the version we develop against. Registry items now declare ranges like `^5.1.9` (the actual installed version) instead of collapsing to `^5.0.0`, so consumers start on the known-tested baseline while caret semantics still allow any compatible release within the same major.#133
- minorv1.1.0Theme docs refresh, readability pass on semantic foregrounds, and consumer-facing Base UI regression fixes.#121
- minorv1.0.0Post-Base-UI polish. Public API unchanged.#116
- majorv1.0.0Migrate every Radix-based primitive to `@base-ui/react@^1.4.0` (Base UI).#114
- minorv0.3.0Add theme-provider component to the registry with documentation and tests. Includes playground type improvements (export RegistryItem, remove as-const boilerplate) and docs site dark mode border color fix.#109
Built by malinskibeniamin. The source code is available on GitHub.