Responsive embeds

This article shows examples of how to embed video and other iframes in a responsive web design and has examples with YouTube, Vimeo and Slideshare.

I found a neat little CSS trick on the A List Apart article “Creating Intrinsic Ratios for Video” and I did some experimenting with it. (Yes, I had to look up the word Intrinsic…)

The problem with embed code that you copy from websites like YouTube is that they often contain a fixed width and height in pixels. And that does not work too well in responsive designs. In a responsive design we have a container with a relative width and we want the embedded object to be relative to our container. This is not possible with all embeds as some of them insist on having a fixed width. If you encounter such embeds, the workaround is probably to move some of the code to the server side or javascript and set the width to the pixel value that you think is most appropriate. It will work, but it will not be flexible like the method shown here.

This technique is fully flexible and I have tests below with Vimeo, SlideShare and YouTube (sorry if this post took long to load…). Here are the steps to make it work.

First, you just need to remove the width/hight attributes from the embed code you get:

<!-- original -->
<iframe
src="http://player.vimeo.com/video/32071937?title=0&amp;byline=0&amp;portrait=0"
width="400"
height="225"
frameborder="0"
 webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>

<!-- removed width/height -->
<iframe
src="http://player.vimeo.com/video/32071937?title=0&amp;byline=0&amp;portrait=0"
frameborder="0"
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>

Next step is to wrap this in a container tag so that we can style it:

<div class="embed-container">
    <!-- embed code goes here-->
</div>

Then comes the CSS. First, we style the container:

.embed-container {
	position: relative;
	padding-bottom: 56.25%; /* 16/9 ratio */
	padding-top: 30px; /* IE6 workaround*/
	height: 0;
	overflow: hidden;
}

I’m not going to explain all this in detail, the original article does that better than I can anyway, but this code asumes a 16/9 ratio (56,25%) on the embed. Next step is to style the actual embed. Some embeds serve iframe, object or embed, based on device so we style all three:

