To make an iframe responsive, set its width to 100%. For embeds that keep a fixed ratio, such as videos and maps, also use the CSS aspect-ratio property with height: auto. The browser then calculates the height from the available width. Content whose height changes needs a script that talks to the frame, as explained in step 4.
An iframe has a default size of 300 by 150 pixels, so it ignores the layout around it unless you tell it otherwise. This guide walks through the steps in the order you apply them. Each step has code you can paste and a quick way to check it.
How an iframe sizes itself
An <iframe> (inline frame) is a replaced element. The browser reserves a rectangle and loads another document inside it. That inner document has its own viewport, which equals the rectangle. The width and height attributes or CSS size this rectangle. The embedded page cannot resize it by itself. Every technique below is a way of sizing that rectangle from the parent page.
Unlike images and the native HTML video element, an iframe does not scale on its own. Also remember that the content inside the frame has to be responsive too. A fixed-width page inside a fluid frame still sticks out or scrolls.

Step 1: Start with clean markup and a fluid width
Begin with a simple embed code and keep the width and height attributes. They give the browser a ratio to reserve space before the content loads, which limits layout shifts (content jumping while the page loads).
<iframe
class="embed"
src="https://www.example.com/embed/video"
width="560"
height="315"
title="Product demo video"
loading="lazy"
allowfullscreen>
</iframe>
Now make the width fluid with CSS:
.embed {
width: 100%;
max-width: 100%;
border: 0;
}
The title attribute matters for accessibility. Give every iframe an accurate, descriptive title so screen reader users know what the frame contains.
How to check: narrow your browser window. The iframe should shrink with its parent element and never cause a horizontal scrollbar. At this point the height is still fixed, so the frame looks stretched or cropped. Step 2 fixes that.
Step 2: Use the CSS aspect-ratio property
An aspect ratio describes the proportional relationship between the width and the height of an element. Common ratios are 4:3 and 16:9, the usual ratio of YouTube videos. The aspect-ratio property tells the browser to keep that ratio when the box scales. With height: auto, the height follows the width automatically.
.embed {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
border: 0;
}
Use another ratio when the content requires it. For example, use 4 / 3 for older slides, or 1 / 1 for a square map. The property is supported by current versions of the major browsers. It is the cleanest method for new projects, and it needs no wrapper element.
How to check: open the developer tools and resize the viewport. Confirm that the computed height always equals the width multiplied by 9/16.
Limit the maximum size
On large desktops a full-width video can become oversized. Wrap the frame in a container and cap it:
.embed-wrap {
max-width: 800px;
margin-inline: auto;
}
The 800 pixels here is only a common starting point. Choose the value that fits your layout.

Step 3: Add the padding-top wrapper as a fallback
Before aspect-ratio existed, developers used the intrinsic ratio technique. A wrapper div gets a percentage of padding, which is always calculated from the width of the parent element. The iframe is then placed over that space with absolute positioning. Keep this method only if you must support very old browsers.
<div class="ratio-box">
<iframe src="https://www.example.com/embed/video"
title="Product demo video" allowfullscreen></iframe>
</div>
.ratio-box {
position: relative;
width: 100%;
padding-top: 56.25%; /* 9 / 16 = 0.5625 */
overflow: hidden;
}
.ratio-box iframe {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
border: 0;
}
The percentage is the height divided by the width. It is 56.25% for 16:9 and 75% for 4:3. Many tutorials use padding-bottom instead of padding-top, and both give the same result.
The wrapper is the parent element with relative positioning. The iframe is its child element, positioned absolutely to fill the area. Some versions of the technique use a pseudo-element to push the wrapper to the right height. If the content inside is taller than the ratio allows, that version can still grow instead of overflowing.
You can also keep the fallback and the modern rule together with a feature query. A feature query is a CSS rule that applies only when the browser supports a given property:
@supports (aspect-ratio: 1 / 1) {
.ratio-box {
padding-top: 0;
aspect-ratio: 16 / 9;
}
}
How to check: the wrapper must keep its height while the frame is still loading. The video must fill it without black bars.
Ready-made options
If you prefer not to write the CSS yourself, there are shortcuts. Bootstrap, a popular CSS library, provides a .ratio class with ratio classes such as ratio-16x9. Wrap the iframe in a parent element with those classes and the immediate child is sized automatically. Small scripts such as FitVids do the same job for videos, at the cost of loading extra JavaScript. Online generators can also output the wrapper code for you.
Embeds you will meet most often
Most embeds follow one of the patterns above.
- YouTube and other videos: use a 16 / 9 ratio, unless the video was recorded in another format.
- Google Maps: use
aspect-ratiofor a fixed shape. You can also give the map a height that suits your layout, since a map needs no exact ratio. - Google Calendar and forms: their height depends on the content, so use the postMessage method in step 4 if the provider supports it. Otherwise set a generous height and let the frame scroll.
- Ads and widgets: browsers may enforce a minimum frame height, so a padding-based wrapper can behave oddly. Test these carefully.
Step 4: Use postMessage when the content height changes
A fixed ratio suits videos, but not a form, a calendar or an article whose height depends on its text. For these, the embedded page measures itself. It then sends the height to the parent with postMessage, the browser API for safe messages between windows.
Why cross-origin matters
An origin is the combination of domain, protocol and port. If the iframe comes from another origin, the same-origin policy forbids the parent from reading the frame’s content or height. You cannot measure it from outside. You also cannot fix that on a third-party page you do not control. The embedded page must cooperate by sending the message. Only same-origin frames can be measured directly with iframe.contentWindow.document.body.scrollHeight.
Code inside the embedded page
function sendHeight() {
const height = document.documentElement.scrollHeight;
window.parent.postMessage(
{ type: "iframe-height", height: height },
"https://www.your-site.com"
);
}
window.addEventListener("load", sendHeight);
new ResizeObserver(sendHeight).observe(document.body);
The second argument is the target origin. Name your real parent site instead of "*", so the data goes only where you intend. ResizeObserver is a browser feature that calls your function whenever an element changes size.
Code in the parent page
const frame = document.querySelector("#dynamic-frame");
window.addEventListener("message", function (event) {
if (event.origin !== "https://widgets.example.com") return;
if (event.source !== frame.contentWindow) return;
const data = event.data;
if (data && data.type === "iframe-height" && Number.isFinite(data.height)) {
frame.style.height = data.height + "px";
}
});
The origin check is the important part. Without it, any other window could send a message and change your layout. Also validate the type of data, as shown. Never insert received strings into the page as HTML.
How to check: add content inside the frame and confirm that the height updates without an inner scrollbar. Log event.origin once to see the exact value to allow.

