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.
“You need to wrap the embed code with a container and specify 50% to 60% padding bottom” to be precise this is ratio width to hight. If width is 800px as in article and height is 450px ( embeded element ) that we have :
450px * 100% / 800px= 56.25%. Anyway thanks, it works.
Tom
Great tip. Thanks.
Thank you man. It works great!
Thanks its what i was looking for!
Thank, exactly what i was after and works great!
video
Thanks! :D Very nice job!
Since upgrading my iPad to iOS 6 this fix doesn’t seem to work any more, I think it’s height: auto causing the issue. Anyone experienced similar and have a fix?
I also am experiencing this but still looking for workarounds. Have you had any luck?
I did a little tour of your blog, it looks really interesting. I’m going to sit back. I congratulate you it’s good to have made this blog
thank you very much.
Your tutorial came in really handy. Thanks! I think responsive design is going to be the future of web. It’s really silly that people still try to get iphone apps for their small business. This is what my company focuses on. Please check out my blog post on the benefits of responsive design. http://www.polarmass.com/understanding-responsive-design-and-its-benefits-to-your-business.html
Great stuff!!, I also found great ideas in this site: http://www.freakzion.com/index.php/The-Blog-December-2012/resize-your-videos-in-responsive-design.html
Hope it helps you as well
It works on my vimeo videos, but when embedding stills/ photos that are on my server it doesn’t work in chrome, the images crop, they don’t scale like the videos. Is there a workaround for that.?
Thanks for sharing this awesome tips bro.I injoy it,,Thanks,,,
Thanks for your excellent post. Elastic videos rocks :)
Thanks for this, we’ve had problems in the past posting videos onto our site and have found this to be very useful, this should allow us to post more videos onto our site, again many thanks.
Thanks for sharing such a wonderful post,its really work,elastic videos rocks :)
please, solution for JW PLAYER? watch this:
VIDEO
Cargando….
jwplayer(“mediaplayer”)
.setup({flashplayer: “PLAYER.swf”,
file: “VIDEO.m4v”,
width: 450, height: 250,
autostart: “true”,
repeat: “none”,
icons: “true”,
controlbar: “none”,
image: “INICIO.jpg”});
WoW!look awesome,, and thanks for this solution, really appreciate it!Thank you so much<,,,,,,,,,,,,,,!;\:
This is just S U P E R B!
Thank you very much for sharing. Helped me a lot to integrate my videos responive in a Genesis theme.
Again many thanks!
Great tip, thanks so much!