Xerobit
Frontend & Design

Replace the padding-top Hack with CSS aspect-ratio (Migration Guide)

Migrate the 56.25% padding-top hack to CSS aspect-ratio: before and after code, plus tested pitfalls with overflowing content, img attributes, grid stretch and flex stretch.

MA Mian Ali Khalid Updated Nov 2, 2026 7 min read
Free tool
Aspect Ratio Calculator
Calculate aspect ratios (16:9, 4:3, 21:9, etc.). Given W:H and one dimension, get the other. Responsive padding-top % for CSS aspect-ratio containers.
Open tool →
On this page ⌄
  1. Before and after
  2. Pitfall 1: content taller than the ratio
  3. Pitfall 2: both width and height set
  4. Pitfall 3: images with width and height attributes
  5. Pitfall 4: stretch in grid and flex
  6. Fallback with @supports
  7. Migration checklist
  8. Tested

If your stylesheet still has padding-top: 56.25% on a wrapper with an absolutely positioned child, aspect-ratio replaces it with two lines. The swap is almost mechanical, but aspect-ratio behaves differently from the hack in four situations: overflowing content, images with width and height attributes, grid stretch and flex stretch. Each was rendered in Chrome 156 and the box sizes read back, so the numbers below are measured, not assumed. Padding percentages and ratios can be worked out with the Aspect Ratio Calculator, which outputs both the aspect-ratio value and the legacy padding-top percentage.

Before and after

The padding hack works because percentage padding is relative to the container's width, so padding-top: 56.25% makes a box 9/16 as tall as it is wide:

/* before */
.frame { position: relative; padding-top: 56.25%; }   /* 9 / 16 * 100 */
.frame > iframe { position: absolute; inset: 0; width: 100%; height: 100%; }

/* after */
.frame { aspect-ratio: 16 / 9; }
.frame > iframe { width: 100%; height: 100%; }

In a 400px wide container both versions produce a 400 x 225 box (measured with getBoundingClientRect). The conversion is padding-top % = height / width * 100: 16/9 is 56.25, 4/3 is 75, 1/1 is 100, 21/9 is 42.8571, 9/16 is 177.7778. Going the other way, put the ratio in aspect-ratio as width / height.

Pitfall 1: content taller than the ratio

The hack keeps the box at a fixed height because the child is absolutely positioned and out of flow. aspect-ratio only sets a preferred ratio, and an element's automatic minimum height is its content. A long paragraph was put in a 400px wide aspect-ratio: 16 / 9 box:

CSSbox sizewhat happens
aspect-ratio: 16 / 9400 x 270box grows to fit the text
plus overflow: hidden400 x 225ratio holds, text clipped
plus min-height: 0 (no overflow rule)400 x 225ratio holds, text overflows (scrollHeight 270)

So the common claim that the box "overflows" without overflow: hidden is not what the default does: the box grows instead and silently stops being 16:9. If you need a fixed ratio with content you do not control, add min-height: 0 (or overflow: hidden) and decide what happens to the excess. For media, keep the media as the only child with width: 100%; height: 100%.

Pitfall 2: both width and height set

If both are definite lengths, the ratio has no effect. With width: 400px; height: 100px; aspect-ratio: 16 / 9 the box measured 400 x 100. MDN says the same: at least one of the box's sizes needs to be automatic. A max-height also wins: width: 100%; aspect-ratio: 16 / 9; max-height: 100px gave 400 x 100, so on wide screens the ratio disappears.

Pitfall 3: images with width and height attributes

Browsers turn the width and height attributes on an <img> into an aspect ratio hint. A 640 x 360 SVG image was loaded slowly (the response was held back) in a 400px wide container, with img { max-width: 100% }, and the box was measured before and after the image arrived:

markup and CSSbefore loadafter loadcomputed aspect-ratio
no attributes, height: auto0 x 0400 x 225auto
width=640 height=360, no CSS height400 x 360400 x 360auto 640 / 360
width=640 height=360 plus height: auto400 x 225400 x 225auto 640 / 360
CSS aspect-ratio: 16 / 9; width: 100%; height: auto400 x 225400 x 22516 / 9

Two lessons. First, the attributes only reserve space (and prevent layout shift) if you also set height: auto in CSS: without it, the attribute height of 360 wins and the image is distorted at 400 wide. Second, an explicit aspect-ratio in CSS reserves the space too, which is useful for responsive images whose attributes you do not control. MDN describes the auto keyword the same way: the given ratio is used until the content loads, then the intrinsic ratio takes over.

Pitfall 4: stretch in grid and flex

Stretching sets a definite size on the cross axis, and then the ratio does not apply to that axis.

A square (aspect-ratio: 1) next to a 300px tall sibling in display: grid; grid-template-columns: 1fr 1fr inside 400px:

  • default align-items: stretch: the square measured 300 x 300. The row is 300 tall, the item stretches to it, and the width follows the ratio, so the column grows and the other column shrinks.
  • with align-self: start: 200 x 200, as intended.

In a flex row (display: flex) the same square with width: 100px next to a 300px tall item measured 100 x 300: the ratio is ignored because stretch fixed the height. The fix is again align-self: flex-start (or align-items: flex-start on the container). If a cards grid looks lopsided after a migration, check for stretch first.

Fallback with @supports

MDN lists aspect-ratio as Baseline widely available, in browsers since September 2021. If you still support something older, keep the hack as the base style and override it:

.frame { position: relative; padding-top: 56.25%; }
.frame > iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }

@supports (aspect-ratio: 16 / 9) {
  .frame { padding-top: 0; aspect-ratio: 16 / 9; }
  .frame > iframe { position: static; }
}

In Chrome 156 the supported branch measured 400 x 225 for both the wrapper and the iframe, so the override does not double the height. The unsupported branch was run only as the standalone hack, which also gives 400 x 225.

Migration checklist

  1. Replace each hack wrapper with aspect-ratio; remove position: absolute from the child.
  2. Search for align-items: stretch parents (grid and flex) around ratio boxes and add align-self: start.
  3. Add min-height: 0 or overflow: hidden where text can exceed the box.
  4. Keep width and height attributes on images and set height: auto in CSS.

Tested

2026-11-02, Chrome 156.0.8078.48, playwright-core, Windows 11: every size above comes from getBoundingClientRect and getComputedStyle in that browser. MDN's aspect-ratio page was read for the Baseline status and the quoted behaviour; exact first-supported version numbers are not stated there, so none are given. Firefox and Safari behaviour for pitfalls 1 to 4 was not tested.

Related tools

Written by Mian Ali Khalid. More guides in Frontend & Design.