More HTML answers

by Danny
Published 2 min read

Make an iframe responsive with width: 100% and aspect-ratio: 16 / 9, give it a title for screen readers, and lazy load it so embeds further down the page do not slow the first load.

The short answer

#

Let the iframe fill the width of its container and use the CSS aspect-ratio property to set its height. Give it a title, add loading="lazy" if it is below the fold, and keep YouTube's referrer policy attribute so the player loads.


_10
<iframe
_10
class="video"
_10
src="https://www.youtube.com/embed/VIDEO_ID"
_10
title="How to shape a sourdough loaf (YouTube video)"
_10
loading="lazy"
_10
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
_10
referrerpolicy="strict-origin-when-cross-origin"
_10
allowfullscreen
_10
></iframe>


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

Why it happens

#

The embed code YouTube gives you has fixed width="560" and height="315" attributes. On a narrow phone screen the iframe is wider than the viewport and causes horizontal scrolling. If you only set width: 100% in CSS, the height stays at 315 pixels and the video gets letterboxed or squashed.

An iframe has no natural aspect ratio of its own, unlike an image, so the browser cannot work out the height from the width. The aspect-ratio property supplies that ratio. With height: auto, the browser calculates the height from the width, so the frame keeps its 16:9 shape at every size.

How to fix it

#

Use aspect-ratio

This is the approach in the short answer and works in all current major browsers. Change the ratio for other content, for example 4 / 3 for older video or 1 / 1 for a square embed. If you keep the width and height attributes from the original embed code, height: auto in your CSS overrides the fixed height.

To stop a video becoming huge on wide screens, limit the width:


_10
.video {
_10
width: 100%;
_10
max-width: 960px;
_10
margin-inline: auto;
_10
aspect-ratio: 16 / 9;
_10
height: auto;
_10
}

Give every iframe a title

Screen readers announce the title when users move into the frame. Without it they may only hear "frame" or the URL. Describe the content, not just "YouTube video".

Lazy load embeds below the fold

A single YouTube embed loads a lot of JavaScript. loading="lazy" tells the browser to wait until the iframe is near the viewport. It is supported for iframes in all current major browsers. Leave it off for a video at the very top of the page, where it would only delay something the user sees straight away.

For pages with many videos, consider a click-to-load facade: show a thumbnail image and a play button, and only insert the iframe when someone clicks it.

Keep the referrer policy

YouTube now expects to see the origin of the page embedding the video. If your site sends no referrer, for example because of a Referrer-Policy: no-referrer header or referrerpolicy="no-referrer" on the iframe, the player can fail with error 153, "Video player configuration error". Keep referrerpolicy="strict-origin-when-cross-origin" on the iframe, which sends only your origin to YouTube, not the full page URL.

The old padding hack

Before aspect-ratio existed, the usual trick was a wrapper with padding-top: 56.25% and an absolutely positioned iframe inside. You will still see it in older code. It works, but there is no reason to use it in new code.

Common pitfalls

#
  • Setting width: 100% without aspect-ratio and height: auto, so the fixed height stays.
  • Missing title, which fails accessibility audits and confuses screen reader users.
  • Lazy loading a video that is visible on first load, which makes it appear later than it should.
  • Stripping the referrer site-wide and then wondering why every YouTube embed shows an error.
  • Assuming the youtube-nocookie.com domain makes an embed fully private. It reduces tracking before playback, but it is not a complete privacy fix and does not solve referrer errors.
  • Forgetting allowfullscreen, so the full screen button does nothing.
#

Was this resource helpful?