.embed-container iframe,
.embed-container object,
.embed-container embed {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

That’s it. Here are some examples:

Vimeo example

<div class="embed-container">
   <iframe
      src="http://player.vimeo.com/video/22669590?title=0&amp;byline=0&amp;portrait=0"
      frameborder="0"
      webkitAllowFullScreen mozallowfullscreen allowFullScreen>
   </iframe>
</div>

Youtube example

<div class="embed-container">
   <iframe
      src="http://www.youtube.com/embed/TFWDUsvLCoE"
      frameborder="0">
   </iframe>
</div>

Slideshare example

<div class="embed-container">
   <iframe
      src="http://www.slideshare.net/slideshow/embed_code/1488558"
      frameborder="0"
      marginwidth="0"
      marginheight="0"
      scrolling="no">
   </iframe>
</div>

PS. There is a jQuery plugin that does the work for you called FitVids.js, but this is so simple and works without JavaScript, so I do not see why we should bring on a JS library in order to get it to work.

UDPATE: I have tested the technique on a few mobile devices, and the technique works good, but the content inside the embed does not work too good… Read more here

UPDATE 2: I have created a fallback solution for devices that does not support the embed by using server side techniques: Responsive embeds + RESS


This entry was posted in Responsive Web Design, Video and tagged , , , , , , , . Bookmark the permalink.
  • http://twitter.com/arnorb Arnor Bogason

    I like this, no javascript needed!

  • http://www.helloper.com Per Sandström

    Nice! Add a Flattr button and I’ll be extra generous this Pay a Blogger day.

  • http://www.helloper.com Per Sandström

    Nice! Add a Flattr button and I’ll be extra generous this Pay a Blogger day.

  • http://www.helloper.com Per Sandström

    Nice! Add a Flattr button and I’ll be extra generous this Pay a Blogger day.

  • http://amobil.se Anders M. Andersen

    Thanks! I’m new to Flattr, but I actually added the button now. And I will head out to Flattr some other good blogs myself now ;-)

  • http://www.helloper.com Per Sandström

    There you go, good sir. :)

  • http://www.facebook.com/people/Jesse-Frye/183500112 James Jesse Frye

    Interesting. I’ll have to play with this. I was doing a responsive site with youtube embeds and had to use javascript to resize the container div according to the video ratio to remove the “black bars” that pad the video. I also had to take the menu bar height into consideration because i believe it is a different size depending on the player.

  • Pingback: Responsive embeds – device testing – Anders M. Andersen

  • Pingback: How to Scale Embedded Media in Responsive Designs | t3knoDorKs

  • Lawrence Botha

    Excellent work, Anders, using this on a site we should have up in the next couple months!

    Will also be using it for responsive speakerdeck presentations!

  • Mike

    Youtube doesnt seem to be working anymore on iPhone. Whenever I click on the video it just doesnt open. Tried this on multiple iPhones, on a video of mine, and also on this page. Any ideas?

  • http://amobil.se Anders M. Andersen

    Hi Mike,

    The only thing I can say is that is probably a YouTube problem. The iframe is self contained and the CSS trick I provided does not interfere with the iframe in any way. It could be a Javascript problem or a YouTube permission problem (I think they block some content for some countries).

    And I just tested on my iPhone and it works so not sure I can help you out…

    -Anders

  • Anonymous

    I’m also using that technique to embed Youtube videos and works brilliantly. A year ago I developed a WordPress plugin called “Youtube shortcode” to embed Youtube videos and recently I added this technique for responsive designs.
    I suggest you visit the plugin website (docs & demos for the plugin): http://www.margenn.com/tubal/youtube_shortcode/

  • Pingback: How to Scale Embedded Media in Responsive Designs | LoQuor IT – Chicago Integrated IT Solutions

  • http://www.thedigitalarmory.com James Korte

    you’re the man. thank you.

  • Pingback: ResponsiveDesigns by paulonpearltrees - Pearltrees

  • Pingback: Easy steps to a mobile-friendly responsive design with an embedded YouTube video with a fluid resize - Scott Hanselman

  • Pingback: Easy steps to a mobile-friendly responsive design with an embedded YouTube video and a fluid resize - Scott Hanselman

  • Pingback: Easy steps to a mobile-friendly responsive design with an embedded YouTube video and a fluid resize « Fast Ninja Blog by Freelanceful – Web Design | Coding | Freelancing

  • Kurt

    Thanks Anders! You helped me wrap up a project!

  • http://amobil.se Anders M. Andersen

    Glad to hear that!

  • http://www.108csr.com wimpykatana

    hey great one thx for sharing….

    you are the man…

  • EC Brown

    Just implemented this. Works like a charm!
    Thank you!

  • Mail

    thx for the tip !

  • Newnoo_19

    Thanks for this, really useful, clever idea to force the aspect ratio!

  • http://www.facebook.com/nordmark Daniel Nordmark

    Thanks for this post, I was actually trying to implement SlideShare embed in a responsive design when I stumbled over your post. Even though I think that my solution would be more or less the same as yours in the end your post saved me some time! :-)

  • Adamball

    Thanks for the help. Just one thing that i cannot get to work. The youtube box on my website is now to small… is there any code that can sort this out .. Thank you in advance

    Adam

  • jk

    Is it possible to embed a responsive vimeo video into wordpress and get auto play and no black bars over and under the video if it panoramic?

  • jk

    Is it possible to embed a responsive vimeo video into wordpress and get auto play and no black bars over and under the video if it panoramic?

  • http://amobil.se Anders M. Andersen

    You should be able to change the

    padding-bottom: 56.25%; /* 16/9 ratio */

    part to fit your aspect ratio. There is nothing here that detects the aspect ratio of the video though so you need to know it up front.

  • ORBITEC sc

    una solución simple y poderosa, solo con CSS´s thanks you so much!!!