Aspect ratio
A box that keeps one shape at any width: a video, a map, a cover image. It holds its height before the media loads, so nothing below it jumps. For a picture that should keep its own shape, a plain image with a width and height does the same.
Install
npx shadcn@latest add https://thrumui.dev/r/aspect-ratio.jsonAdds components/ui/aspect-ratio.tsx. Nothing else in your project changes.
import { AspectRatio } from "@/components/ui/aspect-ratio";Props
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | string | Default 16 / 9 | Width divided by height. A number such as 1.5, or a CSS ratio string such as "16/9" or "4 / 3". |
children | ReactNode | – | The media. An img, video, iframe, canvas, svg or picture placed directly inside fills the box. Other children are placed by their own classes; the box is relative, so absolute children sit inside it. |
className | string | – | Extra classes, merged last. The box fills the width it is given; set a width or a max-width here, and rounded-lg with bg-muted for a placeholder. |
style | CSSProperties | – | Merged after the ratio, so an aspectRatio here wins. |
...props | ComponentProps<"div"> | – | Every other div attribute is passed through. |
Motion and sound
Motion
- The box does not move. It has its final height on the first frame, before any media loads.
Sound
Silent. This component makes no sound.
Accessibility
- The box is a plain div and adds no role. Name the media inside it: alt text on an image, a title on an iframe, a caption or a label on a video.
- Content taller than the box is cut off at its edge, not allowed to stretch it. Keep text in a caption outside the box, or short enough to fit at the smallest width.
- It is drawn by the CSS aspect-ratio property, so it needs no script and works before the page has loaded.
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type AspectRatioProps = ComponentProps<"div"> & {
/** Width divided by height: a number (`16 / 9`, `1.5`) or a CSS ratio string (`"16/9"`, `"4 / 3"`). */
ratio?: number | string;
};
/**
* A box that keeps one shape at any width: a video, a map, a cover image. It is the CSS `aspect-ratio` property, so it
* needs no script and holds its height before the media loads. An image, video, iframe, canvas, svg or picture placed
* directly inside fills the box; give an image `object-cover` to crop it rather than stretch it.
*/
export function AspectRatio({ ratio = 16 / 9, className, style, ...props }: AspectRatioProps) {
return (
<div
data-slot="aspect-ratio"
// overflow-hidden also turns off the automatic minimum height, so tall content cannot stretch the box.
className={cn(
"relative w-full overflow-hidden [&>canvas]:size-full [&>iframe]:size-full [&>img]:size-full [&>picture]:size-full [&>svg]:size-full [&>video]:size-full",
className,
)}
style={{ aspectRatio: String(ratio), ...style }}
{...props}
/>
);
}Files and dependencies
- Adds
components/ui/aspect-ratio.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.