# Aspect ratio

Reserve media space at a chosen width-to-height ratio before content loads.

Access: free. Category: Essentials. Kind: primitive.

## When to use

- Use it when a product interface needs reserve media space at a chosen width-to-height ratio before content loads.
- Choose it when the host application can supply the required state and keep business logic outside the presentation component.

## Alternatives

- Choose tidal-button instead when press, loading, success and disabled states is the actual interface need, rather than aspect ratio.

## Setup for agents

Use React 19 and TypeScript. Initialize shadcn for your framework, then merge this exact entry into components.json:

```json
{
  "registries": {
    "@tidal-pro": {
      "url": "https://tidalui.com/r/pro/{name}.json",
      "headers": {
        "Authorization": "Bearer ${TIDALUI_TOKEN}"
      }
    }
  }
}
```

The namespace must be named **@tidal-pro** because transitive Pro dependencies use that namespace. Obtain an agent key from your active Pro account at https://tidalui.com/account. Keep TIDALUI_TOKEN in your local environment, never in components.json, client code, Git or logs.

```sh
export TIDALUI_TOKEN='your-private-agent-key'
npx shadcn@latest add @tidal-pro/waitlist-form
npx shadcn@latest add https://tidalui.com/r/tidal-button.json
```

Free command: `npx shadcn@latest add https://tidalui.com/r/<name>.json`.
Pro command: `npx shadcn@latest add @tidal-pro/<name>`.

Import the base stylesheet and every stylesheet listed on the installed item's metadata. For example:

```tsx
import "./components/ui/tidal.css";
import "./components/ui/hero-section.css";
// Place installed components inside this wrapper:
<div className="tidal-scope" data-theme="dark">...</div>
```

Omit data-theme for the default light theme. Put the attribute on the same tidal-scope element for dark mode. Every component installs the shared free tidal-base dependency. After upgrading, run `npx shadcn@latest add https://tidalui.com/r/tidal-base.json --overwrite` to receive current tokens. Review local modifications before overwriting.