Step 5: Test on mobile
Use the device toolbar of your browser’s developer tools to try common small screen widths. Then confirm on a real iPhone and an Android phone. Touch scrolling and viewport units can behave differently on iOS. Make sure the page has this tag in its head:
<meta name="viewport" content="width=device-width, initial-scale=1">
Check these points:
- No horizontal scrolling at any width.
- Buttons and links inside the frame remain large enough to tap.
- The frame does not trap scrolling, so users can move past it with a finger.
- Fullscreen works for videos when
allowfullscreenis present.
If a frame needs a different shape on small screens, use a media query. For example, switch a 16 / 9 frame to 1 / 1 under a chosen screen width when the embedded content stacks its layout on phones.
Common problems and fixes
| Symptom | Cause | Fix |
|---|---|---|
| Frame stays 300 by 150 pixels | No CSS, default size applies | Apply width: 100% and aspect-ratio |
| Frame overflows the screen | Fixed width attribute wins over a wider container |
Add max-width: 100% |
| Double scrollbars | Height smaller than the content | Send the height with postMessage |
| Height ignored in the wrapper method | Iframe is not absolutely positioned | Set position: absolute and inset: 0 |
| Page refuses to load in the frame | The source site blocks embedding with X-Frame-Options or a CSP frame-ancestors rule |
Use an official embed URL or ask the site owner to allow your domain |
Questions readers often ask
What is responsive design in web design?
Responsive design is an approach where a page adapts its layout to the screen, using flexible widths, fluid media and media queries. The goal is one codebase that works on phones, tablets and desktops. Making an iframe fluid is one small part of it.
Is it an iframe or an iFrame?
The HTML element is written <iframe> in lowercase, and “iframe” is the standard spelling in text, short for inline frame. “iFrame” is a common variant in older articles and some product names, but it refers to the same thing. In code, always use the lowercase tag.
How do I create an iframe in HTML?
Add an <iframe> tag with a src attribute pointing to the page you want to show, plus a title for accessibility. For example: <iframe src="https://www.example.com" title="Example page"></iframe>. Then size it with the steps above.
Does anyone use iframes anymore?
Yes. They remain the standard way to embed videos, maps, payment forms, calendars and widgets from another origin, because the frame isolates that code from your page. They are less suited to your own content, where normal templates are easier to style and index.
FAQ
Should I use the sandbox attribute?
For third-party content, yes, it is a good habit. The sandbox attribute restricts what the frame can do, and you then re-allow only what is needed, such as allow-scripts. Test the embed afterwards, since some widgets need extra permissions.
Does lazy loading work on iframes?
Yes, loading="lazy" is supported on iframes in current major browsers. It delays loading until the frame is near the viewport, which helps pages with several embeds. Avoid it for a frame that sits at the very top of the page.
Can I use height: 100vh for a full-screen frame?
You can, for tools or dashboards that should fill the screen. On mobile, the browser toolbar changes the visible height, so consider 100dvh, which follows the dynamic viewport. Test it on a real phone.
Which approach fits you
Choose aspect-ratio for videos and maps. Use the padding wrapper only if you still support very old browsers. Pick postMessage with a strict origin check for forms and widgets that change height. Paste the snippet that matches your embed, then run the mobile checks before publishing.
