Skip to content
All components
Primitives

Aspect ratio

Reserve a fixed width-to-height ratio for media so the layout does not shift while images, videos or embeds load.

Reserves space so media cannot shift the page as it loads.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add aspect-ratio

Then import it from @/components/ui/aspect-ratio. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Widescreen

Reserve a 16 by 9 area for wide media. The height follows the available width.

"use client"

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

export function AspectRatioWidescreen() {
  return (
    <div className="flex w-full max-w-md flex-col gap-8">
      <div className="w-full">
        <AspectRatio ratio={16 / 9}>
          <div className="flex size-full items-center justify-center rounded-16 border border-soft-200 bg-weak-50 text-label-sm text-sub-600">
            16 / 9
          </div>
        </AspectRatio>
      </div>
    </div>
  )
}

Square

Reserve a square area for a thumbnail or logo. The wrapper width decides the size; the ratio keeps it square.

"use client"

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

export function AspectRatioSquare() {
  return (
    <div className="flex w-full max-w-md flex-col gap-8">
      <div className="w-full max-w-40">
        <AspectRatio ratio={1}>
          <div className="flex size-full items-center justify-center rounded-12 border border-soft-200 bg-weak-50 text-label-sm text-sub-600">
            1 / 1
          </div>
        </AspectRatio>
      </div>
    </div>
  )
}

Video preview

Reserve a video area inside a figure with a caption. The preview has its own accessible name; AspectRatio adds no media semantics.

"use client"

import { PlayIcon } from "lucide-react"

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

export function AspectRatioVideoPreview() {
  return (
    <div className="flex w-full max-w-md flex-col gap-8">
      <figure className="flex w-full flex-col gap-2">
        <AspectRatio ratio={16 / 9}>
          <div
            role="img"
            aria-label="Video preview: setting up a new workspace"
            className="flex size-full items-center justify-center rounded-16 border border-soft-200 bg-weak-50 text-sub-600"
          >
            <PlayIcon aria-hidden="true" className="size-8" />
          </div>
        </AspectRatio>
        <figcaption className="text-paragraph-sm text-sub-600">
          Setting up a new workspace, 3 minutes.
        </figcaption>
      </figure>
    </div>
  )
}

Thumbnail grid

Keep consistent thumbnails in a responsive grid. Two columns on narrow screens and four from the small breakpoint, all at 4 by 3.

"use client"

import { ImageIcon } from "lucide-react"

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

const thumbnails = ["Front view", "Side view", "Detail", "In use"]

export function AspectRatioThumbnailGrid() {
  return (
    <div className="flex w-full max-w-md flex-col gap-8">
      <ul aria-label="Product photos" className="grid w-full grid-cols-2 gap-3 sm:grid-cols-4">
        {thumbnails.map((label) => (
          <li key={label}>
            <AspectRatio ratio={4 / 3}>
              <div
                role="img"
                aria-label={label}
                className="flex size-full items-center justify-center rounded-10 border border-soft-200 bg-weak-50 text-sub-600"
              >
                <ImageIcon aria-hidden="true" className="size-5" />
              </div>
            </AspectRatio>
          </li>
        ))}
      </ul>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

AspectRatio

Reserve a consistent ratio for media content.

AspectRatio props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
Inherited props (15)
AspectRatio inherited props
PropTypeDefaultDescription
rationumber | undefined—Width divided by height for the aspect-ratio container. Supply a positive ratio appropriate to the media.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLDivElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLDivElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLDivElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLDivElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLDivElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
refReact.Ref<HTMLDivElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

Accessibility & usage

From registry/ui/aspect-ratio.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • Images, video players, maps and embeds whose intrinsic size is unknown until they load.
  • Grids of thumbnails or cards that must line up at every width.
  • Media placeholders and skeletons that should match the final media shape.

When not to use

  • Text containers whose height must grow with their content; a fixed ratio will clip or overflow text.
  • Images that already set width and height attributes and display at their intrinsic ratio.
  • Avatars and icons, which have their own fixed sizes.

Anatomy

AspectRatio
Radix AspectRatio root. Takes ratio (width divided by height, for example 16 / 9) and fills the width of its parent; its single child is stretched to the reserved box.

Keyboard & interaction

  • Passive: no focus, keyboard or pointer behavior of its own.
  • Interactive media inside it (video controls, embedded maps) keep their own keyboard behavior and must stay reachable.
  • The reserved box exists before the media loads, which prevents layout shift and keeps focused elements below from moving.

Responsive behavior

  • The width comes from the parent; constrain the parent (max-w-*, grid columns) and the height follows the ratio at every viewport.
  • Use responsive grid columns around ratio boxes, for example two columns on phones and four on wider screens.
  • Pick a ratio that suits narrow screens: very wide ratios (21:9) become very short on phones; consider a taller ratio below a breakpoint by rendering a different ratio value.
  • Reflow at 320 CSS pixels: never give the wrapper a fixed width wider than the viewport.

Content guidelines

  • Give every informative image a concise alt text; decorative images get alt="".
  • Videos need captions; describe the video in a caption (figcaption) when context helps.
  • Choose ratios that match the source media (16:9 video, 4:3 or 1:1 photos) to avoid aggressive cropping of important content.

Alternatives

  • Avatar — Profile pictures with fallback initials.
  • Skeleton — A loading placeholder; combine both to reserve the final media shape.
  • Card — A surface that groups media with text and actions.

Resources