CSS RWD Videos
In this page:
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
<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.
- 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
<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.
- 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
<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.
- 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
<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.
- 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
<style>
video {
width: 100%;
}
@media (max-width: 600px) {
video {
display: none;
}
}
</style>
<video poster="https://placehold.co/600x300" controls></video>
- Setting a fixed
widthandheighton a video, which does not scale. - Forgetting
position: absoluteon the iframe inside the padding trick wrapper. - Using the padding percentage from the wrong side, since
56.25%gives 16:9 only.
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: