<!-- index: /slotted/llms.txt -->
# aspect-ratio

`data-ui="aspect-ratio"` · Box that keeps its content at a fixed ratio.

## Import

```tsx
import { AspectRatio } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';

const WAREHOUSE =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";

export default function AspectRatioBasic() {
  return (
    <AspectRatio ratio="landscape">
      <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} />
    </AspectRatio>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div | yes |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `ratio` | `'square' \| 'landscape' \| 'portrait' \| 'video' \| 'wide' \| number \| [number, number]` | — | AspectRatio | Required. The proportion the box holds before anything is in it: a name, a number or a pair of numbers. |
| `children` | `ReactNode` | — | AspectRatio | What the frame holds. Each child takes the one cell; a replaced child is fitted whole. |
| `render` | `(props) => ReactNode` | — | AspectRatio | Substitutes the root element — a figure, an li — and hands it every data attribute and the token. |
| `ref` | `Ref<HTMLDivElement>` | — | AspectRatio | Reaches the frame; the props type is the div’s own, extended. |
| `--slotted-aspect-ratio` | `custom property` | — | an instance | What a custom ratio writes on the element, as w / h. A theme retunes a named ratio in the stylesheet instead. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `ratio` | `square`, `landscape`, `portrait`, `video`, `wide`, `custom` | — |

## Rules

- There is no aspect-ratio pattern in the WAI-ARIA Authoring Practices, and the frame needs none: it is a div with no role, and what it holds carries its own semantics — an img its alt, an iframe its title, a p its text.
- The frame never clips. Content taller than the ratio grows the box (the CSS sizing specification keeps a content-based minimum on a box with aspect-ratio), so a translation that runs longer than the design is read whole rather than cut at the ratio or laid over the frame below.
- A replaced child is fitted with object-fit: contain, so a picture is shown entire; a picture that has to cover the frame is a crop, and Image decides that with its fit prop.
- The frame moves nothing on load: the box is its stated proportion before the first byte of a picture arrives, which is what keeps a grid of thumbnails from shifting as each request returns.

## Tokens

`--slotted-aspect-ratio`

## Examples

### Playground

One frame around a survey photograph, driven by the controls: switch the ratio and the frame changes shape while the picture is shown whole inside it.

Excerpt — not a complete module

```tsx
<AspectRatio ratio="video">
  <img alt="The fjord lookout at site 12" src={site.photo} />
</AspectRatio>;
```

### Five ratios a product decides once

square, landscape (4:3), portrait (3:4), video (16:9) and wide (21:9), each named for what it holds rather than for its numbers, so a template says "thumbnails are landscape" and the DOM says which one was taken.

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Card, CardBody } from '@slotted/react/card';
import { Grid } from '@slotted/react/grid';
import { Text } from '@slotted/react/typography';

const RATIOS = [
  { name: 'square', label: 'square, 1:1' },
  { name: 'landscape', label: 'landscape, 4:3' },
  { name: 'portrait', label: 'portrait, 3:4' },
  { name: 'video', label: 'video, 16:9' },
  { name: 'wide', label: 'wide, 21:9' },
] as const;

export default function AspectRatioRatios() {
  return (
    <Grid columns={3} gap={4} minItemWidth="xs">
      {RATIOS.map((ratio) => (
        <AspectRatio key={ratio.name} ratio={ratio.name}>
          <Card>
            <CardBody>
              <Text size="sm">{ratio.label}</Text>
            </CardBody>
          </Card>
        </AspectRatio>
      ))}
    </Grid>
  );
}
```

### Any other proportion, as a number or a pair

A pair is written into the stylesheet exactly as given — 2 / 1 stays 2 / 1 — where a number is its quotient. Both mark the frame data-ratio="custom" and put the value on the instance.

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Card, CardBody } from '@slotted/react/card';
import { Stack } from '@slotted/react/stack';
import { Text } from '@slotted/react/typography';

