designwithfocus

HTML

How to Embed an iframe in HTML Without Breaking the Page

By Design With Focus Team 7 min read
An HTML page with a framed map embed and the iframe tag visible beside it

Short answer

Add an iframe with a src URL and a title, then set the width and height so the frame has a size. MDN's default width is 300 CSS pixels and the default height is 150. Give the element a title that names the embedded page. Use sandbox when the other page should not run scripts or submit forms, and use an https URL on an https page.

Last verified against the sources listed below.

Key takeaways

  • An iframe is a nested browsing context. Both the start tag and the end tag are required.
  • MDN's default width is 300 CSS pixels and the default height is 150.
  • The title attribute is how a screen reader labels the frame without entering it.
  • An empty sandbox attribute applies every restriction. Tokens such as allow-scripts lift one restriction each.
  • X-Frame-Options DENY blocks all framing. A meta tag cannot set that header.
  • Browsers treat an HTTP iframe inside an HTTPS page as blockable mixed content.

how to embed an iframe in html starts with one element, a URL in src, and a title that names the framed page. The frame is a nested browsing context, which means the other document has its own window inside yours.

You need a page you are allowed to frame, an https URL if your page is https, and a short title. HTML already gives you the head and the body. The iframe goes in the body, where you want the other document to appear. Both the start tag and the end tag are required, even when you put nothing between them.

What belongs on the element?

Set src, title, width, and height before you add sandbox or loading.

MDN defines src as the URL of the page to embed. title is the accessible name. MDN’s accessibility note says people using a screen reader use that title to tell what the frame contains. Without it, they have to enter the frame to find out, which is a hard context shift when a page has several frames.

Web accessibility is the reason the title is not optional in practice, even though the parser will accept a frame that lacks one.

Width and height are CSS pixels. MDN’s default width is 300 and the default height is 150. A stylesheet width overrides the width attribute.

A px to rem converter helps when the layout uses rem and the embed brief still says 600 pixels. The frame is a replaced element. MDN says object-fit has no effect on it. object-position can move the embedded document inside the box.

How do you embed the page?

Write the element, point src at an https URL, and name the frame in title.

  1. Choose the document URL. Use https when the parent page is https. MDN’s basic example is src="https://example.org" with a title, a width, and a height.
  2. Put the iframe in the body where the embed should show. Include the end tag. The element is not a void tag.
  3. Add a title that describes the content, such as “Map of the store entrance,” not the word “iframe.”
  4. Set width and height. If you omit them, MDN uses 300 by 150 CSS pixels.
  5. Add loading="lazy" only if the frame is below the first screen. The default is eager, which loads it with the page. Lazy waits until the frame is near the visual viewport, and only when JavaScript is enabled.
  6. Add sandbox if the framed page should not get the parent’s privileges. An empty sandbox attribute turns on every restriction. Add tokens only for what the frame must do.

A map embed from MDN’s reference looks like this, with the title first so the name is obvious in the markup:

<iframe
  title="OpenStreetMap sample"
  width="300"
  height="200"
  src="https://www.openstreetmap.org/export/embed.html">
</iframe>

For a comment or other HTML you control, MDN shows srcdoc plus an empty sandbox. srcdoc overrides src. Relative links inside srcdoc resolve against the embedding page unless you set a base of about:srcdoc.

Which sandbox tokens can you set?

The HTML spec lists 13 tokens. An empty sandbox value applies all restrictions, and each token gives one back.

The WHATWG iframe section allows allow-downloads, allow-forms, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-popups, allow-popups-to-escape-sandbox, allow-presentation, allow-same-origin, allow-scripts, allow-top-navigation, allow-top-navigation-by-user-activation, and allow-top-navigation-to-custom-protocols. Without allow-scripts, scripts do not run.

Without allow-forms, a form can still be seen, but MDN says submitting it will not validate input, send data, or close a dialog. Without allow-same-origin, the framed document is treated as a unique opaque origin.

Do not combine allow-scripts and allow-same-origin when the framed page is on your own origin. MDN and the HTML spec both say that pair lets the framed page remove the sandbox attribute and escape it. The spec also says the tokens do nothing for a document that is already loaded. Changing sandbox later applies on the next navigation.

allow is a different attribute. MDN says it sets a Permissions Policy for features such as the camera or microphone, and it can only tighten a policy that the Permissions-Policy header already set. It does not replace that header.

The older allowfullscreen attribute is legacy. MDN says it is redefined as allow="fullscreen *".

Why is the frame blank?

A blank frame is usually a blocked URL, a framing header, or mixed content, not a missing end tag.

What you see What the docs describe What to change
Blank frame, and load still fired MDN does not fire an error event on iframe, and load fires even when content fails Inspect the response. Do not trust the error event
The other site never appears X-Frame-Options DENY blocks every frame. SAMEORIGIN allows framing only when every ancestor shares the page’s origin You cannot override that header from your markup
HTTPS page, HTTP src MDN lists iframe src as blockable mixed content. Browsers block it Change src to https
Form or script does nothing Empty sandbox, or sandbox without allow-forms or allow-scripts Add only the token that feature needs
Frame ignores a meta X-Frame-Options MDN says a meta tag has no effect. The header must be on the HTTP response Stop setting it in the head

X-Frame-Options is how a site refuses to be framed, often to stop clickjacking. ALLOW-FROM is obsolete. MDN says modern browsers ignore the header entirely if they see that directive.

The replacement MDN points to is the frame-ancestors directive in Content-Security-Policy. If the header is absent and no other embedding rule exists, the browser allows the frame.

Mixed content is the secure-page, insecure-resource case. Images can be upgraded from an insecure URL to a secure one. An iframe is not in that upgradable list.

It is blockable, so the frame stays empty rather than loading the insecure document. The W3C Mixed Content spec says anything that is not upgradeable image, video, or audio is blockable. Local URLs such as a loopback host are treated as secure origins, so they are not the same failure.

The map sample above points at an OpenStreetMap export, which is the URL in MDN’s own try-it example.

What should you leave out?

Leave out deprecated frame attributes, and do not stack frames you do not need.

MDN marks frameborder, scrolling, align, longdesc, marginwidth, and marginheight as deprecated. Use the CSS border property instead of frameborder. A border of 0 in CSS replaces the old frameborder=“0” habit.

Each frame is a full document, and MDN warns that every iframe costs memory. A page of a dozen third-party frames pays that cost even when they are lazy, because lazy only delays the fetch.

Cross-origin scripts cannot read the frame’s document. MDN says same-origin policy blocks that access, and Window.postMessage is the path for a message you intended to send. A sandboxed frame also cannot navigate the top window unless you set allow-top-navigation or allow-top-navigation-by-user-activation. That is the control that stops an embed from sending the visitor to another site without a gesture.

Frequently asked questions

Why does the load event fire when the frame looks empty?

MDN says user agents do not fire the error event on iframes, and the load event always fires even if the content fails to load. That behavior is a security precaution so a page cannot probe which URLs exist on a local network. You cannot treat a missing error event as proof that the frame rendered. Check the network panel for a blocked response instead. A failed frame can still look blank while load has already run.

What does loading=lazy skip when scripting is off?

MDN says lazy loading is deferred only when JavaScript is enabled. If scripting is disabled, the browser does not defer the frame. The reason is tracking. A page could place iframes down the page and learn approximate scroll position from which frames a server saw requested. Eager is the default and loads the frame with the page. Lazy waits until the frame is within a distance of the visual viewport that the browser calculates.

Which sandbox tokens does the HTML spec list?

The WHATWG HTML spec lists allow-downloads, allow-forms, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-popups, allow-popups-to-escape-sandbox, allow-presentation, allow-same-origin, allow-scripts, allow-top-navigation, allow-top-navigation-by-user-activation, and allow-top-navigation-to-custom-protocols. Tokens are ASCII case-insensitive and space-separated. The spec says not to set allow-top-navigation and allow-top-navigation-by-user-activation together, because only allow-top-navigation would apply.

Can a PDF sit in an iframe?

Yes. MDN says an iframe can show a PDF in the browser's built-in viewer. Unlike object, iframe has no child content to use as a fallback if the PDF cannot be displayed, so put a normal link outside the frame. The sandbox attribute can stop that built-in viewer from loading, even if you include allow-scripts or allow-downloads. MDN says sandbox is not a portable way to restrict a native PDF preview, because the viewer already sandboxes executable content.

What happens if you delete the src attribute in script?

MDN says removing src with removeAttribute loads about:blank in Firefox from version 65, in Chromium browsers, and in Safari on iOS. about:blank is also the value you can set on purpose for an empty page that follows the same-origin policy. That blank document uses the embedding page's URL as its base URL when it resolves relative links. srcdoc overrides src and uses about:srcdoc as its location. If a browser does not support srcdoc, it falls back to the src URL.

Sources