# Aspect Ratio

A box that keeps its width-to-height ratio for images, video and embeds.

A box that keeps a width-to-height ratio as its width changes: images, video, maps and embeds. Its width comes from its container; its height follows the ratio and never grows with its content. A direct child (an image, a video, an iframe or any element) fills it; media cover it. No look of its own: the caller rounds or frames it.

Kind: component · Category: Display · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add aspect-ratio
```

It writes the folder `aspect-ratio/` into the project's ui folder (`index.ts`, `aspect-ratio.tsx`, `AspectRatio.vue`, `AspectRatio.svelte`, `aspect-ratio.recipe.ts`). First time: [Installation](/installation.md).

## Usage

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

```vue
import { AspectRatio } from "@/components/ui/aspect-ratio";
```

```svelte
import { AspectRatio } from "$lib/components/ui/aspect-ratio";
```

## Parts

In the DOM the root carries `data-slot="aspect-ratio"` and each part `data-slot="aspect-ratio-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `aspect-ratio` |  | aspect-ratio from --fndn-aspect-ratio; full width of its container, clips what overflows. Its direct children fill it (img, video and iframe with object-fit: cover). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `ratio` | `number` | `1.7777777777777777` | Width divided by height (16 / 9 by default; 1 is a square, 0.75 a portrait 3:4). A value that is not a positive number falls back to 16 / 9. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Slots

| Slot | Content |
| --- | --- |
| `default` | The content that fills the box. |

## Keyboard and accessibility

Notes:

- No role: the box is layout only. Give its image an alt text or its iframe a title.

## Motion

- none

## Examples

### Preview deployment thumbnail

React · `preview-thumbnail.tsx`

```tsx
import { Browsers } from "@phosphor-icons/react";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { StatusPill } from "@/components/ui/status-pill";

export default function PreviewThumbnail() {
  return (
    <div className="flex w-80 max-w-full items-center gap-3">
      <div className="w-36 shrink-0">
        <AspectRatio ratio={16 / 9} className="rounded-control bg-inset">
          <div className="grid place-items-center text-ink-3">
            <Browsers data-slot="icon" weight="bold" className="text-2xl" aria-hidden />
          </div>
        </AspectRatio>
      </div>
      <div className="flex min-w-0 flex-col items-start gap-1 text-sm">
        <p className="font-medium text-ink">web-app</p>
        <p className="max-w-full truncate text-ink-2" title="feat/usage-alerts">feat/usage-alerts</p>
        <StatusPill tone="green" size="sm">Ready</StatusPill>
      </div>
    </div>
  );
}
```

Vue · `preview-thumbnail.vue`

```vue
<script setup lang="ts">
import { PhBrowsers } from "@phosphor-icons/vue";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { StatusPill } from "@/components/ui/status-pill";
</script>

<template>
  <div class="flex w-80 max-w-full items-center gap-3">
    <div class="w-36 shrink-0">
      <AspectRatio :ratio="16 / 9" class="rounded-control bg-inset">
        <div class="grid place-items-center text-ink-3">
          <PhBrowsers data-slot="icon" weight="bold" class="text-2xl" aria-hidden="true" />
        </div>
      </AspectRatio>
    </div>
    <div class="flex min-w-0 flex-col items-start gap-1 text-sm">
      <p class="font-medium text-ink">web-app</p>
      <p class="max-w-full truncate text-ink-2" title="feat/usage-alerts">feat/usage-alerts</p>
      <StatusPill tone="green" size="sm">Ready</StatusPill>
    </div>
  </div>
</template>
```

Svelte · `preview-thumbnail.svelte`

```svelte
<script lang="ts">
  import Browsers from "phosphor-svelte/lib/BrowsersIcon";
  import { AspectRatio } from "$lib/components/ui/aspect-ratio";
  import { StatusPill } from "$lib/components/ui/status-pill";
</script>

<div class="flex w-80 max-w-full items-center gap-3">
  <div class="w-36 shrink-0">
    <AspectRatio ratio={16 / 9} class="rounded-control bg-inset">
      <div class="grid place-items-center text-ink-3">
        <Browsers data-slot="icon" weight="bold" class="text-2xl" aria-hidden="true" />
      </div>
    </AspectRatio>
  </div>
  <div class="flex min-w-0 flex-col items-start gap-1 text-sm">
    <p class="font-medium text-ink">web-app</p>
    <p class="max-w-full truncate text-ink-2" title="feat/usage-alerts">feat/usage-alerts</p>
    <StatusPill tone="green" size="sm">Ready</StatusPill>
  </div>
</div>
```

### Video poster

React · `video-poster.tsx`

```tsx
import { Play } from "@phosphor-icons/react";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { Button } from "@/components/ui/button";

export default function VideoPoster() {
  return (
    <figure className="flex w-80 max-w-full flex-col gap-2">
      <AspectRatio ratio={16 / 9} className="rounded-card bg-[linear-gradient(135deg,var(--fndn-accent),var(--fndn-ink))]">
        <div className="grid place-items-center">
          <Button variant="outline" square aria-label="Play: Preview environments in two minutes" icon={<Play data-slot="icon" weight="fill" />} />
        </div>
      </AspectRatio>
      <figcaption className="text-sm text-ink-2">Preview environments in two minutes · 2:14</figcaption>
    </figure>
  );
}
```

Vue · `video-poster.vue`

