Skip to content
Thrum

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.json

Adds components/ui/aspect-ratio.tsx. Nothing else in your project changes.

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

Props

PropTypeDefaultDescription
rationumber | stringDefault 16 / 9Width divided by height. A number such as 1.5, or a CSS ratio string such as "16/9" or "4 / 3".
childrenReactNode–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.
classNamestring–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.
styleCSSProperties–Merged after the ratio, so an aspectRatio here wins.
...propsComponentProps<"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

components/ui/aspect-ratio.tsx
"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. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor