What is an iframe?

What is an iframe?

An iframe is an HTML element that lets you embed another web page, or content from another site, inside the page you are currently viewing. Its name comes from the expression inline frame: a rectangular window defined in the code that the browser fills by loading a different URL. When a user visits a page that contains an iframe, the embedded content looks like a natural part of the layout, even though it is actually being loaded from a separate document, often hosted on another domain.

Iframes appear across almost the entire modern web. An interactive map inserted on a company's contact page, a video playing inside a news article or a payment form hosted by an external provider are, in most cases, iframes. This article explains in detail what an iframe is, how it works behind the scenes, what it is used for, how to write its code, what its most common attributes are, what advantages it offers, what security risks you should know about, and what alternatives exist when it is not the best choice.

What an iframe is and how it works

In technical terms, the iframe is an HTML element defined with the iframe tag. It creates a rectangular region inside the main page and tells the browser to display there the document found at the given URL. The embedded page may belong to the same site or to a completely different one: the browser does not distinguish between the two situations when rendering the frame and its content.

The most important thing to understand about how it works is that the embedded content is not copied into the host page: it is loaded as an independent document, with its own browsing context. That means the iframe has its own navigation history, runs its own scripts, manages its own cookies and keeps its own origin. It works like a nested tab inside the page. For this reason, if the embedded site updates its content, visitors to the host page will see the newest version without the person in charge of that page having to do anything.

This independence also explains why an iframe does not behave like a simple image. An image is a file that is shown as it is; an iframe is a complete page, with text, links, forms, styles and interactive functionality. In practice, looking at the source code of many websites reveals iframe tags pointing to map services, video players, comment systems, payment gateways and many other tools.

What an iframe is used for

The most frequent uses of the iframe involve integrating content that already exists and that would be costly or unnecessary to build from scratch. Some of the most common cases are:

  • Interactive maps: embedding a map on a business contact page so people can see the location without leaving the site.
  • Videos: inserting videos from platforms such as YouTube or Vimeo so they play directly inside an article or a product page.
  • External forms: showing payment, registration, subscription or survey forms hosted on third-party servers, such as payment providers or email marketing tools.
  • Widgets and tools: calendars, customer support chats, music players, quote calculators, news feeds or social media posts.
  • Documents and applications: presentations, published spreadsheets or small web applications that the provider offers for integration into other sites.

In all these cases, the service provider usually delivers a snippet of code ready to copy, almost always under the label "embed". The site owner only has to paste that code into the page for the content to start working immediately, without developing anything new.

How to write an iframe

The basic way to write an iframe is very simple. You open the tag, point to the content address with the src attribute, set the width and height, and close the tag. A minimal example would look like this in a page's code:

<iframe src="https://example.com/content" width="600" height="400" title="Embedded content"></iframe>

The src attribute is the only required one and holds the URL of the document you want to show. width and height define the dimensions of the frame in pixels and can later be adjusted with CSS styles, for example to make the iframe take the full width of the screen on mobile phones. The title attribute is not required, but it describes the content and is strongly recommended for accessibility, because screen readers use it to announce what is inside the frame.

Keep in mind that the tag must always be closed, and that any text written between the opening and closing tags will only be displayed if the visitor's browser does not support iframes. You can take advantage of that feature to include a notice or an alternative link for people using very old browsers or devices with limited support.

Common iframe attributes

Besides the ones already mentioned, the iframe tag supports several attributes that control its behavior. The most widely used are:

AttributeWhat it doesExample
srcSets the URL of the content being embedded.src="https://example.com"
width and heightDefine the width and height of the frame in pixels.width="560" height="315"
titleDescribes the content for accessibility and to identify the frame.title="Presentation video"
allowfullscreenAllows the content to be displayed in full screen.allowfullscreen
loadingIndicates whether the frame loads immediately or in a deferred way.loading="lazy"
sandboxRestricts the actions the embedded content can perform.sandbox="allow-scripts"

The sandbox attribute deserves special attention because it limits what the content of an iframe can do: with no added values, the content stays isolated and cannot run scripts, submit forms or open windows. Each permission is enabled explicitly. It is a valuable tool when embedding content whose trust level is not complete. Some older attributes, such as frameborder, are now considered obsolete and should not be used.

Advantages of using iframes

The iframe has survived since the early years of the web because it solves real problems. Its main advantages include:

  • Embedding without reloading: content appears inside the page without the user having to leave it or wait through an extra visible load.
  • Leveraging third-party services: there is no need to program a video player, a map or a payment gateway; you integrate a service already built and maintained by its provider.
  • Isolation: an error or an outage in the embedded content does not break the main page, because both documents are independent.
  • Always up-to-date content: since the iframe loads the live version of the URL, any change the provider makes is reflected immediately on every page that embeds it.
  • Reuse: the same content can be embedded in many pages or sites without duplicating files or data.

For a blog or a business website, embedding a map or a video improves the visitor experience, increases the time spent on the page and adds useful visual information, all without touching your own infrastructure.

Security risks and best practices

The iframe is not insecure by itself; the risk depends on what content is embedded and where it comes from. Embedding pages from untrusted sites can expose visitors to misleading content, fraudulent links or malicious scripts trying to steal information. For that reason, the basic rule is clear: only embed known and verified sources, and always use the official embed codes published by the service provider.

One of the best-known attacks related to iframes is clickjacking. In that attack, a malicious page loads a legitimate site, for example a bank's login form, inside an invisible or transparent iframe and places it over elements that invite a click. The user believes they are clicking a harmless button, but they are actually interacting with the hidden site, for example confirming a transaction without knowing it. To protect themselves, sites that do not want to be embedded can tell the browser to refuse that usage through security headers such as X-Frame-Options or the frame-ancestors directive of the Content Security Policy.

To reduce the risks when embedding your own or third-party content, it is worth following a few best practices:

  • Embed only content from trusted domains served over a secure connection.
  • Use the sandbox attribute to limit the permissions of the content when its full functionality is not needed.
  • Always include a descriptive title and correct dimensions so the frame does not affect the layout.
  • Avoid unnecessary iframes: each frame adds an extra request and can slow the page down.
  • Review the provider's privacy policy when the embedded content collects data from visitors.

Alternatives to the iframe

There are other ways to integrate content. The object and embed tags allow embedding documents and applications, although they are used far less than the iframe today and their support is more limited. When the goal is simply to offer a document for viewing, many times the best option is to embed nothing and place a direct link to the resource instead.

There are also specific alternatives depending on the type of content: a static map image linked to the map service, a video thumbnail linked to the platform, or a form that redirects to the provider's site. These options usually load faster and avoid the security risks, although they offer a less integrated experience in return.

In summary

An iframe is an HTML element that embeds a page or external content inside another page, and it is used to integrate maps, videos, forms, widgets and applications without reloading the site or building those components from scratch. It is written with the iframe tag, pointing to the content address and setting the frame dimensions, and it supports attributes such as title, allowfullscreen or sandbox to improve accessibility and security. Its main risk lies in embedding content from untrusted sites, which can be avoided by following best practices and using the right alternatives when the content does not need to be integrated.

Chatea por WhatsApp