Components

Aspect Ratio

Displays content within a desired ratio.

Photo by Drew Beamer

Installation

CLI

npx shadcn@latest add https://exawizards.com/exabase/design/registry/aspect-ratio.json

Manual

Copy and paste the following code into your project.

import { cn } from "@/lib/utils"

function AspectRatio({
  ratio,
  className,
  ...props
}: React.ComponentProps<"div"> & { ratio: number }) {
  return (
    <div
      data-slot="aspect-ratio"
      style={
        {
          "--ratio": ratio,
        } as React.CSSProperties
      }
      className={cn("relative aspect-(--ratio)", className)}
      {...props}
    />
  )
}

export { AspectRatio }

Usage

import Image from "next/image"
import { AspectRatio } from "@/components/ui/aspect-ratio"
<div className="w-[450px]">
  <AspectRatio ratio={16 / 9}>
    <Image src="..." alt="Image" className="rounded-md object-cover" />
  </AspectRatio>
</div>

API

AspectRatio

The AspectRatio component displays content within a desired ratio.

PropTypeDefaultDescription
rationumber-The aspect ratio of the content. Required

On this page