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.
Great time saver thanks…
good tricks..
really helped me a lot in my coding..
thanks
I would like to add some codes in my theme so that all the Embedded youtube videos on my site, get 100 % width and height on mobile. What should I do ? anybody can help me please.
Huge thanks to you! This is exactly what I needed to finish up my site, and it was so clearly explained.
Thanks!
Derek
Nice tips people, cheers for this.
certainly like your web site however you need to test the spelling on quite a few
of your posts. Several of them are rife with spelling issues and I to find it very troublesome to tell the reality
however I’ll definitely come again again.
thanks a lot =] woot!
Thanks I was looking for this fix :)
After searching a long time this Elastic Object & Iframe Embedded Videos worked for me, Thanks alot very good and simple and clear instructions also for not really technical profis.
going to take a look over all the site!
Susanne
Excellent Elastic videos. All the videos have great things and i have learned a lot from it. :)
Thanks
LV Outlet San Diego California
It’s Great tutorial.Thanks Admin :)
thanks for sharing! I learn a lot :D
Excellent post. we can not use videos to get some elasticity in our website.
Thanks