Aspect ratio
Box that holds a fixed width-to-height ratio and clips its child, which is stretched and cropped to cover it.
pnpm dlx shadcn@latest add @haze/aspect-ratio---
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>When to use
- 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
When not to use
- 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
Installation
Add the Haze registry to components.json once, then install Aspect ratio with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/aspect-rationpx shadcn@latest add @haze/aspect-ratioyarn dlx shadcn@latest add @haze/aspect-ratiobunx --bun shadcn@latest add @haze/aspect-ratioFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| ratio | "16/9" | "4/3" | "1/1" | "21/9" | "3/4" | "16/9" | Width to height ratio. Each value maps to a full Tailwind aspect class. |
| rounded | boolean | true | Rounds the corners with rounded-xl. The box clips its child either way. |
| class | string | – | Extra classes on the box. |
| default slot* | slot | – | 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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.