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.
Excellent!
Thanks for this solution, really appreciate it!
Great! Thank you very much :)
Thanks so much! Worked perfectly.
Thanks a lot
Amazing and simple ;)
Cool, thanks very much for the fix. Seems silly that YouTube doesn’t have an option for filling in the auto height (if height is not supplied). It’s already a bit complicated for general users so this cuts down on problems with the automation/integration process.
It works great for IE and Chrome but it doesn’t work for FF 11. Do you have an idea how to get this in FF done?
You may have found this tip at tjkdesign.com, but you rock for bringing it to the rest of the world! Your site came up first in Google (and a very nice site btw).
Passing a small # shortcode [video] to # wordpress for all his videos responsive with the # css link above.
In the functions.php file add:
caption_shortcode function ($ atts, $ content = null) {
return ”. $ content. ”;
}
add_shortcode (‘video’, ‘caption_shortcode’);
And after in the articles or posts surrounded the code from youtube without width = “#” height = “#”.
example
[video] [/ video]
Can’t put any div
http://pastebin.com/UVcAbuUM
I was trying using the method but when I put float left, it failed miserably.
Sorry but never mind, fixed the issue by making another wrapper on it. You can’t float the video container directly.
Thanks for the awesome tip!!!
Extra video-container made it work like crazy!!! :D
Awesome Tip!! This will really help with a responsive theme I am working on.
If you could help with this problem, that would be great! I’m trying to set a div with a border background that will auto height itself with added content. Trouble is, the content has absolute divs in there, which just don’t obey auto height – they are a rule unto themselves. How would I code a div that will auto adjust? Any help appreciated.
Million thanks!!
Videos are good ways for connecting with your audiences and readers of your website are getting chance to see you in action.So videos must be used in website
Excellent post. I’ve been working with responsive design for the past couple of months and stretching my wings a bit. I appreciate how concisely you spelled all this out. Wonder if you know of a responsive audio player that can be duplicated on same page (I’ve only found one using jS that allows just one music clip per page)?
Thank you for this solution!
Amazing!!! streamlined and beautiful!. …worked seamlessly.
Thank you so much for your efforts.
-uxepi
Hi,
I’m currently viewing the demo page with Firefox 13 and I cannot resize the width of the Firefox window.
Seems like a Firefox issue to me as it works everywhere else (Chrome, Safari, Opera, IE), and there is also a bug report about it: https://bugzilla.mozilla.org/show_bug.cgi?id=765788
Just wanted to let you know ;)
Thanks for your work
This is great. Has saved me so much work as I am new to responsive design but the more I do, the more I like it!
thank you for this clean solution. like it (Y)