How to Embed a Responsive YouTube Video with CSS
To make a YouTube embed responsive, size the iframe to its container and give the wrapper aspect-ratio: 16 / 9. This example caps the width at 960px and scales the player down to fit a narrower column.
Copy the embed URL from YouTube
Open the video on YouTube, choose Share → Embed, and copy the markup. If the option is missing or the video cannot be embedded, check YouTube’s embedding guidance and the video owner’s embedding settings.
Use the youtube.com/embed/VIDEO_ID URL in the iframe’s src. Keep the regular watch?v= URL for a direct link when the player is unavailable. The embed markup’s 560×315 dimensions are defaults that CSS can override.
Keep a 16:9 ratio with CSS
The outer box takes the available width up to max-width. The inner box uses the CSS aspect-ratio specification to calculate a 16:9 height from that width. The iframe is positioned relative to the inner box and fills it in both directions.
Save the code as UTF-8 in example.html and resize the browser window. Replace src and title for your own video. To check playback, serve the file from your site or a local development server and open its http:// or https:// address.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive YouTube embed example</title>
<style>
.video-embed {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
.video-embed__frame {
position: relative;
aspect-ratio: 16 / 9;
}
.video-embed__frame iframe {
position: absolute;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
border: 0;
}
</style>
</head>
<body>
<div class="video-embed">
<div class="video-embed__frame">
<iframe
src="https://www.youtube.com/embed/ybhXVSAdIRE"
title="Avengers Endgame final trailer from Marvel Korea"
width="560"
height="315"
loading="lazy"
referrerpolicy="strict-origin-when-cross-origin"
allow="encrypted-media; picture-in-picture"
allowfullscreen
></iframe>
</div>
</div>
</body>
</html>
A 360px parent gives the player an area of about 360×202.5px. A parent wider than 960px gives it at most 960×540px. To calculate the height of a 16:9 frame, multiply the width by 9 / 16.
Check the layout and player controls
The YouTube player size requirements specify a minimum player viewport of 200×200px and recommend at least 480×270px for 16:9 players. Check that the controls fit in the actual page layout. If a narrow column cannot give the player 200px of height, provide a direct link to watch the video.
If the embed is unavailable, watch the original trailer on YouTube.
Give the iframe a descriptive title so screen reader users can identify it. loading="lazy" can defer loading when it is far off screen, and allowfullscreen permits full-screen playback.
Handle older browsers and playback errors
For a browser or WebView that lacks aspect-ratio, replace the example’s .video-embed__frame rule with the CSS below. Keep the outer container’s max-width and the iframe styles.
.video-embed__frame {
position: relative;
height: 0;
padding-top: 56.25%;
}
Vertical percentage padding on the inner box is calculated from the width of its containing block. 56.25% is 9 divided by 16. The absolutely positioned iframe fills the space reserved by that padding.
If the frame is sized correctly and playback fails, check that the video allows embedding and that src uses /embed/. In the YouTube player error codes, error 153 indicates a missing referrer or equivalent identification. Try serving the file over HTTP and check whether the site strips the referrer.




