Skip to content
Slotted

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.

Illustration of the Rotterdam warehouse

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.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { 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.

square, 1:1
landscape, 4:3
portrait, 3:4
video, 16:9
wide, 21:9

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.

A pair, [2, 1]: written as 2 / 1.
A number, 1.85: written as its quotient.

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.

A 4:3 picture in a video frame
Illustration of the Rotterdam warehouse
The same picture in a square frame
Illustration of the Rotterdam warehouse

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.

Pickup window: 08:00 to 10:00.
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.

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.

Last picture from the Rotterdam warehouse camera
Camera offline

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.

Illustration of the Rotterdam warehouse
Rotterdam warehouse, dock 4, March inventory count

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

PartElementRequiredPurpose
rootdivYesThe 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

TokenDefault
--slotted-aspect-ratio1 / 1