While I was coding the Elemin Theme (a responsive WordPress theme that I recently designed), one of the challenges that I faced was to make the embedded videos elastic. Using the max-width:100% and height:auto trick works with native HTML5 video tag, but it doesn't work with embed code using iframe or object tag. After hours of experimenting and Googling, I finally found a trick on how to achieve this. If you are creating a responsive design, this simple CSS trick will come in handy. View the final demo and resize your browser window to see it in action.
Elastic HTML5 Videos (demo)
With HTML5 video element, you can easily make it elastic by using the max-width:100% trick (see elastic HTML5 video demo). As mentioned in the introduction, this trick doesn't work if the embed code uses iframe or object tag which is commonly used by most video sharing sites such as YouTube and Vimeo.
video {
max-width: 100%;
height: auto;
}
Elastic Object & Iframe Embedded Videos (demo)
The trick is very simple. You need to wrap the embed code with a <div> container and specify 50% to 60% padding bottom. Then specify the child elements (iframe, object embed) 100% width, 100% height, with absolute position. This will force the embed elements to expand fullwidth automatically.
CSS
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.video-container iframe,
.video-container object,
.video-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
HTML
<div class="video-container">
<iframe src="http://player.vimeo.com/video/6284199?title=0&byline=0&portrait=0" width="800" height="450" frameborder="0"></iframe>
</div>
How to Create Fixed Width & Elastic
To restrict the width of the video, an additional <div> wrapper is required. Wrap the video with another <div> tag and specify a fixed width value with max-width:100%.
CSS
.video-wrapper {
width: 600px;
max-width: 100%;
}
HTML
<div class="video-wrapper">
<div class="video-container">
<iframe src="http://player.vimeo.com/video/6284199?title=0&byline=0&portrait=0" width="800" height="450" frameborder="0"></iframe>
</div>
<!-- /video -->
</div>
<!-- /video-wrapper -->
Compatibility
This trick works on all browsers (tested on Chrome, Safari, Firefox, Internet Explorer, Opera, iPhone and iPad).
Credits
This trick was found on tjkdesign.com.
@Nick La
You should follow the Creative Commons (CC) “guidelines” when using someone else work: http://creativecommons.org/licenses/by-nc-sa/2.0/
Please let me know if tweeting the above can help…
Excellent. I was looking for a solution for a website based on the 1080p (1920×1080) resolution. The videos work awesome! Thank you for sharing.
perfect tips. i tried and its work. thank you very much.
Ahhhh, I didn’t see what was special about this until I read what you mentioned about the padding. Very creative.
Yes, max-width trick is really helpful. I want to create a page at Web tips and tricks to centralize these techniques.
Thank you.
very nice template Design
it looks great. i may use it in my site. thanks very much.
Brillant… your a life saver!
50 Comments
Thanks for this information. I had the same issue with a site that had a lot ofvideo content. My problem is now solved.
Thanks
Ian
This was exactly what I was looking for. Thanks for taking the time to post!
These are great! Thanks for sharing…love the Elastic HTML5 Videos.
thanks very good idea for my website thanks a lot
Say, did you manage to teach WordPress to wrap oembeds into a div automatically? If so – how the heck? :)
Can this be used with the jwplayer?
Did you find a good solution for JWPlayer? I’m having issues with Youtube through JW on an adaptive site.
WOW!!! Nice Tip
This. is. amazing. Thank you so much for the link!!
Chris Coyer did the same thing over at CSS-Tricks – http://css-tricks.com/7066-fluid-width-youtube-videos/, this one is somewhat similar. Awesome explanation nonetheless.
One thing I’d recommend doing is ensuring that your videos are being set to the proper aspect ratio. There is no guarantee that they will be in 16:9.
height / width * 100 = padding-bottom
I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.
I have had an issue with this for ages, especially now as I have just created a site purely built on video content. The ideas in this will come in handy big time, thanks! I may even share my site with you to see how your fix may have worked.