← Back to CSS Course | Chapter 12: Responsive Design | Lesson 7 of 8

CSS RWD Videos

An embedded video without responsive rules is like a TV bolted into a wall at one fixed size -- responsive video techniques let that same screen resize itself to fit whatever room it ends up in, without ever getting stretched or cropped.
Syntax
css
video {
  max-width: 100%;
  height: auto;
}

The Video Aspect Ratio Problem

Unlike an image, a <video> or embedded <iframe> (e.g. a YouTube embed) does not automatically preserve its aspect ratio when its width is constrained by CSS -- naive resizing either distorts the video or leaves ugly black bars unless the aspect ratio is explicitly maintained.

Example: The Video Aspect Ratio Problem

css
<style>
iframe {
  width: 100%;
}
</style>
<iframe src="about:blank" height="200"></iframe>

The Padding-Top Percentage Trick

Before native CSS support existed, the standard technique wrapped the video in a container with padding-top: 56.25% (the percentage for a 16:9 ratio) and positioned the video absolute to fill that padded box, exploiting the fact that percentage padding is calculated from the container's WIDTH even for the top/bottom sides.

Note:
  • Accepted values:
  • padding-top: 56.25% — 16 / 9 (9 / 16 = 0.5625)
  • padding-top: 75% — 4 / 3
  • position: relative — on the wrapper
  • position: absolute; inset: 0 — the inner iframe

Example: The Padding-Top Percentage Trick

css
<style>
.wrapper {
  position: relative;
  padding-top: 56.25%;
  background: #ddd;
}
.wrapper iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>
<div class="wrapper">
  <iframe src="about:blank"></iframe>
</div>

The Modern aspect-ratio Property

The aspect-ratio CSS property (e.g. aspect-ratio: 16 / 9) now does natively what the padding-top hack simulated, letting a video or its wrapper maintain a fixed width-to-height ratio directly without any absolute positioning wrapper trick.

Note:
  • Accepted values:
  • aspect-ratio: 16 / 9 — widescreen video
  • aspect-ratio: 4 / 3 — classic monitor
  • aspect-ratio: auto — use the natural ratio (initial value)
  • width: 100% — the height follows automatically
  • height: auto — needed so the ratio can apply
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: The Modern aspect-ratio Property

css
<style>
iframe {
  width: 100%;
  aspect-ratio: 16 / 9;
}
</style>
<iframe src="about:blank"></iframe>

max-width on Native Video Elements

A native <video> element (not an iframe embed) responds to the same max-width: 100%; height: auto; rule as images, since the browser already knows and preserves its intrinsic aspect ratio.

The padding-trick/aspect-ratio techniques are mainly needed for iframe embeds that don't expose that intrinsic ratio to CSS.

Note:
  • Accepted values:
  • max-width: 100% — never wider than the parent
  • height: auto — keeps the ratio
  • width: 100% — fills the parent
  • note: iframe embeds do not expose their ratio

Example: max-width on Native Video Elements

css
<style>
video {
  max-width: 100%;
  height: auto;
}
</style>
<video controls></video>

Performance Considerations for Responsive Video

Autoplaying background video is especially costly on mobile data and battery -- responsive video strategy often includes serving a static poster image or lower-resolution source below a certain breakpoint rather than the full video at every screen size.

Note:
  • Accepted values:
  • preload="none | metadata | auto" — how much of the video to download (HTML attribute)
  • loading="lazy" — defers offscreen iframes
  • poster="<url>" — a preview image
  • src — smaller encodes for small screens

Example: Performance Considerations for Responsive Video

css
<style>
video {
  width: 100%;
}
@media (max-width: 600px) {
  video {
    display: none;
  }
}
</style>
<video poster="https://placehold.co/600x300" controls></video>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Setting a fixed width and height on a video, which does not scale.
  2. Forgetting position: absolute on the iframe inside the padding trick wrapper.
  3. Using the padding percentage from the wrong side, since 56.25% gives 16:9 only.
Browser Support

The aspect-ratio property is supported in Chrome 88+, Firefox 89+ and Safari 15+; the padding-top technique works in all modern browsers.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.