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: 0 or overflow: auto.
  • aspect-ratio has no effect when both width and height are set to something other than auto. Leave one of them as auto.
  • For <img> and <video>, writing the width and height attributes in HTML lets the browser reserve the ratio before the media loads. The CSS only needs height: 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-19
  • Edge

    v88+
    2021-01-21
  • Safari

    v15+
    2021-09-20
  • Safari (iOS)

    v15+
    2021-09-20
  • Firefox

    v89+
    2021-06-01

Search by feature name or code

e.g. aspect-ratio, :has(), padding-top

Try a different word or a property name.

Could not load search.