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.
Problem solved. Great advise – thank you!
Thanks for sharing. U saved much time to many people :)
Hey great post..
This should make all user’s life a lot easier from now on. Thanks a lot for posting the project files
Its now easier to understand Elastic CSS better…Thank You
Interesting article! Very useful! I will use this in my future projects!
Again. Thanks for the great tip!
The trick works very well, thank you!
Excellent Info. Will be a great help. Thanks for sharing it.
?
well done!
awesome! thanks, i’ve been wondering about this for a while.
It is a very awesome information.
So max-width:100% is the big deal?
Its work nice for me, thank you.
[code]
img,
iframe,
object,
embed {
max-width: 100%;
}
[/code]
Awesome technic! But I didn’t understand the meaning of that value for padding-bottom (56.25%?). Could you better explain that please?
it’s the 16:9 aspect-ratio in percentage (9/16*100).
you might want to define classes for this and for other ratios, e.g.:
figure.ar169 { padding-bottom: 56.25%; }
figure.ar43 { padding-bottom: 75%; }
figure.ar11 { padding-bottom: 100%; }
then you can allow the user to change the class value by a js button or whatever…
(anyway, i use the html5 figure element as the container.)
Awesome!
Hello!!
Very Nice Template!!
Thank you!!
thx for the tuto ;)
you can remove the “padding-top: 30px;” part from the css if you don’t want fixed control bar for youtube videos. just add the parameter after the video url: ?autohide=1 :) (and there’s really no need to add extra 30 pixels in case of the vimeo player.)
Its work nice for me! Thank you.
Nice blog! Great work!
web design Curitiba