Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use CSS’s native aspect-ratio property instead of percentage bottom padding for responsive boxes. For a full-width 16:9 frame, write width: 100%; aspect-ratio: 16 / 9; and leave its height automatic so the ratio can affect sizing.

Replace the padding trick with aspect-ratio

A modern responsive frame can be expressed directly:

.media-frame {
  width: 100%;
  aspect-ratio: 16 / 9;
}

For a 4:3 box, use aspect-ratio: 4 / 3. The property specifies a preferred width-to-height ratio; it does not force dimensions that CSS has otherwise fixed. At least one dimension needs to be automatic for the preferred ratio to participate in sizing. For example, if both width and height are explicitly set, the ratio does not override them. See MDN’s aspect-ratio reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why percentage padding created a ratio

In horizontal writing modes, percentage padding is calculated from the containing block’s inline size—normally its width—not its height. A box with no intrinsic content height and padding-bottom: 56.25% therefore gains a height equal to 56.25% of its available width, producing a 16:9 shape. That behavior made the technique useful for responsive video wrappers before native aspect-ratio support was broadly available. MDN documents the calculation in its padding-bottom reference.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

The native property states the intended relationship directly, making the layout easier to read and maintain. MDN describes its availability across browsers since September 2021; check the compatibility table against the oldest browsers your project must support.

Keep the frame ratio separate from media fitting

aspect-ratio shapes the box. It does not decide how an image or video fits inside it. If the media must fill the frame, size and position it separately, then select object-fit based on the desired result. cover fills the frame by cropping when ratios differ; contain keeps the whole image visible, which can leave empty space.

.media-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.media-frame img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Choose cover only when cropping is acceptable. If showing the full image matters more than filling every part of the frame, use contain.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for intrinsic image ratios

Replaced elements such as images can have intrinsic dimensions and an intrinsic ratio. If an image should use a temporary 16:9 ratio before its content loads and then honor its own intrinsic ratio, the property supports the form aspect-ratio: auto 16 / 9. Consult MDN’s guide to understanding and setting aspect ratios for the distinction between replaced and non-replaced elements.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Migration checklist

  1. Identify the intended frame ratio—for example, 16:9 or 4:3—and replace the percentage padding with aspect-ratio.
  2. Keep at least one dimension automatic. A common responsive setup is width: 100%; height: auto; aspect-ratio: 16 / 9;, adjusted for the element and layout.
  3. If media must fill the frame, set its dimensions and positioning separately, then choose object-fit for cropping or letterboxing.
  4. Verify browser support for the project’s oldest required browser using MDN’s compatibility data; use a fallback only if that support requirement calls for one.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.