CSS Reference ยท Box Model and Sizing
The aspect-ratio property
Reference for the CSS aspect-ratio property: ratio syntax, auto, how it combines with width and height, examples for cards and video, and browser support.
The aspect-ratio property gives a box a preferred width-to-height ratio. When one dimension is known and the other is auto, the browser computes the missing one from the ratio. It replaces the old "padding-top percentage" trick for responsive thumbnails, video frames and square avatars.
.thumb { width: 100%; aspect-ratio: 16 / 9; }
Try it
The second box is taller than a square because min-height: auto lets content push the box open. The third box has a definite height, so the ratio no longer applies.
Syntax
aspect-ratio: auto || <ratio> /* examples */ aspect-ratio: 16 / 9; aspect-ratio: 1; aspect-ratio: auto 4 / 3;
- Initial value:
auto - Applies to: all elements except inline boxes and internal ruby or table boxes
- Inherited: no
- Animatable: yes, as a number (interpolated between ratios)
- Computed value: the keyword
autoplus a<ratio>if specified
Values
| Value | Description |
|---|---|
auto |
Replaced elements (images, video, canvas) use their intrinsic ratio. Other boxes have no preferred ratio. |
<ratio> |
Width divided by height, written 16 / 9 or as a single number such as 1.5. A lone number 1 means a square. The spaces around / are optional. |
auto <ratio> |
Uses the intrinsic ratio of a replaced element if it has one, and falls back to the given ratio otherwise. Handy for images that have not loaded yet. |
Usage notes
- Needs one free dimension. The ratio only fills in an
autosize. If bothwidthandheightare definite, the ratio is ignored. - Content can win. For non-replaced boxes the ratio sets the preferred size; if the content does not fit, the box grows. Set
min-height: 0plusoverflowto enforce a strict shape, or useoverflow: hidden. - Images. Setting
aspect-ratiotogether withobject-fit: covercrops an image to a consistent frame. Addingwidthandheightattributes on<img>also lets the browser reserve space and avoid layout shift. - Embeds. An
<iframe>has no intrinsic ratio, soaspect-ratio: 16 / 9; width: 100%; height: autois the usual way to size a video embed. - Flex and grid. Items can use
aspect-ratiotoo, but stretch alignment may override the computed size; setalign-self: startif needed. - Invalid ratios. A zero or negative ratio, or
0 / 1, is invalid and the declaration is dropped.
Browser support
Baseline since 2021 and supported in all current major browsers; only very old browsers need the padding-top fallback.
Related
object-fitโ how an image fills its framewidthandheightpadding-topโ the old percentage trick- Lessons: Width and Height, Object-fit