RWD Responsive Videos

Embedded videos like YouTube iframes don't respond to max-width the way images do, so keeping them fluid relies on a wrapper technique that locks in the aspect ratio.

Why Video Embeds Are Trickier Than Images

An embedded <iframe> ships with explicit width and height attributes, usually something like 560 by 315 pixels. Adding max-width: 100% shrinks the visible box down to fit a narrow screen, but the height attribute stays a fixed pixel number -- so as the width shrinks, the video's proportions get squashed instead of scaling evenly.

The Naive (Broken) Attempt

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<iframe
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  width="560" height="315"
  style="max-width: 100%;">
</iframe>

</body>
</html>
Note: That style attribute alone isn't enough. The width shrinks to fit its container, but the height stays exactly 315px regardless of screen size, so on a narrow phone the video ends up looking flattened or oddly cropped instead of keeping its 16:9 shape.

The Modern Fix: aspect-ratio

The CSS aspect-ratio property lets you set width: 100% on the iframe and declare the ratio you want to maintain -- the browser then computes the matching height automatically at every viewport size. It's supported in all current evergreen browsers and needs no extra wrapper element.

Using aspect-ratio

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<iframe
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  style="width: 100%; aspect-ratio: 16 / 9; border: 0;"
  allowfullscreen>
</iframe>

</body>
</html>

The Classic Fix: The Padding-Bottom Trick

Before aspect-ratio existed, developers achieved the same result with a clever use of a CSS quirk: percentage values for padding-bottom are calculated from the containing element's width, not its height. A wrapper div sets padding-bottom to a percentage matching the desired ratio (for 16:9, divide 9 by 16 to get 56.25%), gives itself height: 0, and then the iframe is positioned absolutely to fill that padded space completely.

Padding-Bottom Percentage Wrapper

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
  .video-wrapper {
    position: relative;
    width: 100%;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    height: 0;
    overflow: hidden;
  }
  .video-wrapper iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 0;
  }
</style>
</head>
<body>

<div class="video-wrapper">
  <iframe
    src="https://www.youtube.com/embed/dQw4w9WgXcQ"
    allowfullscreen>
  </iframe>
</div>

</body>
</html>
Aspect Ratiopadding-bottom Value
16:9 (widescreen)56.25%
4:3 (standard)75%
1:1 (square)100%
21:9 (cinematic)42.85%
Note: Reach for aspect-ratio on any project that doesn't need to support very old browsers -- it's shorter, needs no wrapper, and is easier to read. Keep the padding-bottom technique in your back pocket for legacy support requirements or for cases where you're already using a wrapper div to layer custom controls on top of the video.

Exercise: RWD Videos

What is the standard modern technique for making an embedded video scale with its container?