Making Embedded Content Work In Responsive Design


A few HTML elements don’t play nice with responsive layouts. One of these is the good ol’ iframe, which you may need to use when embedding content from external sources such as YouTube.

In this article, we’ll show you how to make embedded content responsive using CSS, so that content such as video and calendars resize with the browser’s viewport. For those occasions when non-coders will be embedding video on your website and you don’t want to rely on them adding extra markup, we’ll also look at a solution that uses JavaScript instead of CSS.

(You can see a demo of these techniques1 on my blog.)

Note: This technique was originally detailed in Thierry Koblenz’s excellent tutorial ‘Creating Intrinsic Ratios for Video2‘. I’ve used techniques I learned from his tutorial and expanded on them here for additional content types such as calendars.

The Markup For Embedded Content

Services such as YouTube provide code that you can copy and paste into your own website to embed content. I tend to recommend to my clients that they host video with YouTube because it will save them server space and, regardless of the user’s browser or device, YouTube will display the video correctly. The two main ways to embed video on a website are the HTML5 video element, which doesn’t work in legacy versions of Internet Explorer, and Flash, which doesn’t work on iOS devices and isn’t standards-compliant.

When you embed content from an external source, the code will include an iframe:

<iframe src="" height="315" width="560" allowfullscreen="" frameborder="0"></iframe>

This iframe enables external content to be displayed on your website, because it includes a URL that points to the source of the streamed content.

However, you’ll notice that our iframe includes width and height attributes. Remove these and the iframe will disappear because it would have no dimensions. And you can’t fix this in your style sheet, unfortunately.

The width attribute means that, on a screen narrower than 560 pixels, the embedded content will protrude outside of its containing element, breaking the layout. In the example below, I’ve added the code above to a page of my blog. The screenshot is taken from an iPhone in portrait mode (320 pixels wide), and the rest of the page has been shrunk so that the embedded content fits the screen. Far from ideal!

Video breaks the layout in a responsive website on iPhone3
Screenshot taken from an iPhone in portrait mode (320px wide). (Larger view4)

Fortunately, there is a way around this using CSS. First, I’ll show you how to do this with embedded video, and then with calendars.

Responsive Video

The Markup

To make embedded content responsive, you need to add a containing wrapper around the iframe. Your markup would be as follows:

    <iframe src="" height="315" width="560" allowfullscreen="" frameborder="0">

The next step is to add styling to this new wrapper and the iframe within it.


First, we style the containing wrapper with the .video-container class. As proposed by Thierry Koblentz5 in his ALA article “Creating Intrinsic Ratios For Video”6, we can use the following snippet in our style sheet:

.video-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px;
    height: 0;
    overflow: hidden;

This does a few things:

  • Setting the position to relative lets us use absolute positioning for the iframe itself, which we’ll get to shortly.
  • The padding-bottom value is calculated out of the aspect ratio of the video. In this case, the aspect ratio is 16:9, which means that the height will be 56.25% of the width. For a video with a 4:3 aspect ratio, we set padding-bottom to 75%.
  • The padding-top value is set to 30 pixels to allow space for the chrome — this is specific to YouTube videos.
  • The height is set to 0 because padding-bottom gives the element the height it needs. We do not set the width because it will automatically resize with the responsive element that contains this div.
  • Setting overflow to hidden ensures that any content protruding outside of this element will be hidden from view.

We also need to style the iframe itself. Add the following to your style sheet:

