The ultimate guide to iframes - LogRocket Blog (2024)

The iframe element (short for inline frame) is probably among the oldest HTML tags and was introduced in 1997 with HTML 4.01 by Microsoft Internet Explorer. Even though all modern browsers support them, many developers write endless articles advising against using them.

The ultimate guide to iframes - LogRocket Blog (1)

I believe that their bad reputation should not prevent you from relying on them; they have many legitimate uses cases. Besides, it’s not that hard to secure them, so you won’t have to worry about your user’s machine becoming compromised.

To help you form your own opinion and sharpen your developer skills, we will cover all the essentials you should know about this controversial tag. We’ll go through most of the features the iframe element provides and talk about how you use them, as well as how iframes can be useful for overcoming some tricky situations. Finally, we’ll talk about how you can secure your iframes to avoid potential vulnerabilities.

What is an iframe, and when do you use it?

Developers mainly use the iframe tag to embed an HTML document within another. You may have crossed paths with it when you had to include a third-party widget (like the famous Facebook like button), a YouTube video, or an advertising section on your website.

For instance, the code below will display a 500px square with the Google homepage within:

<iframe src="https://www.google.com/" height="500px" width="500px"></iframe>

Here is another example in which we display a button to tweet your webpage on Twitter:

<iframe src="https://platform.twitter.com/widgets/tweet_button.html" style="border: 0; width:130px; height:20px;"></iframe>

What you must keep in mind when thinking about iframes is that they let you embed an independent HTML document with its own browsing context. Thus, it will be isolated from the JavaScript and CSS of the parent.

That is one of the valid purposes to use an iframe: to provide a measure of separation between your application and the iframe content. Nevertheless, as you will see in this guide, the separation is not so perfect.

The iframe can still behave in annoying or malicious ways — triggering a popup or autoplaying videos, for instance. To illustrate how this isolation from the JavaScript and CSS is handy, let’s take a look at these two situations.

In an application, a user could create emails and save them as templates. On a particular page, I needed to list them to let the user preview and choose one.

But to prevent the CSS of the current website from affecting the style of these templates, I figured out that using an iframe with the srcdoc attribute was the cleanest solution.

<iframe srcdoc="<html><body>The content you see here will never be affected by the CSS of its parent container. It's supposed to be rendered in black on a white background.</body></html>"></iframe>

The other situation when iframes saved my life was when I had to build a WYSIWYG editor for a customer. But the thing with these editors is that you have to find a way to keep the focus and the selection when the user is clicking on all the buttons across the interface.

Because an iframe offers an isolated environment, this means that the focus or the selection is never lost when you are clicking outside of it. By using communication events between the iframe and the parent (more on how to do so later in this article), I managed to design a powerful editor in a snap.

Over 200k developers use LogRocket to create better digital experiencesLearn more →

The iframe attributes you need to know

To this day, there are eight attributes we can use to customize the behavior or styling of an iframe.

