Responsive embeds

November 28, 2011

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

Tags:, , , , , , ,

Categorised in: ,

Written by Anders M. Andersen

  • http://twitter.com/aloysiusbe VP

    you rock– love pure css techniques

  • http://twitter.com/aloysiusbe VP

    you rock– love pure css techniques

  • bob

    Thanks. That’s awesome.

  • bob

    Thanks. That’s awesome.

  • camsjams

    This doesn’t work on the latest version of Firefox, or pretty much any of them after about 13. Has anyone figured out a solution for FF?

  • bob

    Works for me on FF 17 and 16. I haven’t tested it on other versions of FF, but it should work fine.

  • camsjams

    May I ask what platform – I am on Windows 7, Firefox 17, Flash 11.3.

    You’re not getting the issue where the browser window cannot be resized without the “stutter” or “stuck size” issue?

  • bob

    FF on linux. Flash version 11.2

    I haven’t done any IE testing on the site for which I’m using this technique. I’ll be doing this later today.

  • camsjams

    Ah I found it – it seems that this issue only occurs in Firefox with Flash 11.3. I upgraded to 11.5 and it works correctly now.

  • camsjams

    Ah I found it – it seems that this issue only occurs in Firefox with Flash 11.3. I upgraded to 11.5 and it works correctly now.

  • Mariosa.it

    Thanks a lot. Perfect instructions!

  • Mariosa.it

    Thanks a lot. Perfect instructions!

  • Duncan

    Superb article, you made this dead simple to follow. Big ups!

  • Duncan

    Superb article, you made this dead simple to follow. Big ups!

  • http://twitter.com/LouLouGarcia Louis Garcia

    Thanks for the great examples!

  • http://twitter.com/LouLouGarcia Louis Garcia

    Thanks for the great examples!

  • http://twitter.com/jayrizzi1 Jay Rizzi

    really incredible, thank you, bailed me out on a google calendar embed i couldnt figure out

  • http://twitter.com/jayrizzi1 Jay Rizzi

    really incredible, thank you for bailing me out of a google calendar embed I was banging my head with

  • Pingback: Responsive web design e oggetti multimediali | Arch. Mattia Frigeri

  • Chris

    Silly question, but would if I wanted to take up only 80% of the screen and wanted it centered?

    The easy answer is to put a dive around it and center the div, but this is not working for me. Any thoughts?

    Chris

  • eschelar

    Didn’t work for me. All videos were 300×150 due to having removed the width and height. I tried a few other ways of adding width and height, but no deal.

    Oddly, the code works ok when I view this page.

    I’m using Joomla and Yootheme.

© 2013 Copyright.