Utilities
Aspect ratio
Keeps media at a fixed ratio.
Examples and variants
16:9
Resize the window.
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.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/aspect-ratio.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy 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.tsximport * 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:
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
- 1Frame · Clips the media; --shape-md corners.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
ratio | number | 16 / 9 | Width ÷ 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.
| Token | Points to | Light | Dark |
|---|
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| None | Static. | — |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Images inside need alt text.
Code
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 }