---
title: "How do I embed a responsive YouTube video or iframe?"
description: "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."
url: https://www.danny.engineering/answer/html/how-do-i-embed-a-responsive-youtube-video-or-iframe
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["HTML"]
---

# How do I embed a responsive YouTube video or iframe?

## 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.


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


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


## 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:


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


### 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.

## Related reading

- [MDN: aspect-ratio](https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio)
- [MDN: the iframe element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe)
