Aspect ratio

A box whose height follows its width at a fixed ratio, so media never shifts the layout.

<tp-aspect-ratio>
Aspect ratio
Default density Compact density Restart from the initial state Show the copyable source
A winding road through green hills at sunset

Usage

Set ratio to width divided by height, such as 1.7778 for 16:9 or 1 for a square. The box takes its width from the container and derives its height, so content can’t stretch it. Always pass a ratio; the 16:9 default exists only for compatibility.

Invalid property assignments throw RangeError and preserve the previous valid ratio. Invalid attribute values also report the validation error instead of silently clamping geometry.

Ratios

Common ratios
Default density Compact density Restart from the initial state Show the copyable source
21:9
1:1
9:16

Reserving the box before media loads prevents layout shift in feeds, galleries and video embeds. Width comes from the surrounding layout; the ratio is the only size input.

Container width

One ratio, three widths
Default density Compact density Restart from the initial state Show the copyable source
A winding road through green hills at sunset
Sidebar · 160 px
A winding road through green hills at sunset
Card · 240 px
A winding road through green hills at sunset
Feature · 320 px

The same 16:9 box keeps its shape in a sidebar, a card and a feature slot. Give it a width, or let the container shrink it, and the height follows.

Fit

Cover and contain
Default density Compact density Restart from the initial state Show the copyable source
Mountain lake at dawn, cropped to fill Mountain lake at dawn, shown whole

fit sets how images and video fill the box, like object-fit: cover crops, contain letterboxes and fill stretches. It never changes the ratio. The box adds no image semantics, so keep alt on the image itself.

API Reference

Every element also accepts motion-policy.

Attributes

AttributePropertyTypeDefaultDescriptionratiorationumber—The width divided by the height, such as 1.7778 for 16:9 or 1 for a square; always set it.fitfit'fill' | 'contain' | 'cover' | 'none' | 'scale-down''fill'How an image or a video fills the box: cover crops, contain letterboxes, fill stretches.

Slots

SlotDescriptiondefaultThe content that fills the box, usually an image or a video.

CSS parts

PartDescription::part(aspect-ratio-box)The box that holds the ratio; also exposed as root.::part(aspect-ratio-box-content)The layer the content fills, the full size of the box.

Components, widgets and patterns that use this one.