Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readCSS

Replace the padding-top hack with aspect-ratio

Give a responsive iframe a 16:9 aspect ratio without a percentage-padding wrapper.

You dropped a video embed into an article column and the iframe is 640px wide and 150px tall. An iframe with no height set gets the browser default of 150px. The fix you remember is a wrapper with a percentage of padding-top.

.embed {
  position: relative;
  height: 0;
  padding-top: 56.25%; /* 9 / 16 */
}

.embed iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

Percentage padding is measured against the width of the containing block, and 9 divided by 16 is 0.5625. The wrapper reserves a 16:9 box and the iframe, with position: absolute, fills it. That is two rules and an extra element in the markup.

Put the ratio on the iframe

aspect-ratio states the shape of the box. Give the iframe a width and a ratio, and the browser works out the height.

.video-embed {
  display: block;
  width: 100%;
  height: auto;
  border: 0;
  aspect-ratio: 16 / 9;
}

Add class="video-embed" to the iframe. The browser calculates the height from its width and the ratio.

The rule works on a div as well. An empty div with width: 640px and the same ratio measured 640 by 360. That makes it a fit for a placeholder that reserves room for an embed before it loads.

Measure both

To compare the sizes in Chromium, install playwright, run pnpm exec playwright install chromium, and save this script as an .mjs file. old is the padding wrapper, plain has neither fix, and fixed carries aspect-ratio.

import { chromium } from 'playwright'

const html = `<!doctype html>
<style>
  body { margin: 0; }
  .col { width: 640px; }
  .old { position: relative; height: 0; padding-top: 56.25%; }
  .old iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
  .plain iframe, .fixed iframe { display: block; width: 100%; border: 0; }
  .fixed iframe { aspect-ratio: 16 / 9; }
</style>
<div class="col" id="old"><div class="old"><iframe></iframe></div></div>
<div class="col plain" id="plain"><iframe></iframe></div>
<div class="col fixed" id="fixed"><iframe></iframe></div>`

const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 800, height: 900 } })
await page.setContent(html)
for (const id of ['old', 'plain', 'fixed']) {
  const box = await page.evaluate((id) => {
    const b = document.querySelector(`#${id} iframe`).getBoundingClientRect()
    return `${b.width} x ${b.height}`
  }, id)
  console.log(id, box)
}
await browser.close()
// prints old 640 x 360, plain 640 x 150, fixed 640 x 360

The padding version and the aspect-ratio version both measure 640 by 360, and the iframe with neither measures 640 by 150. In a second run with the column at 320px, both measured 320 by 180, so the ratio holds as the column shrinks.

The preferred aspect ratio needs at least one automatic dimension. The height: auto declaration keeps an embed’s HTML height attribute from setting a fixed height.