<iframe src="https://google.com/" <!-- Sets the address of the document to embed --> srcdoc="<p>Some html</p>" <!-- Sets the HTML content of the page to show --> height="100px" <!-- Sets the iframe height in pixels --> width="100px" <!-- Sets the iframe width in pixels --> name="my-iframe" <!-- Sets the name of the iframe (mainly used to reference the element in JavaScript --> allow="fullscreen" <!-- Sets the feature policy for the iframe. --> referrerpolicy="no-referrer" <!-- Set the referrer to send when fetching the iframe content --> sandbox="allow-same-origin" <!-- Sets the restrictions of the iframe (more on this below) -->></iframe>

You may find more than the ones listed above, but keep in mind that they are not supported in HTML5 anymore: align, frameborder, longdesc, marginheight, marginwidth and scrolling.

Note that, by default, the iframe element has a border around it. To remove it, you can use the style attribute to set the border CSS property to none:

<iframe src="https://logrocket.com/" style="border: none;"></iframe>

iframe events and communication

Loading and errors

Because an iframe is a document, you can use most global event handlers.

When you are initiating the iframe, two of these event handlers come in handy to improve the experience, like displaying a spinner or a specific message to assist the user:

  1. The load event: This is triggered when the iframe is fully loaded. In other words, all static assets have been downloaded, and all the elements in the DOM tree have fired their load event
  2. The error event: This is triggered when loading fails

You can listen to them with the onload and onerror attribute, respectively:

<iframe src="https://logrocket.com/" onload="onLoad()" onerror="onError()"></iframe>

Or if you can add the listeners to your iframe programmatically.

// For a new iframeconst iframe = document.createElement("iframe");iframe.onload = function() { console.log("The iframe is loaded");};iframe.onerror = function() { console.log("Something wrong happened");};iframe.src = "https://logrocket.com/";document.body.appendChild(iframe);// For an existing iframeconst iframe = document.querySelector('.my-iframe');iframe.onload = function() { console.log("The iframe is loaded");}iframe.onerror = function() { console.log("Something wrong happened");}

Communication with iframes

It is quite easy to send messages between the parent and the iframe. You have to use the postMessage function, which is documented here.

From the parent to the iframe

Send the message from the parent element:

const myiframe = document.getElementById('myIframe')myIframe.contentWindow.postMessage('message', '*');

And listen to it in the iframe:

window.onmessage = function(event){ if (event.data == 'message') { console('Message received!'); }};

From the iframe to the parent

Send the message from the iframe:

window.top.postMessage('reply', '*')

And listen to it in the parent:

window.onmessage = function(event){ if (event.data == 'reply') { console('Reply received!'); }};

N.B., keep in mind that you can end up in some tricky situations when you need to debug something as messages are fire-and-forget (i.e., there is no real error handling).

iframe security

When you are using an iframe, you are mostly dealing with content coming from a third party over which you have no control. Thus, you are increasing the risk of having a potential vulnerability in your application or simply having to deal with a bad user experience (like annoying video autoplay ).

Thankfully, you can block or allow specific features. You have to use the sandbox and allow the attributes we discussed earlier.

Keep in mind that a good rule of thumb is to always grant the minimum level of capability necessary to a resource to do its job. Security experts refer to this concept as “the principle of least privilege.”

The sandbox attribute

Here is the complete list of sandboxing flags and their purposes:

FlagDetails
allow-formsAllows form submission.
allow-modalsAllows the resource to open new modal windows.
allow-orientation-lockAllows the resource to lock the screen orientation.
allow-pointer-lockAllows the resource to use the Pointer Lock API.
allow-popupsAllows the resource to open new popups or tabs.
allow-popups-to-escape-sandboxAllows the resource to open new windows that will not inherit the sandboxing.
allow-presentationAllows the resource to start a presentation session.
allow-same-originAllows the resource to maintain its origin.
allow-scriptsAllows the resource to run scripts.
allow-top-navigationAllows the resource to navigate the top-level browsing context.
allow-top-navigation-by-user-activationAllows the resource to navigate the top-level browsing context, but only if initiated by a user gesture.

It is up to you to define which privileges you can grant to each iframe.

For instance, if your iframe only needs to submit forms and to open new modal windows, here is how you would configure the sandbox attribute:

<iframe sandbox="allow-forms allow-modals" src="https://www.something.com/"></iframe>

For a situation when the sandbox attribute is configured, and one feature is not working correctly within the resource, it might be because it lacks a specific flag. Make sure you know more about them to debug things quickly.

More great articles from LogRocket:

  • Don't miss a moment with The Replay, a curated newsletter from LogRocket
  • Learn how LogRocket's Galileo cuts through the noise to proactively resolve issues in your app
  • Use React's useEffect to optimize your application's performance
  • Switch between multiple versions of Node
  • Discover how to use the React children prop with TypeScript
  • Explore creating a custom mouse cursor with CSS
  • Advisory boards aren’t just for executives. Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.

Also, keep in mind that using an empty sandbox attribute will fully sandbox the iframe. This means that the JavaScript inside the iframe will not be executed, and all the privileges listed above will be restricted (like creating new windows or loading a plugin).

The empty sandbox attribute is mostly used for static content but will drastically reduce the capability required for other resources to work properly.

N.B., the sandbox attribute is unsupported in Internet Explorer 9 and earlier.

The allow attribute

This allow attribute lets you allow specific features like letting the iframe access the accelerometer, the battery information, or the camera. Be sure to check Can I Use… for details on cross-browser support.

There are more than 25 available flags, so I will not list them all here. You can browse them on the Mozilla Feature Policy Documentation. I summarized the most popular in the table below:

FlagDetails
accelerometerAllows access the Accelerometer interface
ambient-light-sensorAllows access the AmbientLightSensor interface
autoplayAllows to autoplay video and audio files
batteryAllows access to the Battery Status API
cameraAllows access to the camera
fullscreenAllows access to fullscreen mode
geolocationAllows access to the Geolocation API
gyroscopeAllows access to the Sensors API Gyroscope interface
magnetometerAllows access to the Sensors API Magnetometer interface
microphoneAllows access to the device microphone
midiAllows access to the Web MIDI API
paymentAllows access to the Payment Request API
usbAllows access to the WebUSB API
vibrateAllows access to the Vibration API

Things to know about iframes

How to deal with browsers that do not support iframes

If a browser does not support an iframe, it will display the content included between the opening <iframe> tag and the closing </iframe> tag.

Thus, you should always think about placing a warning message as a fallback for those poor users.

<iframe> <p>Your browser does not support iframes.</p></iframe>

How can you render the iframe like it is actually part of the parent document (i.e., no borders and scrollbars)?

🤓 The seamless attribute has been introduced for this exact purpose. It is still experimental and poorly supported among browsers. It’s also not part of the W3C HTML5 specification at the time of this writing.

<iframe seamless src="https://logrocket.com/"></iframe>

Can iframes affect my website’s SEO?

I did not know much about this, so I had to dig a little. There’s a lot of speculation around this subject.

For a long time, crawlers could not understand iframes, but this is no longer the case. The most relevant answer I found was from this article, and today’s conclusion seems to be:

Since search engines consider the content in iframes to belong to another website, the best you can hope for is no effect. iframes tend to neither help nor hurt your search engine ranking.

Thus, it is best to assume that the content displayed via iframes may not be indexed or available to appear in Google’s search results. A workaround would be to make sure to provide additional text-based links to the content they display so that Google crawlers can find and index this content.

N.B., you should also not worry about duplicate content issues since today’s web crawlers usually recognize that.

Can iframes affect my website’s loading speed?

Every iframe on a page will increase the memory used, as well as other computing resources like your bandwidth. So you should not use iframes excessively without monitoring what’s going on, or you might end up harming your page performance.

To avoid having your iframes slow down your pages, a good technique is to lazy load them (i.e., loading them only when they are required, like when the user scrolls near them). This can be achieved easily just by adding the loading="lazy" attribute to the tag.

Keep in mind that all modern Chromium-based browsers support this at the time of writing. For something that works everywhere, you may be interested in the lazyload library.

<iframe src="https://logrocket.com/" loading="lazy"></iframe>

N.B., the loading="lazy" attribute also works with the img tag, in case you didn’t know that already. 😜

How can I make an iframe responsive?

As more people browse the web using their phones, it is essential to make sure every one of your interfaces is responsive. But how can you do so when you have an iframe in your page?

We could dedicate an entire guide to the myriad ways to make your iframe responsive. Instead, I will just link to two excellent articles:

  1. This article (with probably the simplest solution) will show you how you can achieve this by wrapping your iframe in another HTML element and by adding a few CSS properties to it
  2. This second article will show you how you can make an iframe responsive by dealing with aspect ratios

There is also the iframe-resizer library, but keep in mind that it comes with a lot of additional features you may not actually need.

If you are using Bootstrap in your project, there are the embed-responsive and embed-responsive-16by9 classes that you can use straight out of the box to make your iframes responsive:

<div class="embed-responsive embed-responsive-16by9"> <iframe src="https://logrocket.com/" loading="lazy"></iframe></div>

How to prevent the white flash that occurs while the iframe is loading

Yes, my friends, there is a solution for this. In this article, Chris Coyier shares a little snippet that hides all the iframes on the page with some CSS, removes it until the window is loaded, and then makes them visible.

How to reload the content of an iframe

Easy peasy lemon squeezy! As you can access the window element of the iframe with contentWindow, you have to do this:

// Get the iframeconst iframe = document.getElementById('myIframe'); // Reload the iframeiframe.contentWindow.location.reload();

Bonus: About iframe accessibility

Most of the time, a screen reader will indicate that an iframe is present on the page.

Many will even enable you to navigate inside. If your iframe does not contain another webpage but instead contains external content like a video player or an ad, it is essential to add a title to the tag.

Adding a title to the tag gives users more context for what is the iframe is about.

<iframe src="an_ad.html" title="I contain an advertisem*nt"></iframe>

Some will say that this attribute is not essential since most screen readers will only read the document’s title when available and skip the title attribute on the iframe tag.

However, for the same title to be read correctly across various screen readers, it remains good practice to provide both and to make sure they match each other.

Another thing to keep in mind is that when your iframe includes non-readable content (like, for instance, a video game built with JavaScript), you will have to hide its contents from screen reader users using the aria-hidden attribute.

<iframe src="a_video_game.com" title="I contain a video game" aria-hidden="true"></iframe>

We talked about this at the beginning of this guide, but make sure to include some content inside the iframe for all the older browsers that do not support them.

This will give more context to everyone about what should be displayed in the space.

<iframe> <p>Your browser does not support iframes.</p></iframe>

LogRocket: Monitor iframe performance and how users interact with them

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. When implementing iframes, it’s critical that you understand how they impact the performance of the overall app or page as well as the user experience.

Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

Try it for free.

Conclusion

I hope this guide has helped you to improve your knowledge of iframes.

While they can be insecure if you’re loading untrusted content, they also offer some significant advantages. So you should not ban them entirely from your developing arsenal but only use them in relevant situations.

If you have anything to add to this article, you can reach me in the comments below or just ping me on Twitter @RifkiNada.

The ultimate guide to iframes - LogRocket Blog (2024)

References

Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5484

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.