Aspect Ratio
Holds a box at a stated proportion of its own width before its content arrives, so a picture, a map or a player moves nothing below it.
import { AspectRatio } from '@slotted/react/aspect-ratio';const WAREHOUSE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";export default function AspectRatioBasic() { return ( <AspectRatio ratio="landscape"> <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} /> </AspectRatio> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { AspectRatio } from '@slotted/react';Reserving room for media
AspectRatio is a box whose height is a stated proportion of its width. The room exists before the first byte of the picture arrives, so a grid of thumbnails does not shift as each request returns. The frame uses the CSS aspect-ratio property and draws nothing of its own. To fix a width or a height in a unit instead of a proportion, size the element directly; to crop a picture to a shape, use Image with fit.
Named ratios
ratio is required, because the proportion is a decision of your page. Five names cover the common cases: square (1:1), landscape (4:3), portrait (3:4), video (16:9) and wide (21:9). The name is written to data-ratio, so the DOM says which proportion was chosen. Each frame below holds a card so its edge is visible.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Card, CardBody } from '@slotted/react/card';import { Grid } from '@slotted/react/grid';import { Text } from '@slotted/react/typography';const RATIOS = [ { name: 'square', label: 'square, 1:1' }, { name: 'landscape', label: 'landscape, 4:3' }, { name: 'portrait', label: 'portrait, 3:4' }, { name: 'video', label: 'video, 16:9' }, { name: 'wide', label: 'wide, 21:9' },] as const;export default function AspectRatioRatios() { return ( <Grid columns={3} gap={4} minItemWidth="xs"> {RATIOS.map((ratio) => ( <AspectRatio key={ratio.name} ratio={ratio.name}> <Card> <CardBody> <Text size="sm">{ratio.label}</Text> </CardBody> </Card> </AspectRatio> ))} </Grid> );}Custom ratios
A pair of numbers or a single number sets any other proportion. A pair is written exactly as given: [2, 1] becomes 2 / 1. A number is its own quotient: 1.85 stays 1.85. Both set data-ratio="custom" and write the value to the --slotted-aspect-ratio custom property on the element.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Card, CardBody } from '@slotted/react/card';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function AspectRatioCustom() { return ( <Stack gap={4}> <AspectRatio ratio={[2, 1]}> <Card> <CardBody> <Text size="sm">A pair, [2, 1]: written as 2 / 1.</Text> </CardBody> </Card> </AspectRatio> <AspectRatio ratio={1.85}> <Card> <CardBody> <Text size="sm">A number, 1.85: written as its quotient.</Text> </CardBody> </Card> </AspectRatio> </Stack> );}Fitting media whole
The frame reserves room and never crops. An img or a video child is sized to the frame and fitted with object-fit: contain, so the whole picture is shown, with space around it when its proportion differs from the frame’s. Here a card outlines the frame: a 4:3 picture sits inside a 16:9 frame, then inside a square one.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Card } from '@slotted/react/card';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const WAREHOUSE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";export default function AspectRatioMedia() { return ( <Stack gap={4}> <Text size="sm" tone="muted"> A 4:3 picture in a video frame </Text> <Card> <AspectRatio ratio="video"> <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} /> </AspectRatio> </Card> <Text size="sm" tone="muted"> The same picture in a square frame </Text> <Card> <AspectRatio ratio="square"> <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} /> </AspectRatio> </Card> </Stack> );}Content taller than the ratio
The ratio is a floor, not a ceiling. Content that has a height of its own, such as text or a card, keeps its minimum size, so a frame that cannot hold it grows to fit. Nothing is clipped and nothing overlaps the content below. The first frame holds one line and keeps its 16:9 shape; the second holds a paragraph taller than that shape and grows to the paragraph. A replaced child, such as an image, does not grow the frame.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Card, CardBody } from '@slotted/react/card';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function AspectRatioGrowth() { return ( <Stack gap={4}> <AspectRatio ratio="video"> <Card> <CardBody> <Text>Pickup window: 08:00 to 10:00.</Text> </CardBody> </Card> </AspectRatio> <AspectRatio ratio="video"> <Card> <CardBody> <Text> Harbor Line moved the pickup for orders 20418 to 20431 from the morning window to the afternoon window because the vessel from Singapore docked six hours late. The Rotterdam warehouse holds the pallets on dock 4 until the carrier confirms the new slot. Customers with a delivery promise before Thursday receive an email with the new date, and the support team sees the same date on the order. Orders paid by transfer are released after the payment clears. </Text> </CardBody> </Card> </AspectRatio> </Stack> );}Layers in one cell
The frame is a grid of one cell, and every child takes that cell. A second child stacks on the first with no positioning of your own. Here an Inline fills the cell and centers a badge over the picture of a camera that stopped.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';const WAREHOUSE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";export default function AspectRatioOverlay() { return ( <AspectRatio ratio="landscape"> <img alt="Last picture from the Rotterdam warehouse camera" src={WAREHOUSE} /> <Inline justify="center"> <Badge tone="danger" variant="solid"> Camera offline </Badge> </Inline> </AspectRatio> );}Picture with a caption
The frame has no role and announces nothing. The content carries its own name: an img its alt, an iframe its title. A picture that needs a visible caption goes in a figure that wraps the frame, with the figcaption after it. The alt describes the picture and does not repeat the caption.
import { AspectRatio } from '@slotted/react/aspect-ratio';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const WAREHOUSE = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 120'%3E%3Crect width='160' height='120' fill='%231e3a5f'/%3E%3Crect x='20' y='50' width='120' height='50' fill='%2393c5fd'/%3E%3Cpath d='M10 50 80 20l70 30Z' fill='%23dbeafe'/%3E%3Crect x='65' y='70' width='30' height='30' fill='%231e3a5f'/%3E%3C/svg%3E";export default function AspectRatioFigure() { return ( <Stack gap={3} render={(props) => <figure {...props} style={{ margin: 0 }} />}> <AspectRatio ratio="landscape"> <img alt="Illustration of the Rotterdam warehouse" src={WAREHOUSE} /> </AspectRatio> <Text render={(props) => <figcaption {...props} />} size="sm" tone="muted"> Rotterdam warehouse, dock 4, March inventory count </Text> </Stack> );}Accessibility
- There is no aspect-ratio pattern in the WAI-ARIA Authoring Practices, and the frame needs none: it is a div with no role, and what it holds carries its own semantics — an img its alt, an iframe its title, a p its text.
- The frame never clips. Content taller than the ratio grows the box (the CSS sizing specification keeps a content-based minimum on a box with aspect-ratio), so a translation that runs longer than the design is read whole rather than cut at the ratio or laid over the frame below.
- A replaced child is fitted with object-fit: contain, so a picture is shown entire; a picture that has to cover the frame is a crop, and Image decides that with its fit prop.
- The frame moves nothing on load: the box is its stated proportion before the first byte of a picture arrives, which is what keeps a grid of thumbnails from shifting as each request returns.
API
aspect-ratio
Props
ratio'square' | 'landscape' | 'portrait' | 'video' | 'wide' | number | [number, number]- Required. The proportion the box holds before anything is in it: a name, a number or a pair of numbers.
childrenReactNode- What the frame holds. Each child takes the one cell; a replaced child is fitted whole.
Composition
render(props) => ReactNode- Substitutes the root element — a figure, an li — and hands it every data attribute and the token.
refRef<HTMLDivElement>- Reaches the frame; the props type is the div’s own, extended.
--slotted-aspect-ratiocustom property- On an instanceWhat a custom ratio writes on the element, as w / h. A theme retunes a named ratio in the stylesheet instead.
Parts
| Part | Element | Required | Purpose |
|---|---|---|---|
root | div | Yes | The frame: a grid of one cell that is a stated proportion of its own width. Every child takes the cell and stretches to it; a replaced child is fitted whole, never cropped; content taller than the ratio grows the frame. |
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every aspect ratio inside it.
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-aspect-ratio | 1 / 1 |