How to Set CSS Aspect Ratio on Any Element
The CSS aspect-ratio property maintains an element's proportions regardless of its width. Set aspect-ratio: 16 / 9 and the element is always 16:9 โ whether it's 300px wide or 1200px wide. It replaces the decades-old padding-bottom percentage hack with a single, readable property.
The aspect-ratio Property
The syntax is straightforward:
.video-container {
aspect-ratio: 16 / 9;
width: 100%;
}
.square-card {
aspect-ratio: 1 / 1;
width: 300px;
}The element calculates its height from the width and ratio. If both width and height are set, the ratio is ignored. And by default the ratio is a minimum, not a fixed size: if the content is taller than the ratio allows, the box grows to fit it (see the measurements below). Add min-height: 0 with overflow: auto if the shape must hold.
Replacing the Padding-Bottom Hack
Before aspect-ratio, developers used padding-bottom percentages to maintain ratios:
/* Old way */
.video-wrapper { position: relative; padding-bottom: 56.25%; height: 0; }
.video-wrapper iframe { position: absolute; inset: 0; width: 100%; height: 100%; }
/* New way */
.video-wrapper { aspect-ratio: 16 / 9; }
.video-wrapper iframe { width: 100%; height: 100%; }The new way is cleaner, more readable, and doesn't require absolute positioning tricks.
Preventing Layout Shift with Images
You do not need CSS for this. Put width and height attributes on the <img> and keep height: auto in your CSS: since 2021 every major browser's built-in stylesheet turns those attributes into aspect-ratio: auto 1600 / 900, so the correct space is reserved before the image downloads. The aspect-ratio: attr(width) / attr(height) rule that circulates online is unnecessary โ the browser already does exactly that. Use an explicit ratio with object-fit: cover only when you want every image cropped to the same shape.
Worked Example: What Chromium Does
We tested in Chromium 141 with a 400px-wide container and an image URL that never finishes loading (script and results in our test folder, October 13, 2026):
| Case | Height |
|---|---|
<img width="1600" height="900">, CSS width:100%; height:auto, before load | 225 px โ space reserved (computed aspect-ratio: auto 1600 / 900) |
| Same image without the attributes | 0 px โ the page will jump when it loads |
aspect-ratio: 16/9, short content | 225 px |
| Same box, content longer than fits | 324 px โ grew to fit the text |
Same, plus min-height: 0; overflow: auto | 225 px โ ratio held, content scrolls |
aspect-ratio: 16/9 with height: 100px | 100 px โ explicit height wins |
Common Aspect Ratios
16:9 โ video, YouTube embeds, widescreen displays. 4:3 โ traditional TV, iPad. 1:1 โ Instagram posts, avatars, product thumbnails. 9:16 โ vertical video (Stories, Reels). 21:9 โ ultrawide/cinematic. 3:2 โ DSLR photos. 4:5 โ Instagram portrait. Use the calculator to find the ratio for any pixel dimensions.