The CSS aspect-ratio property
The CSS aspect-ratio property gives a box a preferred shape, so one dimension can be derived from the other. Its main practical use is reserving space for media before the file arrives.
At a glance
- Property
- aspect-ratio
- Syntax
- aspect-ratio: 16 / 9; or aspect-ratio: 1.778;
- Initial value
- auto
- Applies to
- All elements except inline boxes and internal table and ruby boxes
- Overridden when
- Both width and height resolve to definite values
- Main use
- Reserving layout space so content does not shift when media loads
In detail
The property states a preferred ratio between a box's width and its height. Given one definite dimension, the browser derives the other, so a container with a width of one hundred percent and a ratio of sixteen over nine gets a height without any calculation in your stylesheet and without the padding trick that used to be required.
It is a preference rather than a command. If both the width and the height resolve to definite values, those win and the ratio is ignored, which surprises people who set a height elsewhere in a component and then wonder why the ratio does nothing. Minimum and maximum constraints also override it, so a ratio box inside a flex container with a min-height will not always be the shape you asked for.
For replaced elements such as an image or a video the value auto is not a no-op: it means use the intrinsic ratio of the media. Combining auto with an explicit ratio, written as auto and then the fraction, means use the intrinsic ratio if the media has one and fall back to the stated ratio if it does not, which is the safest declaration for a slot that might receive anything.
The layout shift argument is the strongest reason to use it. Width and height attributes on an image already give the browser an intrinsic ratio to reserve space with, and the CSS property covers everything else: an embedded player, a canvas, a placeholder that will be filled by script. Reserving the box before the content arrives is what stops the page jumping.
Examples
.media {
aspect-ratio: 16 / 9;
width: 100%;
overflow: hidden;
}
.media > * {
width: 100%;
height: 100%;
object-fit: cover;
}img {
aspect-ratio: auto 4 / 3;
width: 100%;
height: auto;
}Uses the image's own ratio when it has one, and 4:3 while it is still loading.
.card {
aspect-ratio: 1 / 1;
width: 300px;
height: 200px; /* both definite, so the ratio does nothing */
}Things that catch people out
- Setting both a width and a height cancels the ratio silently. There is no warning, the box is just the wrong shape.
- A ratio box inside a flex or grid item can be stretched by the alignment defaults. Set align-self to start if the shape must hold.
- Width and height attributes on an image are not obsolete. They give the browser a ratio before any CSS loads, which the property alone cannot do.
Try it: Aspect Ratio Calculator
Enter a width and a height, or a ratio and one dimension, and it solves the missing one, simplifies the ratio, counts the megapixels and gives you a copyable CSS value.
Aspect Ratio Calculator
LOCAL · Browser-OnlyCalculate and convert aspect ratios for images, videos, and screens
1920 x 1080px · 16:9 · 1.7778:1
Common Aspect Ratios Reference
| Ratio | Decimal | Common Use |
|---|---|---|
| 16:9 | 1.778 | Widescreen TV, YouTube, monitors |
| 4:3 | 1.333 | Classic TV, iPad, presentations |
| 1:1 | 1.000 | Instagram square, profile pictures |
| 21:9 | 2.333 | Ultrawide monitors, cinema |
| 9:16 | 0.563 | Phone vertical, TikTok, Reels |
| 3:2 | 1.500 | 35mm photography, MacBook |
| 2:3 | 0.667 | Portrait photography, posters |
| 5:4 | 1.250 | Large format photography, older monitors |
| 32:9 | 3.556 | Super ultrawide monitors |
| 2.39:1 | 2.390 | Anamorphic cinema, widescreen films |
Related Image and canvas sizes
All image and canvas sizes →What is an aspect ratio?
Aspect ratio is width divided by height, and it says nothing about resolution or file size. How to simplify one, read it as a decimal, and use it correctly.
Letterboxing versus cropping
What happens when an image does not match its frame: bars that keep everything, or a crop that discards the edges. How object-fit chooses between them.
16:9 aspect ratio
Where the 16:9 ratio came from, the pixel dimensions that hit it exactly, what it suits, and the places where a widescreen frame is the wrong shape.
DPI versus pixel dimensions
Why a raster file only stores pixels, what the DPI number in its metadata does and does not change, and how to work out a print size from a pixel count.
YouTube 1080p video size: 1920 by 1080
Why 1920 by 1080 is the standard full HD upload frame, what the player overlays cover, and how the thumbnail is a separate asset at the same shape.
YouTube 4K video size: 3840 by 2160
The 4K UHD frame at 3840 by 2160, why it is exactly four times the pixel count of 1080p, and what uploading above the watched resolution actually buys you.
Tools for this
- Aspect Ratio Calculator Free online aspect ratio calculator - calculate and convert aspect ratios for images, videos, and screens