How do you make a YouTube or Vimeo iframe responsive?

A YouTube or Vimeo iframe becomes responsive when its width follows the container and its aspect ratio stays fixed. Iframes do not derive an intrinsic video ratio from the remote player, so a fixed width paired with an unrelated height can overflow or squash the frame. Reserve the player shape before it loads.
<div class='video-embed'>
<iframe
src='https://www.youtube.com/embed/M7lc1UVf-VE'
width='560'
height='315'
title='Product setup demonstration'
loading='lazy'
allow='fullscreen; picture-in-picture'
allowfullscreen>
</iframe>
</div>
.video-embed {
aspect-ratio: 16 / 9;
}
.video-embed iframe {
display: block;
width: 100%;
height: 100%;
border: 0;
}
The wrapper reserves space and prevents layout shift while the provider initializes. Use the real media ratio if the video is vertical or uses another format. A maximum width on the wrapper can keep a low-resolution embed from becoming oversized on a wide content column, while margin auto centers it.
Give every iframe a descriptive title that identifies the video or player purpose. Preserve provider-required allow values and full-screen permission. Lazy loading can reduce initial work for embeds below the fold, although a click-to-load facade often saves more requests when a page contains several third-party players.
Do not hide overflow around a player until you know what is escaping. Cookie notices, captions or player controls may need the full frame. Test the narrowest supported width with browser zoom and larger system text. The playback controls must remain reachable without horizontal page scrolling.
Run the embed on real mobile browsers because third-party scripts, consent modes and full-screen behavior are not fully represented by a resized desktop window. Confirm that orientation changes preserve the player, that leaving full screen returns focus sensibly, and that a blocked or unavailable video leaves useful surrounding context.