Aspect Ratio

A responsive container component that maintains a fixed aspect ratio, commonly used for media like images and videos.

Notebook and coffee by Giulia Bertelli on Unsplash

Installation

npx scrollx-ui@latest add aspect-ratio

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 Reference

AspectRatio

The root container for the AspectRatio component, which helps to maintain a consistent aspect ratio for its child content, like images or videos.

Props

PropertyTypeDefaultDescription
rationumber16 / 9
Aspect ratio of the container (e.g., 4/3, 1/1). Used in AspectRatio.
childrenReactNoderequired
Content inside the container. Used in AspectRatio and AspectRatioChild.
fillbooleanfalse
Makes the child fill the entire container. Used in AspectRatioChild.
object-coverbooleantrue
Keeps aspect ratio while covering the container. Used in AspectRatioChild.
classNamestring
Optional styling class. Used in AspectRatio and AspectRatioChild.

Built withby Ahdeetai.