Responsive iframes

Building responsive iframes can be frustrating. Don't let one break your beautifully laid out responsive site. Learn how to build responsive iframes the right way with just CSS using the intrinsic ratio technique to create an aspect ratio box.
Typical Read
8 minutes
Sponsor my site by donating to help support open-source projects, like WordPress Zero Spam, Referrer Analytics & others.
Find your next web developer job
jobs by Indeed

Responsive iframes was originally published on Mar 19, 2014 and has recently been updated on Aug 6, 2020 to reflect emerging trends.

Learn to create responsive iframes with clean & simple CSS by making an aspect ratio box using intrinsic ratios. Transform any iframe to become responsive that’ll keep its aspect ratio when resized.


.iframe-container {
  overflow: hidden;
  // 16:9 aspect ratio
  padding-top: 56.25%;
  position: relative;

.iframe-container iframe {
   border: 0;
   height: 100%;
   left: 0;
   position: absolute;
   top: 0;
   width: 100%;
<div class="iframe-container">
  <iframe src="//" allowfullscreen></iframe>

Articles like Responsive iframes delivered to your inbox.

…and don’t worry, I hate spam as much as you. Expect to hear from me at most once a week.

Step-by-Step Guide to Responsive iframes

Building responsive iframes is a wright of passage in the web development world. The problem, iframes set to 100% become fluid, not responsive. The aspect ratio doesn’t stay the same since only the width scales. 

So how do you make an iframe responsive? It’s a common question with a simple CSS-only solution. Use the intrinsic ratio technique to create an aspect ratio box. Check out the example below.

How to make an iframe responsive?
  1. Step 1: Get the iframe embed code and paste into your document
  2. Step 2: Set the height & width attributes to 100% (optional, can be handled via CSS)
  3. Step 3: Absolute position the iframe within a container
  4. Step 4: Add padding-top to the container

Don’t forget to lazy-load your iframes. In addition to making your iframes responsive, you’ll want to lazy-load them using the loading attribute. This improves page load times, enhances the user experience, and increases your search engine rankings. Learn more about how to lazy-load iframes.

This example demonstrates how to embed a responsive (16:9) YouTube video using an iframe snippet. It creates an aspect ratio box that will resize proportionally based on screen size. Try it out by resizing your browser window.

Example Aspect Ratios

padding-top: 56.25%; /* 16:9 aspect ratio */
padding-top: 75%; /* 4:3 aspect ratio */
padding-top: 66.66%; /* 3:2 aspect ratio */
padding-top: 62.5%; /* 8:5 aspect ratio */
padding-top: 100%; /* 1:1 aspect ratio */

What’s an aspect ratio? An aspect ratio of an element describes the proportional relationship between its width and its height. Two common video aspect ratios are 4:3 (the universal video format of the 20th century), and 16:9 (universal for HD television and European digital television, and for YouTube videos).

The Magic of Responsive iframes

The secret sauce to making iframes responsive is something called the intrinsic ratio technique (aka. an aspect ratio box).

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. Yet making the height resize accordingly can be tricky.

So how do you keep from blowing your top trying to make an iframe responsive? Hint: it doesn’t require any JavaScript, just a simple CSS technique!

Native responsive iframes are coming! There is the experimental intrinsicsize attribute that I could imagine being quite nice for iframes in addition to images. Plus the aspect-ratio in CSS which could default to use the width and height attributes on the element, which I hope would extend to iframes.

The Intrinsic Ratio Technique (aka. aspect ratio box)

Aspect ratio box to create responsive iframes
For the visual learner, here’s how creating responsive iframes work. We have set a default padding-top here, but in reality, the ratio of the sizing of an embed change per provider and even per embed. We need to calculate the padding-top and then add this as a style to the responsive wrapper.

In Resize Videos Proportionally, I went over how to use the intrinsic ratio technique to make your embedded videos responsive. We’ll use that same method to create an aspect ratio box 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.

Do not use JavaScript to make iframes responsive. I cringe every-time I see someone using JS when a simple CSS solution exists — even if it’s “light-weight”, it’s not needed. Worse, they often have issues with cross-browser compatibility & bugginess. The intrinsic ratio technique is a much simpler way to implement cross-browser compliant responsive iframes.

In-depth Guide to Making iframes Responsive

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.

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="iframe-container">
  <iframe src="//" allowfullscreen></iframe>

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

.iframe-container {
  overflow: hidden;
  padding-top: 56.25%;
  position: relative;

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

/* 4x3 Aspect Ratio */
.iframe-container-4x3 {
  padding-top: 75%;

IMPORTANT: Don’t forget to apply an aspect ratio class to your iframe. If you don’t, it could cause them 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:

Highlights to Making iframes Responsive

  • It’s key to specify the containers position to be relative. This allows us to absolute position the iframe within it, which is needed to make it responsive.
  • The padding-top 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 site’s 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.

Using a CSS Framework?

A lot of projects now will use some kind of CSS framework like Bootstrap, Foundation or Materialize to help with keep styling uniform throughout the project. Some frameworks already have classes that will do exactly the same as what’s above. Check out some of the examples below.

Responsive iframes in Bootstrap

Bootstrap 3.2+, use the predefined class .embed-responsive and an aspect ratio class modifier like .embed-responsive-16by9 (more listed below). This aspect ratio modifier will add the padding-top with different percentages depending on the given modifier class, then give your iframe the .embed-responsive-item class.

<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="" allowfullscreen></iframe>

Other responsive iframe ratios in Bootstrap:

  • .embed-responsive-21by9
  • .embed-responsive-16by9
  • .embed-responsive-4by3
  • .embed-responsive-1by1

You can, of course, create your own modifier class:

.embed-responsive-10by3 {
   padding-top: 30%;

Responsive iframes in Materialize

If you are using Materialize CSS, then you don’t need your own classes either. Just add the .video-container class to your wrapper:

<div class="video-container">
  <iframe src="" frameborder="0" allowfullscreen></iframe>

Responsive iframes in Foundation

<div class="responsive-embed">
  <iframe src="" frameborder="0" allowfullscreen></iframe>

Aspect ratio modifier classes are set in your $responsive-embed-ratios map in your Foundation settings file:

$responsive-embed-ratios: (
  default: 16 by 9,
  vertical: 9 by 16,
  panorama: 256 by 81,
  square: 1 by 1,

So you’re determined to use 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 & save the aspect ratio for all iframes
$iframes.each(function () {
  $( this ).data( "ratio", this.height / this.width )
    // Remove the hardcoded width & height attributes
    .removeAttr( "width" )
    .removeAttr( "height" );

Responsive iframes are awesome.

Say Goodbye to embedded content breaking your layouts with the intrinsic 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:

Did you find Responsive iframes useful? Get articles in your inbox.

…and don’t worry, I hate spam as much as you. Expect to hear from me at most once a week.

Latest Job Postings
Posted on Aug 9, 2020 at 9:17pm
Los Angeles
Posted on Aug 9, 2020 at 9:15pm
Los Angeles
Posted on Aug 9, 2020 at 1:19am
Littleton, CO
jobs by Indeed
Do you run a development agency, freelance developer or offer website services. Contact me today for information about advertising.
Need help with website, a boost in ranking or online marketing? Contact me today for a free quote.
Do you have a great article to share you want to contribute? Contact me about your idea.
Do you have a great article to share you want to contribute? Contact me about your idea.
Help support open-source projects & pro-bono non-profit services by donating today.

149 Comments on “Responsive iframes”

# Sep 8, 2016

These tricks worked perfectly, thank you so much ! 🙂

# Aug 28, 2016

Thank you! Nothing except your “Proportionally Resize iframes Using JS” have helped me!


# Aug 23, 2016

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?

# Oct 25, 2016

Height needs to be zero. You need to apply the padding styles for height.

Joe Ryan Stephens

# Aug 22, 2016

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.

# Aug 18, 2016

I was able to get the iframe to be responsive but the contents in the iframe are not.

# Oct 25, 2016

That’s because that URL you’re loading isn’t responsive. The page you’re loading also needs to be responsive for it to work.

# Aug 12, 2016

So if I’m using a service like Website Builder with Godaddy (I’m not a website coder), I have to have access to CSS, which I don’t. Any other way to do it?

# Aug 8, 2016

Thank you Ben ! The article was of great help !

# Aug 4, 2016

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.

Piet Konings

# May 9, 2017

I’m having the same question..

# Jul 27, 2016

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.


# Jul 13, 2016

Hello! This is wonderful idea! Thank you very much! 😀

# May 29, 2016

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

Jim Krill

# May 15, 2016

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

# May 16, 2016

Thanks for the catch, updated!

# May 8, 2016

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!


# Jul 14, 2016

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

# Sep 23, 2016

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….

# Sep 26, 2016

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.

# Sep 26, 2016

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.


# Apr 4, 2016

Works great Ben, thank you for sharing this solution.

# Apr 9, 2015

I followed this example exactly, including your example frame and the frame also disappears for me.

# May 20, 2015

Be sure to include the padding class if following this example word by word. If no padding-bottom exists on the container, it won’t show.


# Apr 9, 2015

It is helpful!!
You all are try this one also:-

# Mar 24, 2015

I had the same issue – my videos disappeared trying your code. This example worked for me:

# Feb 16, 2015

Hey, thanks for this information, solved the challenge I was faced with. I appreciate your taking the time to post.


# Dec 3, 2014

i had the same problem. added the div and it disappeared.

also tried the js code and it didn’t work. i’d love to get this to work.


# Feb 3, 2015

Most likely you’ve iframe is inheriting a style it shouldn’t have. I’ve included an example in the post to see how it works.

# Nov 6, 2014


after adding div to my page and code to css file, the iframe no longer appears 🙁 can you please advise? thank you!

# Nov 18, 2014

Can I see the code you’re using?


# Jan 15, 2015

Very nice article . Thanks a lot for putting this up.

# Feb 3, 2015

I had the same issue as marcin and changed height of div from 0% to 100% and then it worked perfectly.


# Jul 5, 2017

Had the same issue as marcin. Your solution solved the problem. Thanks Sameer!

Leave a Reply

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

All comments posted on 'Responsive iframes' are held for moderation and only published when on topic and not rude. Get a gold star if you actually read & follow these rules.

You may write comments in Markdown. This is the best way to post any code, inline like `<div>this</div>` or multiline blocks within triple backtick fences (```) with double new lines before and after.

Want to tell me something privately, like pointing out a typo or stuff like that? Contact Me.