# Aspect Ratio

Reserve a stable ratio for media and embedded content.

`AspectRatio` reserves predictable space while its width changes. Use it for images, videos, embeds, previews, and other content whose proportions should remain stable.

```tsx
import { AspectRatio, Label, Stack, Switch, Text, ToggleGroup } from "@zentara-co/zenos-ui";
import { useState } from "react";

function AspectRatioExample() {
  const ratios = [
    { label: "1:1", value: 1, use: "Avatars and tiles" },
    { label: "4:3", value: 4 / 3, use: "Classic photos and thumbnails" },
    { label: "3:2", value: 3 / 2, use: "Landscape photography" },
    { label: "16:9", value: 16 / 9, use: "Video and product previews" },
    { label: "21:9", value: 21 / 9, use: "Cinematic banners" },
    { label: "4:5", value: 4 / 5, use: "Portrait cards" },
    { label: "2:3", value: 2 / 3, use: "Posters and editorial covers" },
    { label: "9:16", value: 9 / 16, use: "Stories and vertical video" },
  ] as const;
  const [ratioLabel, setRatioLabel] = useState<(typeof ratios)[number]["label"]>("4:3");
  const [crop, setCrop] = useState(true);
  const selectedRatio = ratios.find(({ label }) => label === ratioLabel) ?? ratios[1];
  const frameWidth = `min(100%, calc(28rem * ${selectedRatio.value}))`;

  return (
    <Stack gap="4" className="w-full">
      <div className="flex w-full flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
        <ToggleGroup
          aria-label="Photo aspect ratio"
          value={[ratioLabel]}
          onValueChange={(value) => {
            const nextRatio = ratios.find(({ label }) => label === value[0]);
            if (nextRatio) setRatioLabel(nextRatio.label);
          }}
          className="flex-wrap rounded-lg bg-background-neutral-default p-1"
        >
          {ratios.map(({ label }) => (
            <ToggleGroup.Item key={label} value={label} className="min-w-12 px-3">
              {label}
            </ToggleGroup.Item>
          ))}
        </ToggleGroup>

        <Label className="flex shrink-0 items-center gap-2 self-end lg:self-auto">
          <Switch checked={crop} onCheckedChange={setCrop} />
          <span className="type-body-m">Crop to fill</span>
        </Label>
      </div>

      <div className="grid min-h-[30rem] w-full place-items-center overflow-hidden py-3 sm:py-5">
        <AspectRatio
          ratio={selectedRatio.value}
          className="max-w-full rounded-xl border border-border-default bg-background-neutral-card shadow-raised transition-[width,aspect-ratio] duration-500 ease-out motion-reduce:transition-none"
          style={{ width: frameWidth }}
        >
          <img
            src="https://images.unsplash.com/photo-1669604120699-eeba651ac235?auto=format&fit=crop&w=1600&q=80"
            alt="Turquoise Moraine Lake surrounded by evergreen forest and mountains"
            className={`size-full transition-transform duration-500 ease-out motion-reduce:transition-none ${crop ? "scale-[1.01] object-cover" : "object-contain"}`}
          />
        </AspectRatio>
      </div>

      <div className="flex w-full flex-wrap items-center justify-between gap-2">
        <code className="rounded-md bg-background-neutral-default px-2 py-1 type-code text-text-default">
          aspect-[{selectedRatio.label.replace(":", "/")}]
        </code>
        <Text size="s" color="subtle">
          {selectedRatio.use}
        </Text>
      </div>
    </Stack>
  );
}
```

Switch between common landscape, square, and portrait ratios to see one image reframed. The frame transitions between dimensions while the image remains centered. Turn off **Crop to fill** to use `object-contain` and preserve the complete image with letterboxing.

## Import

```tsx
import { AspectRatio } from "@zentara-co/zenos-ui";
```

## Media

AspectRatio controls the outer geometry. Size the media to fill that box and choose `object-cover` or `object-contain` according to whether cropping is acceptable.

