Responsive Youtube Embed
Responsive YouTube Embed
I came across a small problem when adding a video from YouTube to one of my blog posts about the Childish Gambino website. Since this website is fully “responsive”, I needed the YouTube video to re-size depending on the browser size or device width. But when I added the embed code, the video had a fixed height and width. No!! This looked fine on desktop computers, but pretty much broke the design when viewing on a mobile device. We needed a responsive YouTube embed code. You would think that videos that have 100% width set would automatically just resize to the surrounding container. Well, thats is not the case.
The fix was actually quite simple, and here is how.
You will need to wrap the responsive youtube embed code with a <div> and specify a 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. Awesome! Exactly what we need.
First you will need to add the following to your style sheet.
CSS
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%;
}
Next, edit add some HTML around your embed code.
HTML
If the responsive YouTube embed worked, your videos should be responsive, and ready to view on Tablets and Mobile devices.
I found this article on Web Designer Wall. Which was very helpful. Check them out for more info on Elastic Videos w/ CSS.
For a working example, check out this post on Childish Gambino. Let me know if this worked out for you!
Cheers
– John

Pingback: Responsive Youtube Embeds | electblake
Pingback: Youtube and Vimeo responsive - elastic videos with CSS and HTML | Useful Mix
Pingback: Mobile Tips for Embedded HTML5 Video Streaming | Template Monster Blog
Pingback: Munzir Rosdi – Responsive Youtube Embed
Pingback: Responsive Videos - Tobias Scheible
Pingback: Responsive Youtube Embedded videos « Wiki
Pingback: Responsive Websites: Making Video and other iFrame Embeds Responsive » CapeLinks Blog