export default function AspectRatioCustom() {
  return (
    <Stack gap={4}>
      <AspectRatio ratio={[2, 1]}>
        <Card>
          <CardBody>
            <Text size="sm">A pair, [2, 1]: written as 2 / 1.</Text>
          </CardBody>
        </Card>
      </AspectRatio>
      <AspectRatio ratio={1.85}>
        <Card>
          <CardBody>
            <Text size="sm">A number, 1.85: written as its quotient.</Text>
          </CardBody>
        </Card>
      </AspectRatio>
    </Stack>
  );
}
```

### Replaced content is fitted whole

A 4:3 photograph in a 16:9 frame is shown entire with the frame either side; a wide one in a square frame is letterboxed the same way. The frame reserves and never crops — the crop is Image’s decision, taken with fit.

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Card } from '@slotted/react/card';
import { Stack } from '@slotted/react/stack';
import { Text } from '@slotted/react/typography';

const WAREHOUSE =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";

export default function AspectRatioMedia() {
  return (
    <Stack gap={4}>
      <Text size="sm" tone="muted">
        A 4:3 picture in a video frame
      </Text>
      <Card>
        <AspectRatio ratio="video">
          <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} />
        </AspectRatio>
      </Card>
      <Text size="sm" tone="muted">
        The same picture in a square frame
      </Text>
      <Card>
        <AspectRatio ratio="square">
          <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} />
        </AspectRatio>
      </Card>
    </Stack>
  );
}
```

### Content taller than the ratio grows the frame

Two lines fit a 16:9 frame at this width and the frame keeps its ratio; a longer paragraph does not fit, and the frame is exactly as tall as the paragraph. Nothing is clipped and nothing lands on the frame below: the ratio is a floor, not a ceiling.

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Card, CardBody } from '@slotted/react/card';
import { Stack } from '@slotted/react/stack';
import { Text } from '@slotted/react/typography';

export default function AspectRatioGrowth() {
  return (
    <Stack gap={4}>
      <AspectRatio ratio="video">
        <Card>
          <CardBody>
            <Text>Pickup window: 08:00 to 10:00.</Text>
          </CardBody>
        </Card>
      </AspectRatio>
      <AspectRatio ratio="video">
        <Card>
          <CardBody>
            <Text>
              Harbor Line moved the pickup for orders 20418 to 20431 from the morning window to the
              afternoon window because the vessel from Singapore docked six hours late. The
              Rotterdam warehouse holds the pallets on dock 4 until the carrier confirms the new
              slot. Customers with a delivery promise before Thursday receive an email with the new
              date, and the support team sees the same date on the order. Orders paid by transfer
              are released after the payment clears.
            </Text>
          </CardBody>
        </Card>
      </AspectRatio>
    </Stack>
  );
}
```

### Two children stack; an Image composes the frame

Every child takes the one cell, so a caption sits over a picture with nothing positioned by hand. An Image given a ratio renders this same frame inside its own root — the second tile is what Image writes.

Excerpt — not a complete module

```tsx
<AspectRatio ratio="landscape">
  <img alt="" src={site.photo} />
  <Text tone="inverse">{site.name}</Text>
</AspectRatio>;
```

### The frame has no role

A frame is a div and announces nothing; the content is yours and carries its own name. A picture with a caption is a figure around the frame, with the figcaption after it, and the caption is not repeated in the alt.

Excerpt — not a complete module

```tsx
<figure>
  <AspectRatio ratio="landscape">
    <img alt="The fjord lookout at site 12 from the cliff path" src={site.photo} />
  </AspectRatio>
  <figcaption>Site 12 — fjord lookout, March survey</figcaption>
</figure>;
```

### figure.tsx

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Stack } from '@slotted/react/stack';
import { Text } from '@slotted/react/typography';

const WAREHOUSE =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";

export default function AspectRatioFigure() {
  return (
    <Stack gap={3} render={(props) => <figure {...props} style={{ margin: 0 }} />}>
      <AspectRatio ratio="landscape">
        <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} />
      </AspectRatio>
      <Text render={(props) => <figcaption {...props} />} size="sm" tone="muted">
        Rotterdam warehouse, dock 4, March inventory count
      </Text>
    </Stack>
  );
}
```

### overlay.tsx

Example

```tsx
import { AspectRatio } from '@slotted/react/aspect-ratio';
import { Badge } from '@slotted/react/badge';
import { Inline } from '@slotted/react/inline';

const WAREHOUSE =
  "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";

export default function AspectRatioOverlay() {
  return (
    <AspectRatio ratio="landscape">
      <img alt="Last picture from the Rotterdam warehouse camera" src={WAREHOUSE} />
      <Inline justify="center">
        <Badge tone="danger" variant="solid">
          Camera offline
        </Badge>
      </Inline>
    </AspectRatio>
  );
}
```
