← Components

Utilities

Aspect ratio

Keeps media at a fixed ratio.

Examples and variants

16:9

Resize the window.

16 : 9
Example
function AspectRatioBasic() {
  return (
    <AspectRatio ratio={16 / 9} className="max-w-xs">
      <div className="flex size-full items-center justify-center text-sm text-muted-foreground">16 : 9</div>
    </AspectRatio>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/aspect-ratio.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/aspect-ratio.tsxaspect-ratio
      components/ui/aspect-ratio.tsx
      import * as React from "react"
      import { cn } from "cn"
      
      /* Aspect Ratio: keeps media at a fixed ratio. Images inside fade in on load (.aspect-media). */
      function AspectRatio({ ratio = 16 / 9, className, style, ...props }: React.ComponentProps<"div"> & { ratio?: number }) {
        return (
          <div
            data-slot="aspect-ratio"
            className={cn("relative w-full overflow-hidden rounded-(--radius-control) bg-surface-sunken [&_img]:size-full [&_img]:object-cover", className)}
            style={{ aspectRatio: ratio, ...style }}
            {...props}
          />
        )
      }
      
      export { AspectRatio }
      

Imports

What the examples on this page import:

Imports
import { InfoIcon } from "lucide-react"
import { Alert, AlertTitle } from "@/components/ui/alert"
import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleLabel, CollapsibleTrigger } from "@/components/ui/collapsible"
import { DirectionProvider } from "@/components/ui/direction"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Heading, Text } from "@/components/ui/typography"

Anatomy

  1. 1Frame · Clips the media; --shape-md corners.

Props

PropTypeDefaultDescription
rationumber16 / 9Width ÷ height.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

This utility has no component tokens; it uses semantic tokens directly.

TokenPoints toLightDark

Motion

TriggerWhat happensTokens
NoneStatic.—

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Images inside need alt text.

Code

components/ui/aspect-ratio.tsx
import * as React from "react"
import { cn } from "cn"

/* Aspect Ratio: keeps media at a fixed ratio. Images inside fade in on load (.aspect-media). */
function AspectRatio({ ratio = 16 / 9, className, style, ...props }: React.ComponentProps<"div"> & { ratio?: number }) {
  return (
    <div
      data-slot="aspect-ratio"
      className={cn("relative w-full overflow-hidden rounded-(--radius-control) bg-surface-sunken [&_img]:size-full [&_img]:object-cover", className)}
      style={{ aspectRatio: ratio, ...style }}
      {...props}
    />
  )
}

export { AspectRatio }