The problem with fixed dimensions
Video is the media type that most often breaks responsive layouts. A self-hosted HTML5 <video> element and an <iframe> embed from YouTube, Vimeo or another provider are usually declared with static width and height attributes.
<video width="400" height="300" controls ... ></video>
<iframe width="400" height="300" ... ></iframe>
<!-- maybe even super old school -->
<object width="400" height="300" ... />
<embed width="400" height="300" ... />
Those static values fail as soon as the parent container is narrower than the declared width. The video overflows the layout rather than shrinking to fit.

HTML5 video
For standard HTML5 video, a fluid width is a single declaration:
<video width="100%" ... ></video>
This makes the video fit the width of its container. The height declaration must be removed at the same time, so the element keeps the video's aspect ratio as it grows and shrinks. Leaving it in produces empty "bars" around the content. Unlike images, the video itself holds its own aspect ratio no matter how the element is sized.
The same result can be achieved entirely in CSS, ignoring whatever the HTML declares:
video {
/* override other styles to make responsive */
width: 100% !important;
height: auto !important;
}
Making <iframe> embeds fluid
The width-only approach does not work for iframe-delivered video. Forcing the width to 100% is fine, but setting height: auto leaves a static height of 150px, which is too squat for most video.
The fix comes from a technique pioneered by Thierry Koblentz and published on A List Apart in 2009, "Creating Intrinsic Ratios for Video". The video is wrapped in another element that carries an intrinsic aspect ratio, and the video is absolutely positioned inside it. The result is fluid width with a predictable height.
<div class="videoWrapper">
<!-- Copy & Pasted from YouTube -->
<iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe>
</div>
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
A variation of the technique lets the aspect ratio be set directly from the HTML:
<div class="videoWrapper" style="--aspect-ratio: 3 / 4;">
<iframe ...>
.videoWrapper {
...
/* falls back to 16/9, but otherwise uses ratio from HTML */
padding-bottom: calc(var(--aspect-ratio, .5625) * 100%);
}
Legacy embeds using <object> and <embed> tags are covered by extending the wrapper selector:
.videoWrapper iframe,
.videoWrapper embed,
.videoWrapper object { }
Limitations of the wrapper approach
The technique has several practical drawbacks:
- It requires a wrapper element, so pasting embed code straight from YouTube is no longer enough; users need a little more know-how.
- When an existing site moves to a fluid design, every old video needs its HTML adjusted.
- All videos must share the same aspect ratio. Any video with a different ratio is forced into the wrapper's ratio and gets bars, unless you maintain a set of class names to adjust each case, which adds complexity.
When either of those constraints applies, a JavaScript solution is an alternative. On page load, each video's aspect ratio is recorded; then, once immediately and again on every window resize, every video is resized to fill the available width while holding its aspect ratio. With jQuery, that looks like this:
// Find all YouTube videos
// Expand that selector for Vimeo and whatever else
var $allVideos = $("iframe[src^='//www.youtube.com']"),
// The element that is fluid width
$fluidEl = $("body");
// Figure out and save aspect ratio for each video
$allVideos.each(function() {
$(this)
.data('aspectRatio', this.height / this.width)
// and remove the hard coded width/height
.removeAttr('height')
.removeAttr('width');
});
// When the window is resized
$(window).resize(function() {
var newWidth = $fluidEl.width();
// Resize all videos according to their own aspect ratio
$allVideos.each(function() {
var $el = $(this);
$el
.width(newWidth)
.height(newWidth * $el.data('aspectRatio'));
});
// Kick off one resize to fix all videos on page load
}).resize();
FitVids.js and a plain JavaScript option
FitVids.js is roughly what this approach became. Instead of handling resizing, it loops over all the videos and inserts the aspect-ratio-enabling HTML wrapper plus the CSS the technique needs, which is considerably more efficient than binding to a window resize handler.
jQuery has fallen out of favor, so a Vanilla JavaScript version is also available (it leaves the CSS to you).
- Note that every browser renders iframe, canvas, embed and object tags at 300px × 150px when no other dimensions are declared, even where this is not present in the UA stylesheet.



