in Software Development

Creating Responsive CSS Sites with Video

I was recently working on a project creating a WordPress site using the Virtue theme. Since this is a responsive theme, all elements were working great on mobile web as well as various desktop sizes. Then, I wanted to embed a Vimeo video right onto the home page. When you copy Vimeo embed  code (or Youtube for that matter), you get something like this with the height/width explicitly defined:

<iframe src="//player.vimeo.com/video/106486492" 
width="1140" height="641" frameborder="0">
</iframe>

This looks great on my 1440×900 monitor:

image_ok

But wasn’t at all responsive to smaller screens. It looked like this on an Android phone:

Android

The search bar wrapped nicely and the menu condensed down to a mobile friendly bar but the video stayed as is and broke the whole experience.

I found an awesomely complete post describing this issue and ways to fix it. There were several solutions posted – the one I used worked quite well. The idea is that you declare a css class that auto-sizes to the parent element and also sets a ratio (16:9) for width/height that will be maintained regardless of the size of the parent element.

The CSS classes look like this:

.videoWrapper {
	position: relative;
	padding-bottom: 56.25%; /* 16:9 */
	padding-top: 25px;
	height: 0;
}
.videoWrapper iframe {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

And the declaration is something like this:

<div class="videoWrapper">
<iframe src="//player.vimeo.com/video/106486492" frameborder="0">
</iframe>
</div>

The results are something like this:

after

 

Great to see that it worked out well. I do wonder though if there’s a way to allow these videos to be responsive without any additional effort – especially for all the non-technical folks out there embedding videos into blogs and such.

Write a Comment

Comment