---
title: "Aspect Ratio"
description: "Displays content within a desired ratio."
component: true
---

```tsx
import { AspectRatio } from "~/components/ui/aspect-ratio"

export default function AspectRatioDemo() {
  return (
    <div class="w-full max-w-sm">
      <AspectRatio ratio={16 / 9} class="rounded-lg bg-muted">
        <img
          src="/solid-brand/launch-light.svg"
          alt="Solid logo artwork"
          class="size-full rounded-lg object-cover"
        />
      </AspectRatio>
    </div>
  )
}
```

## Installation

<CodeTabs>

<TabsList>
  <TabsTrigger value="cli">Command</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

```bash
npx solidui-cli@latest add aspect-ratio
```

</TabsContent>

<TabsContent value="manual">

<Steps class="mb-0 pt-2">

<Step>Copy and paste the following code into your project.</Step>

```tsx title="components/ui/aspect-ratio.tsx"
import { type ComponentProps } from "@solidjs/web"
import { omit } from "solid-js"

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

type AspectRatioProps = ComponentProps<"div"> & {
  ratio: number
  class?: string | undefined
}

const AspectRatio = (props: AspectRatioProps) => {
  const others = omit(props, "class", "ratio")
  return (
    <div
      data-slot="aspect-ratio"
      style={{
        "--ratio": props.ratio
      }}
      class={cn("relative aspect-(--ratio) overflow-hidden", props.class)}
      {...others}
    />
  )
}

export { AspectRatio }
```

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</CodeTabs>

## Usage

```tsx
import { AspectRatio } from "~/components/ui/aspect-ratio"
```

```tsx
<AspectRatio ratio={16 / 9}>
  <img
    src="/photo.jpg"
    alt="Mountain landscape"
    class="size-full object-cover"
  />
</AspectRatio>
```

## Square

A square aspect ratio component using the `ratio={1 / 1}` prop. This is useful for displaying images in a square format.

```tsx
import { AspectRatio } from "~/components/ui/aspect-ratio"

export default function AspectRatioSquare() {
  return (
    <div class="w-full max-w-[12rem]">
      <AspectRatio ratio={1 / 1} class="rounded-lg bg-muted">
        <img
          src="/solid-brand/launch-light.svg"
          alt="Solid logo artwork"
          class="size-full rounded-lg object-cover"
        />
      </AspectRatio>
    </div>
  )
}
```

## Portrait

A portrait aspect ratio component using the `ratio={9 / 16}` prop. This is useful for displaying images in a portrait format.

```tsx
import { AspectRatio } from "~/components/ui/aspect-ratio"

export default function AspectRatioPortrait() {
  return (
    <div class="w-full max-w-[10rem]">
      <AspectRatio ratio={9 / 16} class="rounded-lg bg-muted">
        <img
          src="/solid-brand/launch-light.svg"
          alt="Solid logo artwork"
          class="size-full rounded-lg object-cover"
        />
      </AspectRatio>
    </div>
  )
}
```

## API Reference

### AspectRatio

The `AspectRatio` component displays content within a desired ratio.

| Prop    | Type     |
| ------- | -------- |
| `ratio` | `number` |
| `class` | `string` |