.video-container iframe {
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;

This targets iframes inside containers with the .video-container class. Let’s work through the code:

  • Absolute positioning is used because the containing element has a height of 0. If the iframe were positioned normally, we would have given it a height of 0 as well.
  • The top and left properties position the iframe correctly in the containing element.
  • The width and height properties ensure that the video takes up 100% of the space used by the containing element (which is actually set with padding).

Having done this, the video will now resize with the screen’s width. Here’s how it will look on a desktop:

Responsive video as seen on the desktop (in the flow of the content, fitting nicely)7
Desktop screenshot of the video resizing the screen’s width. (Larger view8)

And here’s how it will look on a screen that is 320 pixels wide:

Responsive video fits into the layout on a 320px wide screen9
The video on a 320 pixels wide screen. (Larger view10)

Let’s move on to other sources of embedded content — specifically, Google calendars.

Responsive Calendar

The Markup

The CSS to make any form of embedded content responsive is essentially the same, but different content will have different aspect ratios, which means you’ll need to set the padding-bottom value accordingly.

Below is a screenshot of a website that I manage for a primary school, a website that embeds a Google calendar. As you can see, the calendar breaks the layout on a small screen. In this case, the website is displayed at the correct width, but the calendar goes beyond the screen’s width.

A celandar as seen on a responsive website on an iPhone - not all of the calendar is visible11
The calendar breaks the layout on a small screen. (Larger view12)

The markup for the embedded calendar is as follows:

<iframe src=";wkst=1&amp;bgcolor=%23FFFFFF&amp;;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>

To make a calendar responsive, add a div with a class of .calendar-container to contain the iframe:

    <iframe src=";wkst=1&amp;bgcolor=%23FFFFFF&amp;;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no">

The next step is to style this div.


The CSS for a calendar is almost identical to the CSS for a video, with two exceptions: The aspect ratio will be different, and padding-top isn’t needed.

Add the following to your style sheet:

.calendar-container {
    position: relative;
    padding-bottom: 75%;
    height: 0;
    overflow: hidden;

In this case, the iframe is 800 pixels wide and 600 pixels high, which gives us an aspect ratio of 4:3. So, set padding-bottom to be 75%.

Having done this, we need to apply the same styling to the iframe element in this new container:

.calendar-container iframe {
    position: absolute;
    left: 0;
    width: 100%;
    height: 100%;

This is exactly the same styling that we applied to the video.

Now, the calendar will resize with the browser window, as shown here in Opera Mobile on an Android phone:

A responsive calendar fits into the width of a 320px wide screen

As long as you remember to wrap your embedded calendars and videos with the appropriate containing element, then this CSS will work for any new videos and calendars that you add to your website.

The problem, however, is that although you can fit the whole calendar on a page, it’s still almost unusable because click targets are so small and no information is visible. If you absolutely have to display Google Calendar, you can, but if you can use more usable responsive calendar solutions like simple CSS (setting display: block on table rows, for example), or w3widgets Responsive calendar13 or Calendario14 for your own calendars, your users might appreciate it.

Responsive Video With CSS or JavaScript

If you’re developing a responsive website using a content management system, then one or more of the website’s editors will probably have to embed video at some point. You can point your editors to EmbedResponsively.com15 which generates responsive <embed> codes for embedding rich third-party media with one click, with CSS alone. Alternatively, you could use a JavaScript solution, to relieve nervous editors from having to add extra CSS and markup. However, as long as you can avoid this path, the better, of course.

Until recently, most solutions were plugins, which are OK to an extent but can have performance issues. A popular plugin is FitVids.js16, developed by Chris Coyier and Paravel.

A more current solution is to use just a script — such as FluidVids.js17, developed by Todd Motto. FluidVids.js is simple to use18:

  1. Download the script19 (ZIP) from GitHub and upload it to your server with the same folder structure that the downloaded files come in. This will place the script itself in a folder named dist.
  2. Call the script in each page’s <head> section with the following code:
<script src="dist/fluidvids.js></script>

That’s all you need to do to make videos resize on all devices that support JavaScript. It works not only for YouTube, but for Vimeo, too. The problem, however, is that if you users don’t have JavaScript support or the JavaScript hasn’t loaded yet or JavaScript hasn’t loaded correctly, the only fallback you can use is to add the following to your style sheet:

iframe {
    max-width: 100%; 

This will ensure that video resizes to the width of the browser’s window. But it won’t resize the video’s height; unfortunately, iframe just doesn’t work this way. So, the video won’t break your layout, but it won’t look very good either. This is not really a good option, so if you can avoid using JavaScript for videos, it’s a good idea to do so.

Responsive Google Maps

Apart from videos and calendars, another common issue is the embedding of Google maps, responsively. Basically, we again use the same intrinsic ratio technique, and when setting padding-bottom for the wrapper, we just divide the height by width and add the aspect ratio in CSS.

Usually, the code generated by Google Maps would look like this:

<iframe src="!1m14!1m8!1m3!1d3022.260812859955!2d-73.990184!3d40.756288!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xb134c693ac14a04c!2sThe+TimesCenter!5e0!3m2!1sen!2suk!4v1393485725496" width="500" height="450" frameborder="0" style="border:0"></iframe>

We just wrap a div around the iframe and apply the familiar CSS styling to it:

.google-maps {
    position: relative;
    padding-bottom: 90%; // (450 ÷ 500 = 0.9 = 90%)
    height: 0;
    overflow: hidden;
.google-maps iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

And this is what the markup will look like:

<div class="google-maps">
<iframe src="!1m14!1m12!1m3!1d7098.94326104394!2d78.0430654485247!3d27.172909818538997!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sen!2s!4v1385710909804" width="500" height="450" frameborder="0" style="border:0"></iframe>

Voilá! Again, we can just use EmbedResponsively20 to generate the copy-paste-code with one click.


Embedded content has a habit of breaking responsive layouts, because it’s contained in an iframe with a fixed width. In this article, we’ve seen how to add a single containing wrapper, and some CSS, to ensure that all embedded content contained in an iframe resizes with the browser’s window.

Sometimes it’s good enough, but sometimes you might need to come up with more advanced solutions, since resizing isn’t always a solution. We’ve also looked at embed code generators and alternative solutions, using JavaScript, which might be necessary in some cases, especially if editors have to deal with many videos and don’t have technical skills required, or CMS doesn’t allow for inline styling.

How do you embed third-party content on your responsive websites? Have you discovered any particular tricks or techniques? What does your workflow for embedding such content look like?

(al, vf, il)

Front page image credits: Juhan Sonin21.


  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21

↑ Back to top Tweet itShare on Facebook

  1. 1

    Perfect! I just used the CSS solution for Google Maps. Made a few adjustments (reduced padding on the bottom, added a max-height ) and everything looks great and responds correctly.

    Thank YOU!

  2. 52

    Thanks for this – REALLY helpful as I was trying to find a solution for implementing interactive panoramic images within WordPress – and with a few adjustments to what you provided, found a very usable solution that works across all platforms I’ve tested so far – thanks so much!

  3. 103


    I found this article on my first search and man do I feel lucky. It was exactly what I needed. I can’t say I understood any of it, but I just copied it to my style sheet and added the container and class. It just worked.

    Now I need to study it and try to understand why it worked.

    Thank you!!!!!

  4. 154


    Does the method for embedding responsive Google Calendars still work as described above? My calendar looks fine on the computer, expands vertically (endlessly) on tablets and looks like the “before” version on the phone.

    I’ve tried your method but perhaps Google has made some changes?



  5. 205

    Question: using bootstrap 3 which has a similar solution baked in, to import a form via iframe, all works great. However is there a way to change the value of padding-bottom set on the class of .embed-responsive, once the form has submitted and returned a Thank You notice. As it stands once the form has submitted the iframe and there fore the page it is in, is left with loads of space which was previously occupied by the form. There maybe other ajax solutions etc, but for now I am stuck using an iframe, is it possible at all to determine the padding of the containing div based on a condition within the iframe. Ideas?

  6. 256

    Nice article. Finally found something I was long looking for. I wanted to know is there a way to implement this technique for HTML data tables. I use responsive WordPress themes and these HTML tables breaks the responsive layout. And I don’t like responsive tables too.

    I want to get the tables reduced to smaller size when viewed on small resolutions.

    Please help.


  7. 307

    Very helpful. Thanks a bunch!

  8. 358

    For the iframe topic, it didnt work for me as explained above due to the divclass missing in the HTML. You must add tag at the beginning of the iframe code.

  9. 409

    It is helpful indeed! You all can try this one also:

  10. 460

    Nice article. I wrote an introduction to responsive web design for embedded content if anyone is eager to learn more:

  11. 511

    Great article Rachel,

    Do you think you could add how this would be done if doing a Tableau visualization? I am working from a Tableau public viz at:!/vizhome/AlbertaIncomeTaxes3/Story1

    With the link:

    I believe the width is 1020px and height of 1033px. Thanks.

  12. 562

    Thks for helps

  13. 613

    This is great – thank you! Very simple to implement.

  14. 664

    Thank you Rachel. This was very clearly written, as opposed to some other solutions out there.

  15. 715

    This one really works nice! I have one issue though.

    Using the YouTube parameter ?rel=0 (don’t show related videos) seems to not work while using this CSS. Any ideas or workarounds?

  16. 766

    Philip Oakley

    May 3, 2015 8:39 pm

    Thanks Rachel really useful post really helped embedding my Google map.

  17. 817

    Great article, I’m late to the party but it is timely for me LOL.

    Wondering if anyone has ever done responsive with Adobe EchoSign document signing?

    Going to be testing that out shortly.


  18. 868

    I am a huge newbie at this so please bare with me on this question. I have a web site that has a parallax effect to it. I have video embedded straight into the php file of a specific section am I doing that right? Also where exactly does the wrapper code go in css if I want the video in a specific part of the site? can it just go any where? Thanks in advance if you answer!

  19. 919

    Thanks for the trick, it’s awesome, worked for me !

  20. 970

    I am trying to use this in my wordpress blog. I added the code to my theme’s style.css but it doesnt wokr. My iframes are still extended and chopped off in mobile devices.

  21. 1021

    Correction – it seems like the iframe is responsive but the content inside is not.


Leave a Comment

Yay! You've decided to leave a comment. That's fantastic! Please keep in mind that comments are moderated and rel="nofollow" is in use. So, please do not use a spammy keyword or a domain as your name, or else it will be deleted. Let's have a personal and meaningful conversation instead. Thanks for dropping by!

↑ Back to top