```vue
<script setup lang="ts">
import { PhPlay } from "@phosphor-icons/vue";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { Button } from "@/components/ui/button";
</script>

<template>
  <figure class="flex w-80 max-w-full flex-col gap-2">
    <AspectRatio :ratio="16 / 9" class="rounded-card bg-[linear-gradient(135deg,var(--fndn-accent),var(--fndn-ink))]">
      <div class="grid place-items-center">
        <Button variant="outline" square aria-label="Play: Preview environments in two minutes">
          <template #icon><PhPlay data-slot="icon" weight="fill" /></template>
        </Button>
      </div>
    </AspectRatio>
    <figcaption class="text-sm text-ink-2">Preview environments in two minutes · 2:14</figcaption>
  </figure>
</template>
```

Svelte · `video-poster.svelte`

```svelte
<script lang="ts">
  import Play from "phosphor-svelte/lib/PlayIcon";
  import { AspectRatio } from "$lib/components/ui/aspect-ratio";
  import { Button } from "$lib/components/ui/button";
</script>

<figure class="flex w-80 max-w-full flex-col gap-2">
  <AspectRatio ratio={16 / 9} class="rounded-card bg-[linear-gradient(135deg,var(--fndn-accent),var(--fndn-ink))]">
    <div class="grid place-items-center">
      <Button variant="outline" square aria-label="Play: Preview environments in two minutes">
        {#snippet icon()}<Play data-slot="icon" weight="fill" />{/snippet}
      </Button>
    </div>
  </AspectRatio>
  <figcaption class="text-sm text-ink-2">Preview environments in two minutes · 2:14</figcaption>
</figure>
```

### Image slots by format

React · `upload-slots.tsx`

```tsx
import { Image } from "@phosphor-icons/react";
import { AspectRatio } from "@/components/ui/aspect-ratio";

const slots = [
  { ratio: 1, name: "Workspace logo", size: "512 × 512" },
  { ratio: 1.91, name: "Social card", size: "1200 × 630" },
  { ratio: 9 / 16, name: "Phone screenshot", size: "1080 × 1920" },
];

export default function UploadSlots() {
  return (
    <div className="grid w-[400px] max-w-full grid-cols-3 items-end gap-3">
      {slots.map((slot) => (
        <div key={slot.name} className="flex min-w-0 flex-col gap-1.5">
          <AspectRatio ratio={slot.ratio} className="rounded-control bg-inset">
            <div className="grid place-items-center text-ink-3">
              <Image data-slot="icon" weight="bold" className="text-xl" aria-hidden />
            </div>
          </AspectRatio>
          <p className="text-sm text-ink">{slot.name}</p>
          <p className="text-xs text-ink-2 tabular-nums">{slot.size}</p>
        </div>
      ))}
    </div>
  );
}
```

Vue · `upload-slots.vue`

```vue
<script setup lang="ts">
import { PhImage } from "@phosphor-icons/vue";
import { AspectRatio } from "@/components/ui/aspect-ratio";

const slots = [
  { ratio: 1, name: "Workspace logo", size: "512 × 512" },
  { ratio: 1.91, name: "Social card", size: "1200 × 630" },
  { ratio: 9 / 16, name: "Phone screenshot", size: "1080 × 1920" },
];
</script>

<template>
  <div class="grid w-[400px] max-w-full grid-cols-3 items-end gap-3">
    <div v-for="slot in slots" :key="slot.name" class="flex min-w-0 flex-col gap-1.5">
      <AspectRatio :ratio="slot.ratio" class="rounded-control bg-inset">
        <div class="grid place-items-center text-ink-3">
          <PhImage data-slot="icon" weight="bold" class="text-xl" aria-hidden="true" />
        </div>
      </AspectRatio>
      <p class="text-sm text-ink">{{ slot.name }}</p>
      <p class="text-xs text-ink-2 tabular-nums">{{ slot.size }}</p>
    </div>
  </div>
</template>
```

Svelte · `upload-slots.svelte`

```svelte
<script lang="ts">
  import Image from "phosphor-svelte/lib/ImageIcon";
  import { AspectRatio } from "$lib/components/ui/aspect-ratio";

  const slots = [
    { ratio: 1, name: "Workspace logo", size: "512 × 512" },
    { ratio: 1.91, name: "Social card", size: "1200 × 630" },
    { ratio: 9 / 16, name: "Phone screenshot", size: "1080 × 1920" },
  ];
</script>

<div class="grid w-[400px] max-w-full grid-cols-3 items-end gap-3">
  {#each slots as slot (slot.name)}
    <div class="flex min-w-0 flex-col gap-1.5">
      <AspectRatio ratio={slot.ratio} class="rounded-control bg-inset">
        <div class="grid place-items-center text-ink-3">
          <Image data-slot="icon" weight="bold" class="text-xl" aria-hidden="true" />
        </div>
      </AspectRatio>
      <p class="text-sm text-ink">{slot.name}</p>
      <p class="text-xs text-ink-2 tabular-nums">{slot.size}</p>
    </div>
  {/each}
</div>
```

## Recipe

`aspect-ratio.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* aspect-ratio — a box that keeps a width-to-height ratio (--fndn-aspect-ratio, set by the adapter
 * from its ratio prop). Its width is its container's; its height follows the ratio. overflow
 * hidden also drops the automatic minimum size, so content taller than the box never stretches
 * it. Direct children fill the box; media cover it. No look of its own (no radius, no fill): the
 * caller frames it. No motion. */

export const aspectRatio = tv({
  base: [
    "relative w-full overflow-hidden [aspect-ratio:var(--fndn-aspect-ratio,16/9)]",
    "[&>*]:size-full [&>:is(img,video,iframe)]:block [&>:is(img,video)]:object-cover [&>iframe]:border-0",
  ],
});

export type AspectRatioRecipeProps = VariantProps<typeof aspectRatio>;
```
