aspect-ratio
Sets the preferred width-to-height ratio of a box. Replaces the padding-top hack with a single declaration.
Modern approach
Specify the ratio directly — no wrapper element or percentage calculated from the ratio needed.
iframe {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
}Legacy approach
Relied on percentage padding being resolved against the parent’s width, giving a zero-height box space proportional to the ratio. The content had to be absolutely positioned on top, which required an extra wrapper element.
.video {
position: relative;
height: 0;
padding-top: 56.25%; /* 9 ÷ 16 */
}
.video > iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="video">
<iframe src="https://www.youtube.com/embed/..."></iframe>
</div>Things to watch when replacing
- If the content is taller than the ratio allows, the box grows to fit it. To keep the ratio, combine it with
min-height: 0oroverflow: auto. aspect-ratiohas no effect when bothwidthandheightare set to something other thanauto. Leave one of them asauto.- For
<img>and<video>, writing thewidthandheightattributes in HTML lets the browser reserve the ratio before the media loads. The CSS only needsheight: auto, and layout shifts on load are avoided.
Fallback
All five target browsers support it, so you normally don’t need to keep the old technique. Only when you also have to support older browsers, branch with @supports and fall back to the old technique. Because the old technique relies on a wrapper element, keep the .video element around the iframe in the HTML.
@supports not (aspect-ratio: 1) {
.video {
position: relative;
height: 0;
padding-top: 56.25%;
}
.video > iframe {
position: absolute;
top: 0;
left: 0;
height: 100%;
}
}Browser Support
Chrome
v88+2021-01-19Edge
v88+2021-01-21Safari
v15+2021-09-20Safari (iOS)
v15+2021-09-20Firefox
v89+2021-06-01