Vite: Use React + TypeScript. Map @/* to ./src/* in paths and Vite resolve.alias. Import tidal.css and each component stylesheet in your application entry.

Next.js 15 App Router: Next.js App Router: import tidal.css and component CSS in app/layout.tsx. Interactive examples belong in a client component with "use client". Map @/* to ./* or ./src/* to match your project.

TypeScript 6: use paths without baseUrl:

```json
{
  "compilerOptions": {
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  }
}
```

Pro registry requests are server/CLI-only. Use the same-origin account interface in a browser; cross-origin Authorization preflights are intentionally unsupported.


## Install Aspect ratio

```sh
npx shadcn@latest add https://tidalui.com/r/aspect-ratio.json
```

Stylesheets: `components/ui/tidal.css`, `components/ui/aspect-ratio.css`.

## Minimal example

```tsx
import"./components/ui/tidal.css";
import"./components/ui/aspect-ratio.css";
import{TidalAspectRatio}from"./components/ui/aspect-ratio";
export default()=><div className="tidal-scope"><TidalAspectRatio/></div>;
```

## Structured props

- `TidalAspectRatio.clip?: boolean | undefined` (default false): Crop overflowing media. Leave false when content needs a visible focus ring.
- `TidalAspectRatio.ratio?: number | undefined` (default 16 / 9): Width divided by height. Invalid or nonpositive values use 16 / 9.

## Complete usage example

[Source](https://tidalui.com/examples/aspect-ratio.tsx) | [Manifest](https://tidalui.com/examples/aspect-ratio.json). Install the listed additional dependencies. Consumer fixture: TypeScript pass; Vite pass. This fixture check is distinct from the actual shadcn install receipt.

```tsx
import "./components/ui/tidal.css";

import { useState } from "react";

import { TidalAspectRatio } from "./components/ui/aspect-ratio";

import "./components/ui/aspect-ratio.css";

function tidalExampleAvatarDataUri(seed: unknown) {
  const text = String(seed).replace(/[^a-z0-9]/gi, "").slice(-2) || "UI";
  const palette = ["#333333", "#555555", "#777777", "#999999", "#bbbbbb"];
  const color = palette[Array.from(text).reduce((sum, character) => sum + character.charCodeAt(0), 0) % palette.length];
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><rect width="96" height="96" rx="48" fill="${color}"/><circle cx="48" cy="38" r="17" fill="white" fill-opacity=".92"/><path d="M18 92c3-22 15-34 30-34s27 12 30 34" fill="white" fill-opacity=".92"/></svg>`;
  return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}

function tidalExampleImageDataUri(label: string) {
  const safeLabel = label.replace(/[<>&]/g, "").slice(0, 28) || "Sample image";
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 675"><defs><linearGradient id="g" x1="0" x2="1" y1="0" y2="1"><stop stop-color="#171717"/><stop offset="1" stop-color="#555555"/></linearGradient></defs><rect width="1200" height="675" fill="url(#g)"/><circle cx="950" cy="100" r="260" fill="white" fill-opacity=".08"/><text x="80" y="570" fill="white" font-family="system-ui,sans-serif" font-size="64" font-weight="700">${safeLabel}</text></svg>`;
  return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}

const tidalExampleStyles = ".tidal-scope .aspect-ratio-demo {\n  width: min(100%, 390px);\n  min-width: 0;\n}\n.tidal-scope .aspect-ratio-demo-options {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 6px;\n  margin-bottom: 16px;\n}\n.tidal-scope .aspect-ratio-demo-options button {\n  min-width: 56px;\n  min-height: 44px;\n  border: 1px solid var(--t-border);\n  border-radius: 9px;\n  background: var(--t-surface);\n  color: var(--t-muted);\n  font: inherit;\n  font-size: 12px;\n  cursor: pointer;\n}\n.tidal-scope .aspect-ratio-demo-options button[aria-pressed=\"true\"] {\n  background: var(--t-ink);\n  color: var(--t-surface);\n  border-color: var(--t-ink);\n}\n.tidal-scope .aspect-ratio-demo-options button:focus-visible {\n  outline: 2px solid var(--t-ink);\n  outline-offset: 3px;\n}\n.tidal-scope .aspect-ratio-demo figure {\n  margin: 0;\n}\n.tidal-scope .aspect-ratio-demo .tidal-aspect-ratio {\n  border-radius: 16px;\n  background: var(--t-soft);\n}\n.tidal-scope .aspect-ratio-demo img {\n  object-position: 50% 38%;\n}\n.tidal-scope .aspect-ratio-demo figcaption {\n  display: flex;\n  justify-content: space-between;\n  flex-wrap: wrap;\n  gap: 8px;\n  margin-top: 12px;\n  color: var(--t-muted);\n  font-size: 12px;\n  line-height: 1.6;\n}";

const ratios = [
  { label: "16:9", value: 16 / 9 },
  { label: "4:3", value: 4 / 3 },
  { label: "1:1", value: 1 },
];

export function AspectRatioDemo() {
  const [ratio, setRatio] = useState(ratios[0]);
  return (
    <div className="aspect-ratio-demo">
      <div
        className="aspect-ratio-demo-options"
        role="group"
        aria-label="Image aspect ratio"
      >
        {ratios.map((option) => (
          <button
            key={option.label}
            type="button"
            aria-pressed={option.label === ratio.label}
            onClick={() => setRatio(option)}
          >
            {option.label}
          </button>
        ))}
      </div>
      <figure>
        <TidalAspectRatio ratio={ratio.value} clip>
          <img
            src={tidalExampleAvatarDataUri("06")}
            alt="Studio portrait of a person"
            width={480}
            height={480}
          />
        </TidalAspectRatio>
        <figcaption>
          <span>Profile cover</span>
          <span>{ratio.label}</span>
        </figcaption>
      </figure>
    </div>
  );
}

export default function AgentExample() {
  return (
    <div className="tidal-scope" data-theme="light" dir="ltr">
      <style>{tidalExampleStyles}</style>
      <AspectRatioDemo />
    </div>
  );
}

```

## Gotchas

- Install the declared registry dependencies and render inside a tidal-scope with the foundation stylesheet loaded.

## Theme and accessibility

{"tokens":["--t-accent","--t-border","--t-border-strong","--t-canvas","--t-danger","--t-display","--t-ease-out","--t-ease-spring","--t-faint","--t-font","--t-ink","--t-line","--t-muted","--t-primary","--t-primary-contrast","--t-radius-card","--t-radius-control","--t-radius-overlay","--t-radius-panel","--t-ring","--t-shadow-lg","--t-shadow-soft","--t-soft","--t-subtle","--t-surface","--t-wash"],"darkMode":true}

{"aria":"Keep visible text and native element semantics intact, and add a consumer label when the surrounding context does not provide one.","notes":"Verify focus visibility, text alternatives, contrast, and reduced motion in the final composition."}

Native div attributes/ref and children (raw root HTML is excluded because the frame owns its content wrapper), ratio (width divided by height, defaults to 16/9; nonfinite or nonpositive values fall back to 16/9), and optional clip=false. Direct image/video/canvas/svg children fill the reserved frame with object-fit:cover; callers can override media fit and position with their own CSS. Captions belong outside the frame. The ratio owns inline aspectRatio; explicit caller width/height/min/max geometry remains their responsibility. clip=true hides overflowing media, while false preserves visible focus rings. No media fetch, load state, semantics, or animation is invented.