```tsx
import { AspectRatio, Grid, Stack, Text } from "@zentara-co/zenos-ui";

function AspectRatioMediaExample() {
  return (
    <Grid columns={{ base: 1, md: 2 }} gap="4" className="w-full">
      <Stack gap="2">
        <AspectRatio
          ratio={16 / 9}
          className="rounded-lg border border-border-default bg-background-neutral-card"
        >
          <img
            src="https://images.unsplash.com/photo-1768160864911-90f7837453a2?auto=format&fit=crop&w=1600&q=80"
            alt="Snow-capped Southern Alps reflected in Lake Matheson"
            className="size-full object-cover"
          />
        </AspectRatio>
        <Text size="s" color="subtle">
          16:9 landscape cover
        </Text>
      </Stack>
      <Stack gap="2">
        <AspectRatio
          ratio={4 / 3}
          className="rounded-lg border border-border-default bg-background-neutral-card"
        >
          <img
            src="https://images.unsplash.com/photo-1761600577035-c9cb717c7059?auto=format&fit=crop&w=1600&q=80"
            alt="Minimal modern building with strong geometric shadows"
            className="size-full object-cover"
          />
        </AspectRatio>
        <Text size="s" color="subtle">
          4:3 editorial thumbnail
        </Text>
      </Stack>
    </Grid>
  );
}
```

## Semantic content

Use `as` when the ratio box itself has semantic meaning. Native props and the ref follow the selected element.

```tsx
import { AspectRatio, Badge } from "@zentara-co/zenos-ui";

function AspectRatioSemanticExample() {
  return (
    <AspectRatio
      as="figure"
      ratio={16 / 9}
      aria-labelledby="release-preview-caption"
      className="relative w-full max-w-xl rounded-xl border border-border-default bg-background-neutral-card"
    >
      <img
        src="https://images.unsplash.com/photo-1669604120699-eeba651ac235?auto=format&fit=crop&w=1600&q=80"
        alt="Turquoise Moraine Lake surrounded by evergreen forest and mountains"
        className="size-full object-cover"
      />
      <Badge variant="default" color="information" className="absolute start-4 top-4 shadow-raised">
        Travel journal
      </Badge>
      <figcaption
        id="release-preview-caption"
        className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/80 to-transparent px-5 pb-4 pt-12 text-white"
      >
        <span className="type-heading-h5">Morning at Moraine Lake</span>
      </figcaption>
    </AspectRatio>
  );
}
```

## Choosing a ratio

Pass a positive number using width divided by height:

| Ratio  | Value    | Typical use                      |
| ------ | -------- | -------------------------------- |
| `1:1`  | `1`      | Avatars, square tiles, and icons |
| `4:3`  | `4 / 3`  | Classic photos and thumbnails    |
| `3:2`  | `3 / 2`  | Landscape photography            |
| `16:9` | `16 / 9` | Video and product previews       |
| `21:9` | `21 / 9` | Cinematic banners and hero media |
| `4:5`  | `4 / 5`  | Portrait cards and social media  |
| `2:3`  | `2 / 3`  | Posters and editorial covers     |
| `9:16` | `9 / 16` | Stories and vertical video       |

Choose the ratio from the destination layout rather than the source file. A stable frame prevents surrounding content from shifting while media loads, and one source image can be reframed for several placements.

The component does not crop or resize its children. It only sets `aspect-ratio`, full width, and overflow clipping. Use `object-cover` when filling the frame matters more than preserving every edge, or `object-contain` when the complete image must remain visible. Add decorative surface, border, radius, and motion styles through `className`.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| as | ElementType | div | Rendered semantic element. |
| ratio | number | 1 | CSS aspect-ratio value, such as 16 / 9. |
| native props | React.HTMLAttributes<HTMLElement> | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

AspectRatio does not change child semantics. Images still need meaningful alternative text when informative and an empty `alt` when decorative. Videos and embeds retain their own caption, title, and keyboard requirements. Avoid placing essential text where media cropping can hide it.

Source: https://zenos-ui.pages.dev/layout/aspect-ratio/