Responsive IFrames — The Right Way!

Building responsive IFrames can be frustrating. Don’t let an IFrame break your beautifully laid out responsive site. Learn how to build responsive IFrames the right way with just a few lines of CSS.

You’ve spent countless hours designing and building the perfect responsive site. One problem — iframes. Proportionally resizing (aka. responsive iframes) these pesky little windows to another world can be frustrating. It’s easy enough to make an iframe’s width span 100% of its container. Rather, making the height resize accordingly can be tricky.

So how do you keep from blowing your top trying to make responsive iframes?

Responsive IFrames

Most implementations I’ve seen require the use of JS, like Pym.js. This can cause unexpected behavior depending on the device and browser version — Pym.js only goes back to IE9. Wouldn’t it be nice if there was a way to make responsive iframes without using a dirty little code snippet? There is! It’s name is the CSS intrinsic ratio technique — or what I like to call, ‘Magic iframes!’.

Responsive IFrames Demo

With just a little CSS and HTML, you can build a responsive iframes — even if the dimensions vary! Check out the demo below:

Responsive IFrames Using CSS

In Resize Videos Proportionally, we learned how to use the intrinsic ratio technique to make your embedded videos responsive. We’ll use that same method and apply it to make any iframe, YouTube & Vimeo video or Google Map responsive. Only dependency is you know the aspect ratio (width x height) of the iframe.

When embedding iframes for content such as videos, most services like YouTube and Vimeo will provide you a snippet of code like the one below:

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

Notice I removed the frameborder attribute. If you’re using HTML5, that attribute is no longer supported.

The Code

First of all, remove the width and height attributes. Keeping those attributes forces the content to stay at that size regardless of the screen size. This causes problems in responsive layouts when the screen size is smaller than the width of the iframe. Though we could use CSS to force the size, why have them if their not being used — less code is beautiful code.

<iframe src="//" allowfullscreen></iframe>

Next, let’s add a container with a class around the iframe:

<div class="intrinsic-container intrinsic-container-16x9">
  <iframe src="//" allowfullscreen></iframe>

Now, we add a little touch of CSS magic to make the iframe responsive. Same way we did in the “Resize Videos Proportionally with Intrinsic Ratios”.

