IN A WORLD of responsive and fluid layouts on the web, ONE MEDIA TYPE stands in the way of perfect harmony: video. There are lots of ways in which video can be displayed on your site. You might be self-hosting the video and presenting it via the HTML5 tag. You might be using YouTube, Vimeo, or some other video provider that provides
.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
height: 0;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
CodePen Embed Fallback
There is a clever adaptation of this that allows you to adjust the aspect ratio right from the HTML, like:
.videoWrapper {
…
/* falls back to 16/9, but otherwise uses ratio from HTML */
padding-bottom: calc(var(–aspect-ratio, .5625) * 100%);
}
Some old school video embedding uses and tags, so if you’re trying to be comprehensive, update that wrapper selector to:
.videoWrapper iframe,
.videoWrapper embed,
.videoWrapper object { }
But, but… aspect ratios, legacy content, non-tech users, etc.
The above technique is awesome, but it has several possible limitations:
It requires a wrapper element, so just straight up copy-and-pasting code from YouTube is out. Users will need to be a bit savvier.If you have legacy content and are redesigning to be fluid, all old videos need HTML adjustments.All videos need to be the same aspect ratio. Otherwise, they’ll be forced into a different aspect ratio and you’ll get the “bars”. Or, you’ll need a toolbox of class names you can apply to adjust it which is an additional complication.
If either of these limitations applies to you, you might consider a JavaScript solution.
Imagine this: when the page loads all videos are looked at and their aspect ratio is saved. Once right away, and whenever the window is resized, all the videos are resized to fill the available width and maintain their aspect ratio. Using the jQuery JavaScript Library, 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();
That’s sorta what became FitVids.js
Except rather than deal with all that resizing business, FitVids.js loops over all the videos and adds the aspect-ratio enabling HTML wrapper and CSS necessary. That’s way more efficient than needing to bind to a window resize handler!
CodePen Embed Fallback
Plain JavaScript instead
jQuery is rather out of favor these days. Fortunately, Dave has a Vanilla version (that is BYO CSS):
CodePen Embed Fallback
Literally all browsers will render iframe, canvas, embed, and object tags as 300px × 150px if not otherwise declared. Even if this isn’t present in the UA stylesheet.
Post navigation