# Aspect ratio

> Box that holds a fixed width-to-height ratio and clips its child, which is stretched and cropped to cover it.

- Kind: ui · Category: Layout and media · Interactivity: none
- Install: `npx shadcn@latest add @haze/aspect-ratio`
- File: `src/components/haze/ui/aspect-ratio.astro`
- npm dependencies: none
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/aspect-ratio

## Use when
- Reserving space for an image or video so the page does not jump while it loads
- A grid of thumbnails that must all share one shape
- An iframe embed that should keep 16:9 at any width

## Avoid when
- Text content, whose height must follow its length
- An image that must never be cropped; give it width and height attributes and let it size itself

## Alternatives
- [placeholder](https://hazeui.com/ui/placeholder.md)
- [skeleton](https://hazeui.com/ui/skeleton.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| ratio | `"16/9" \| "4/3" \| "1/1" \| "21/9" \| "3/4"` | "16/9" | no | Width to height ratio. Each value maps to a full Tailwind aspect class. |
| rounded | `boolean` | true | no | Rounds the corners with rounded-xl. The box clips its child either way. |
| class | `string` |  | no | Extra classes on the box. |
| default slot | `slot` |  | yes | One image, video, iframe or placeholder. Direct children get size-full and object-cover. |

## Accessibility
- The wrapper adds no role and no name; the media inside carries the alt text, the iframe title or the placeholder label.
- object-cover crops the edges, so keep the subject of an image away from the border or the crop can hide it.
- A video inside keeps its own controls; the box only clips it and does not intercept pointer or keyboard input.

## Agent prompts

### Install and use

Install the Haze UI component "Aspect ratio" with `npx shadcn@latest add @haze/aspect-ratio`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import AspectRatio from "@/components/haze/ui/aspect-ratio.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/aspect-ratio.md

### Customize with real content

Adapt the Haze aspect-ratio ui for our product. Replace the demo values of these props with our real content: ratio, rounded, class, default slot. Keep the layout, the heading level rules and these accessibility details: The wrapper adds no role and no name; the media inside carries the alt text, the iframe title or the placeholder label. object-cover crops the edges, so keep the subject of an image away from the border or the crop can hide it. A video inside keeps its own controls; the box only clips it and does not intercept pointer or keyboard input. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using aspect-ratio, check it fits: use it when reserving space for an image or video so the page does not jump while it loads; a grid of thumbnails that must all share one shape; an iframe embed that should keep 16:9 at any width. Avoid it when text content, whose height must follow its length; an image that must never be cropped; give it width and height attributes and let it size itself. Alternatives: @haze/placeholder, @haze/skeleton.

## Example requests that should lead here
- Put the hero image in a 16:9 box with rounded corners
- Make a row of square thumbnails with the aspect ratio component

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";

interface Props {
  ratio?: "16/9" | "4/3" | "1/1" | "21/9" | "3/4";
  /** Rounds the corners (the box always clips its child). */
  rounded?: boolean;
  class?: string;
}

const { ratio = "16/9", rounded = true, class: className } = Astro.props;
const ratios = {
  "16/9": "aspect-video",
  "4/3": "aspect-[4/3]",
  "1/1": "aspect-square",
  "21/9": "aspect-[21/9]",
  "3/4": "aspect-[3/4]",
};
---

<div class={cn("w-full overflow-hidden [&>*]:size-full [&>*]:object-cover", ratios[ratio], rounded && "rounded-xl", className)}>
  <slot />
</div>
```