.intrinsic-container {
  position: relative;
  height: 0;
  overflow: hidden;

/* 16x9 Aspect Ratio */
.intrinsic-container-16x9 {
  padding-bottom: 56.25%;

/* 4x3 Aspect Ratio */
.intrinsic-container-4x3 {
  padding-bottom: 75%;

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

IMPORTANT: Don’t forget to apply a aspect ratio class to your iframe. If you don’t, it could cause the iframe to disappear.

That it! Simple, huh? Your iframe should now proportionally resize based on the browser size. Here’s a breakdown of how it works:

Breaking It Down

  • It’s key to specify the container’s position to be relative. This allows us to absolutely position the iframe within it, which is needed to make it responsive.
  • The padding-bottom value is calculated based on the aspect ratio of your content. Instead of adding it to the intrinsic-container class, we added separate classes that can be appended to that element depending on the type of content you’re embedding. I prefer doing this so I’m not duplicating the container code for different aspect ratios. To find the aspect ratio of a container, use this formula: height ÷ width = aspect ratio
  • height is set to 0 because padding-bottom gives the iframe it’s height.
  • Using overflow: hidden is important because it ensures if any content does protrude outside of the container, it will be hidden and avoid screwing up the sites layout.
  • Like with most absolute positioned elements, we need to set the top and left properties so the iframe get’s put in the right place.
  • Finally, width and height are set to 100% so the iframe takes up 100% of the containers space.

Using SASS?

If you’re using SASS, use this function to find the ratio or padding-bottom of the parent container:

 * Ratios
 * Returns the ratio for specified dimensions.
@function ratio($width, $height) {
  return percentage( $height / $width);

Taking that one step further, you can create a mixin to generate ratio classes:

@mixin generateRatios($width, $height, $prefix: "ratio-") {
  $class-name: $prefix + $width + "x" + $height;

  .#{$class-name} {
    padding-bottom: ratio($width, $height);
  // Output example: .ratio-16x9 {}

@include generateRatios(16,9); // 16x9
@include generateRatios(4,3);  // 4x3

We can use this same technique to make other types of embedded content responsive like Google Maps & Calendars. Basically, anything that uses a iframe using only CSS! If you don’t have access to edit the site stylesheets directly, here’s a nifty tool that will generate responsive embed codes for you.

Responsive IFrames Using JS

What if you don’t know the aspect ratio? Let’s say you have content authors creating interactives with each having different dimensions. Without knowing the aspect ratio of the iframe, it’s not easy to implement the intrinsic ratio technique.

You can overcome this problem by using JS:

// Find all iframes
var $iframes = $( "iframe" );

// Find &#x26; save the aspect ratio for all iframes
$iframes.each(function () {
  $( this ).data( "ratio", this.height / this.width )
    // Remove the hardcoded width &#x26; height attributes
    .removeAttr( "width" )
    .removeAttr( "height" );

// Resize the iframes when the window is resized
$( window ).resize( function () {
  $iframes.each( function() {
    // Get the parent container&#x27;s width
    var width = $( this ).parent().width();
    $( this ).width( width )
      .height( width * $( this ).data( "ratio" ) );
// Resize to fix all iframes on page load.

Let’s Sum It Up!

Say Goodbye to embedded content breaking your layouts with the intrinic ratio technique. We’ve walked through how just a little bit of code can easily make your iframes and other embedded content responsive friendly.

How do you embed third-party content on your responsive website? Do you have a nifty technique or trick to accomplish responsive embedded content? What about your workflow for embedding content like Google Maps, YouTube, etc? I’d love to hear from you. Comment below with your thoughts.

Additional Resources

Check out these other great articles about making embedded content responsive:

Author: Ben Marshall

Red Bull Addict, Self-Proclaimed Grill Master, Entrepreneur, Workaholic, Front End Engineer, SEO/SM Strategist, Web Developer, Blogger

74 thoughts on “Responsive IFrames — The Right Way!”

  1. I had to view source on your example to get it (the “Don’t forget to apply the styles above to your iframe!” note was what I needed but, not being a programmer, didn’t understand). Anyway, got it, brilliant tutorial, thanks so much!

    1. Miriam, I suspect I am experiencing the same translation challenge. Can you elaborate on what it is that you did to fix the issue?

    2. I still don’t get it.

      From the comment, “Don’t forget to apply the styles above to your iframe!”, I assume there’s something that needs to be done to the line, but I guess I’m even less of a coder than are you, as I don’t even know how to look at the source of his example. I right-clicked on the scrolling frame and selected “View page source”, but I think that’s showing the source for this entire page, not just the scrolling window/frame, and when I search in that source-code for “<iframe", I still don't see any modifications to the "<iframe src=…" line.

      So I still don't have a resized iframe. Me=sad….

      Other than that, this tutorial looks like exactly what I need, if I could just get it to work. Thanks for putting it out there; it may get me closer….

      1. After a weekend, still no success.

        And as I’ve thought about it, as great a work-around as this is, it is a work-around, around a broken-ness that should not exist in the first place.

        should work. If that broken-ness were fixed, we’d be done with it.

        I understand there’s probably a good reason for this behavior (something about iframes not being block-level elements, blah blah blah), but intuitively, height=”100%” should work.

        1. My example code got wiped out by the commenting system (with no warning, and apparently no way to edit my post to fix it – arg!). Without the code, my statement was essentially that using the width=”100%” format should work.

  2. In your generateRatios mixin, you swapped the width/height in the ratio() function. Should be: ratio($width, $height)

  3. Your CSS example worked well for me creating a responsive embed for, which doesn’t use its own responsive script loader (like Thanks!

  4. Nice article!

    I inherrited some iframes that have image maps inside them. These are notoriously mobile unfriendly. I’m looking at some jquery which might solve the problem, but your solution for the iframes is brilliant.


  5. Important: Don’t forget to apply the styles above to your iframe. If you don’t, it could cause the iframe to disappear.

    My iframe disappeared. I’m not sure what you mean by apply the styles to the iframe.

  6. This will not work for my site. I have tried every solution provided but to no avail. Can anyone help with this? I am using Bootstrap.

  7. Great tutorial!
    But the iframe only shows up when I change the height. When the height is 0, the iframe will not display. What can I do about it?

  8. Correction:
    I meant:

    div class=”intrinsic-container intrinsic-container-16×9″
    for 16×9 ratio


    div class=”intrinsic-container intrinsic-container-4×3″
    for 4×3 ratio


  9. This is fine for videos but doesn’t work for embedded iframes that have styles embedded in the iframe like a width on the body tag.

  10. Hi, thanks for a simple solution, works perfect using Youtube film.
    I our case we are hosting the videos at external provider, and a typical ifram code would look like this:
    (messing up code a littlebit so it will be accepted)
    iframe src=”//” frameborder=”0″ allowfullscreen – /iframe
    In this case it’s not responsive, could the poblem be the player or content in some way?

  11. I tried the code, see below but it did not work. What am I doing wrong?

    .intrinsic-container {
    position: relative;
    height: 0;
    overflow: hidden;

    /* 16×9 Aspect Ratio */
    .intrinsic-container-16×9 {
    padding-bottom: 56.25%;

    /* 4×3 Aspect Ratio */
    .intrinsic-container-4×3 {
    padding-bottom: 75%;

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


  12. Possible Error??? You mentioned: “Don’t forget to apply a aspect ratio class to your iframe” I did that and it wouldn’t show up. Then I applied the aspect ratio class to the containing the iframe and it worked. Thanks for the info!

  13. This worked so nicely and it was so easy! I did have to adjust the height here:
    .intrinsic-container {
    position: relative;
    height: 0;
    overflow: hidden;
    but used everything else, as is. Didn’t take long, didn’t bulk up my code. Thank you!

  14. Thank you for posting this. I’ve really struggled with iframes with YouTube videos and other instances with my clients’ websites. Just tested this out with a custom Google map on a new website, and it worked like a champ. This is going straight into my Evernote database!

  15. Has anyone had any luck in getting an iFrame to be responsive with a new CSS Grid element?

    I’ve tried all of the tricks mentioned above and I’m having zero success in getting an iFRAME to work within a CSS Grid element and be responsive.


  16. Thank you very much! The Javascript solution for removing width and height iframe attribute on WordPress is really useful!

  17. Hi Ben

    I am a rank amateur and I want to embed an iFrame which links to a URL. I cannot seem to get this to work for me

    Can you assist?


  18. What if it’s not a YouTube video? What if we don’t know the size/aspect ratio of the content. What if the content is HTML. Then how do we do it?

  19. For those wondering what the extra step is, you have to add in the aspect ratio class. So in addition to div class=”intrinsic-container” you need

    div class=”intrinsic-container intrinsic-container-16×9″

    Hope that makes sense.

  20. @Paul Sorry, a bit noob to responsive coding, but . . . if I wanted an embedded Articulate module to show responsively in whatever viewport size the viewer has, do I have to duplicate the code you mention for both the 16×9 and for the 4×3 aspect ratios?

    Here is what i had based on the article before I read your comment today:

    . . . what would it look like to accomodate both/all aspect ratios?

  21. Hello, I got the code to work and it’s perfect, but I had to alter it. 56.25% produced a way too large window vertically, so I turned it down to 30% and it seems to match the video size.

    Can somebody reassure me that what I did is fine?

    1. Nevermind, turns out if you set a ‘max-width’ for .intrinsic-container it messes with the scaling of a video. What I did only solved my problems temporarily. Now 56.25% works perfect, sorry.

  22. Not sure how I got here except I was searching how to make an iframe responsive but I am not needing a video to be responsive, I need a Google Calendar to be responsive. I have been on website after website, trying different code after code with no results. I am getting very frustrated :-/ I am not a coder and I am doing this for free as a booster member at a high school. I tried the code above and yes, it also made the calendar disappear (as others mentioned) but I thought that might have been because I didn’t set Aspect Ratios, although I tried but not sure what I am supposed to put?? Here is my dilemma:

    1. With the code I have in place currently, it cuts off part of the week and actually, I would like it to show the Agenda view on a phone that way it is easier to read. Is this possible?
    2. The calendar gradually “crawls” once you open the page it’s on? I have changed all kinds of css rules trying to fix/correct this problem with NO luck!
    3. You cannot click on an event on the calendar to view it? It opens and then closes right after, it’s not even on the screen for one second?

    The calendar ALSO “crawls”! And once it starts, you cannot open an event here either unless you do it right as the page opens, but once it starts crawling, you cannot..

    The calendar is WAY too big even though I have set the size to width=”320″ height=”320″.

    Here is the code in my css file that I have applied (at least it doesn’t extend past the border of my page now on a mobile device)

    .responsiveCal { position: relative; padding-bottom: 100%; height: 0; overflow: hidden;
    .responsiveCal iframe { position: absolute; top:0; left: 0; width: 100%; height: 100%;

    I need help! Any code you can give me to fix this problem would be greatly appreciate. Also, if you can tell me where to put the code that would be helpful cause as I said, I am not a coder.

  23. Thanks very much for the guide! Really helpful!
    Do you have any suggestions for making content in iframes responsive (or in any way behave) on Firefox?
    It’s the only browser that doesn’t seem to see the edges of the box/page layout the iframe is in.

Leave a Reply to tommy Cancel reply

Your email address will not be published. Required fields are marked *