Making a YouTube Embed Properly Responsive

I was responsible for adding a YouTube video to a client site. It sounded like the sort of task that should take a few minutes: copy the embed code, paste it into the page, check that the video loads, and move on.

The default YouTube embed produced a fixed 560 by 315 video. That is a sensible desktop-sized box, but it is not especially useful when the surrounding page is responsive.

My first attempt was to make the video use the full width of its container. I changed the width to 100% and tried setting the height to auto. The width behaved as expected. The height did not.

The video expanded horizontally, but the player became only around 20–30 pixels high. It was technically present, but visually useless. A very wide strip of YouTube is not much of a video.

Width was easy; height was the trap

The problem was that I had only told the browser how wide the iframe should be. I had not told it how the height should relate to that width.

An iframe does not size itself like an image with an obvious intrinsic shape. Setting the width to 100% does not automatically tell the browser that the video should remain 16:9. height: auto alone was not enough in this situation.

The original YouTube dimensions had quietly provided the correct ratio: 560 pixels wide, 315 pixels high, or 16:9. I needed to preserve that ratio while allowing the width to change.

The missing piece was aspect-ratio

The fix was to use CSS’s aspect-ratio property alongside the full-width declaration:

CSS
width: 100%;
aspect-ratio: 16 / 9;
Code language: CSS (css)

That gave the browser the information it was missing. The video could now fill the available width while calculating the appropriate height automatically.

The MDN reference for aspect-ratio describes it as the preferred width-to-height ratio of an element’s box. It also notes that at least one dimension needs to remain automatic for the ratio to affect sizing. That is the important part here: the width is fluid, and the height is calculated from the ratio.

The practical example in Flavio Copes’s explanation of responsive YouTube embeds uses the same approach. A separate DEV Community example reaches the same result with a full-width iframe and a 16:9 ratio.

Can I Use says modern browsers are covered

I also checked the current Can I Use support table for aspect-ratio. The major browser thresholds are:

  • Chrome 88 and newer — supported
  • Edge 88 and newer — supported
  • Firefox 89 and newer — supported
  • Safari and Safari on iOS 15 and newer — supported
  • Opera 74 and newer — supported
  • Internet Explorer 6–11 — not supported

That makes aspect-ratio a sensible choice for a modern client site, but it is not universal. If the site still needs to support Internet Explorer or older browser versions, the CSS alone is not enough. A fallback is required.

The old padding hack still works

There is an older solution involving a wrapper with padding-bottom: 56.25%, an absolutely positioned iframe, and several extra CSS rules. It works because percentage padding is calculated from the wrapper’s width. Flavio Copes’s explanation covers this fallback as well.

For this client site, the newer property was cleaner. It removed the need for an extra wrapper and made the intent obvious. The video should be full width, with a 16:9 shape. The CSS says exactly that.

The lesson was small but useful: making an embedded video responsive is not just a matter of changing its width. The browser also needs to know the relationship between width and height.

Once I supplied that missing ratio, the YouTube video stopped being a strangely compressed strip and became a normal responsive embed.