---
title: "Aspect Ratio · Base UI — StyleX Blocks"
description: "Locks media, maps, and artwork frames to design-token aspect ratios with rounded clipping."
url: "https://stylexblocks.com/components/base-ui/aspect-ratio/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Aspect Ratio

Locks media, maps, and artwork frames to design-token aspect ratios with rounded clipping.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/aspect-ratio.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/aspect-ratio.md)
- Category: Layout
- HTML page: https://stylexblocks.com/components/base-ui/aspect-ratio/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add aspect-ratio
```

Equivalent: `npx shadcn add @stylexblocks/aspect-ratio`. Registry item JSON: https://stylexblocks.com/r/base-ui/aspect-ratio.json

## Usage

```tsx
import { AspectRatio } from "#components/ui/base-ui/aspect-ratio.tsx";

export function Example() {
  return (
    <AspectRatio ratio="wide" rounded="lg">
      <img src="/cover.jpg" alt="Studio preview" />
    </AspectRatio>
  );
}
```

## Source

`components/ui/base-ui/aspect-ratio.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { createComponent } from "#lib/base-ui/create-component.tsx";
import { defineRecipe } from "#lib/recipe-props.ts";
import { aspectRatios } from "@stylexblocks/ui/tokens/aspect-ratios.stylex.ts";
import { radii } from "@stylexblocks/ui/tokens/radii.stylex.ts";

const aspectRatioStyles = stylex.create({
  base: {
    position: "relative",
    width: "100%",
    overflow: "hidden",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
  },
});

const aspectRatioValues = stylex.create({
  square: { aspectRatio: aspectRatios.square },
  landscape: { aspectRatio: aspectRatios.landscape },
  portrait: { aspectRatio: aspectRatios.portrait },
  wide: { aspectRatio: aspectRatios.wide },
  ultrawide: { aspectRatio: aspectRatios.ultrawide },
  golden: { aspectRatio: aspectRatios.golden },
  "1/1": { aspectRatio: aspectRatios.square },
  "4/3": { aspectRatio: aspectRatios.landscape },
  "3/4": { aspectRatio: aspectRatios.portrait },
  "16/9": { aspectRatio: aspectRatios.wide },
  "21/9": { aspectRatio: "21 / 9" },
  "3/2": { aspectRatio: "3 / 2" },
  "4/5": { aspectRatio: "4 / 5" },
});

const aspectRatioRadii = stylex.create({
  none: { borderRadius: 0 },
  sm: { borderRadius: radii.sm },
  md: { borderRadius: radii.md },
  lg: { borderRadius: radii.lg },
  full: { borderRadius: radii.full },
});

const aspectRatioRecipe = defineRecipe({
  styles: aspectRatioStyles,
  axes: {
    ratio: aspectRatioValues,
    rounded: aspectRatioRadii,
  },
  defaultVariants: {
    ratio: "wide",
    rounded: "md",
  },
});

/**
 * AspectRatio — locks media, maps, and artwork frames to a design-token
 * aspect ratio (`square`, `landscape`, `portrait`, `wide`, `ultrawide`,
 * `golden`, `3/2`, `4/5`). Composition via `render`.
 */
export const AspectRatio = createComponent("div", aspectRatioRecipe, "AspectRatio");
export type AspectRatioProps = React.ComponentProps<typeof AspectRatio>;
```
