W3cubDocs

/HTML

<iframe>

The <iframe> represents a nested browsing context, effectively embedding another HTML page into the current page. You can include any number of <iframe> elements within a document, each of which embeds another document inside <body> of a page.

Each embedded browsing context has its own session history and active document. The browsing context that contains the embedded content is called the parent browsing context. The topmost browsing context—the one without a parent—is usually the browser window, represented by the Window object.

Because each embedded browsing context created by <iframe> is itself a complete document environment, every use of <iframe> within a page can cause substantial increases in the amount of memory and other computing resources required by the document overall, so while theoretically you can use as many <iframe> as you like on a page, you should keep the potential for performance issues in mind.

Content categories Flow content, phrasing content, embedded content, interactive content, palpable content.
Permitted content Fallback content, i.e. content that is normally not rendered, but that browsers not supporting the <iframe> element will render.
Tag omission None, both the starting and ending tag are mandatory.
Permitted parents Any element that accepts embedded content.
Permitted ARIA roles application, document, img
DOM interface HTMLIFrameElement

Attributes

This element includes the global attributes.

allow
The allow attribute is used to specify a feature policy for the iframe.
allowfullscreen
This attribute can be set to true if the frame is allowed to be placed into full screen mode by calling its requestFullscreen() method.
This attribute is considered a legacy attribute and redefined as allow="fullscreen".
allowpaymentrequest
This attribute can be set to true if the contents of a cross-origin <iframe> should be allowed to invoke the Payment Request API.
This attribute is considered a legacy attribute and redefined as allow="payment".
height
Indicates the height of the frame in CSS pixels; prior to HTML 5, this value was specified in device pixels or as a percentage. Percentage values are no longer allowed.
importance
Indicates the relative importance of the resource. Priority hints are delegated using the values:

auto: Indicates no preference. The browser may use its own heuristics to decide the priority of the resource.

high: Indicates to the browser that the resource is of high priority.

low: Indicates to the browser that the resource is of low priority.

name
A name for the embedded browsing context (or frame). This can be used as the value of the target attribute of an <a>, <form> or <base> element, or the formtarget attribute of an <input> or <button> element. It can also be used as the value of the windowName parameter in the window.open() method.
referrerpolicy
A string indicating which referrer to use when fetching the resource:
  • no-referrer: The Referer header will not be sent.
  • no-referrer-when-downgrade: No Referer: header will be sent when navigating to an origin without TLS (HTTPS). This is a user agent’s default behavior, if no policy is otherwise specified.
  • origin: The referrer will be the origin of the page, that is roughly the scheme, the host, and the port.
  • origin-when-cross-origin: Navigations to other origins will be limited to the scheme, the host and the port, while navigations on the same origin will include the referrer's path.
  • unsafe-url: The referrer will include the origin and the path (but not the fragment, password, or username). This case is unsafe because it can leak origins and paths from TLS-protected resources to insecure origins.
sandbox
If specified as an empty string, this attribute enables extra restrictions on the content that can appear in the inline frame. The value of the attribute can either be an empty string (all the restrictions are applied), or a space-separated list of tokens that lift particular restrictions. Valid tokens are:
  • allow-forms: Allows the embedded browsing context to submit forms. If this keyword is not used, this operation is not allowed.
  • allow-modals: Allows the embedded browsing context to open modal windows.
  • allow-orientation-lock: Allows the embedded browsing context to disable the ability to lock the screen orientation.
  • allow-pointer-lock: Allows the embedded browsing context to use the Pointer Lock API.
  • allow-popups: Allows popups (like from window.open, target="_blank", showModalDialog). If this keyword is not used, that functionality will silently fail.
  • allow-popups-to-escape-sandbox: Allows a sandboxed document to open new windows without forcing the sandboxing flags upon them. This will allow, for example, a third-party advertisement to be safely sandboxed without forcing the same restrictions upon a landing page.
  • allow-presentation: Allows embedders to have control over whether an iframe can start a presentation session.
  • allow-same-origin: Allows the content to be treated as being from its normal origin. If this keyword is not used, the embedded content is treated as being from a unique origin.
  • allow-scripts: Allows the embedded browsing context to run scripts (but not create pop-up windows). If this keyword is not used, this operation is not allowed.
  • allow-storage-access-by-user-activation : Allows the embedded browsing context to request access to the embedding document's storage capabilities (e.g. cookies, Web Storage) via the Storage Access API.
  • allow-top-navigation: Allows the embedded browsing context to navigate (load) content to the top-level browsing context. If this keyword is not used, this operation is not allowed.
  • allow-top-navigation-by-user-activation: Allows the embedded browsing context to navigate (load) content to the top-level browsing context only when initiated by a user gesture. If this keyword is not used, this operation is not allowed.
Notes about sandboxing:
  • When the embedded document has the same origin as the main page, it is strongly discouraged to use both allow-scripts and allow-same-origin at the same time, as that allows the embedded document to programmatically remove the sandbox attribute. Although it is accepted, this case is no more secure than not using the sandbox attribute.
  • Sandboxing in general is only of minimal help if the attacker can arrange for the potentially hostile content to be displayed in the user's browser outside a sandboxed iframe. It is recommended that such content should be served from a separate dedicated domain, to limit the potential damage.
  • The sandbox attribute is not supported in Internet Explorer 9 and earlier versions.
src
The URL of the page to embed. Use "about:blank" to embed an empty page that conforms to the same-origin policy.
srcdoc
The content of the page that the embedded context is to contain. This attribute is expected to generally be used together with the sandbox attribute. If a browser supports the srcdoc attribute, it will override the content specified in the src attribute (if present). If a browser does not support the srcdoc attribute, it will show the file specified in the src attribute instead (if present). Note that if the content of the attribute contains a script tag then a closing script tag is required for the script to run, even if nothing else comes after the script.
width
Indicates the width of the frame. Beginning in HTML 5, this is specified in CSS pixels. Previously, it was specified in either device pixels or as a percentage. See Window.devicePixelRatio for an explanation of the difference between CSS pixels and device pixels.

Deprecated attributes

These attributes are deprecated and may no longer be supported by all browsers and user agents. You should not use them in new content, and should try to remove them from existing content.

align Deprecated since HTML4.01, Obsolete since HTML5
The alignment of this element with respect to the surrounding context.
frameborder Obsolete since HTML5
The value 1 (the default) tells the browser to draw a border between this frame and every other frame. The value 0 tells the browser not to draw a border between this frame and other frames. You should instead use the CSS property border and its longhand properties to create borders around <iframe>s.
longdesc Obsolete since HTML5
A URI of a long description of the frame. Due to widespread misuse, this is not helpful for non-visual browsers.
marginheight Obsolete since HTML5
The amount of space in pixels between the frame's content and its top and bottom margins.
marginwidth Obsolete since HTML5
The amount of space in pixels between the frame's content and its left and right margins.
scrolling Obsolete since HTML5
Enumerated attribute indicating when the browser should provide a scroll bar (or other scrolling device) for the frame:
  • auto: Only when needed.
  • yes: Always provide a scroll bar.
  • no: Never provide a scroll bar.

Non-standard attributes

mozbrowser
See bug 1318532 for exposing this to WebExtensions in Firefox.
Makes the <iframe> act like a top-level browser window. See Browser API for details.
Available only to WebExtensions.

Scripting

Inline frames, like <frame> elements, are included in the list of the window's frames in the window.frames pseudo-array.

From the DOM HTMLIFrameElement object which implements the <iframe> element, scripts can get access to the window object of the included HTML page via the contentWindow property. The contentDocument property refers to the document element inside the <iframe> (this is equivalent to contentWindow.document).

From the inside of a frame, a script can get a reference to the parent window using window.parent.

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved using Window.postMessage().

Positioning and scaling

As a replaced element, the position and alignment of the embedded document within the <iframe> element's box, as well as the scaling of the embedded content, can be adjusted using the object-position and object-fit properties.

Examples

Here are a few examples demonstrating the use of the <iframe> element.

A simple <iframe>

This is a basic example of an <iframe> in action. After creating the frame, when the user clicks a button, its title is retrieved and displayed in an alert.

HTML

<iframe src="https://mdn-samples.mozilla.org/snippets/html/iframe-simple-contents.html" title="iframe example 1" width="400" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>

Result

Open a link in an <iframe> in another tab

In this example, a Google map is displayed in a frame;

HTML

<base target="_blank">
<iframe id="Example2"
    title="Example2"
    width="400"
    height="300"
    frameborder="0"
    scrolling="no"
    marginheight="0"
    marginwidth="0"
    src="https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=es-419&amp;geocode=&amp;q=buenos+aires&amp;sll=37.0625,-95.677068&amp;sspn=38.638819,80.859375&amp;t=h&amp;ie=UTF8&amp;hq=&amp;hnear=Buenos+Aires,+Argentina&amp;z=11&amp;ll=-34.603723,-58.381593&amp;output=embed">
</iframe>

<br>
<small>
  <a href="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=es-419&amp;geocode=&amp;q=buenos+aires&amp;sll=37.0625,-95.677068&amp;sspn=38.638819,80.859375&amp;t=h&amp;ie=UTF8&amp;hq=&amp;hnear=Buenos+Aires,+Argentina&amp;z=11&amp;ll=-34.603723,-58.381593" style="color:#0000FF;text-align:left"> See bigger map </a>
</small>

Result

Accessibility concerns

People navigating with the aid of assistive technology such as a screen reader can use the value of a title attribute declared on an iframe to determine what embedded content it contains. The title's value should concisely describe the embedded content.

<iframe title="The Wikipedia page for Avocados" src="https://en.wikipedia.org/wiki/Avocado"></iframe>

Without this description, they may have to navigate into the iframe and browse around to determine what the embedded content is. This context shifting can be a confusing and time-consuming process, especially for pages where multiple iframe embeds are present and/or if the embeds contain interactive content such as video or music.

Specifications

Specification Status Comment
Referrer Policy
The definition of 'referrerpolicy attribute' in that specification.
Candidate Recommendation Added the referrerpolicy attribute.
HTML Living Standard
The definition of '<iframe>' in that specification.
Living Standard
HTML5
The definition of '<iframe>' in that specification.
Recommendation
HTML 4.01 Specification
The definition of '<iframe>' in that specification.
Recommendation
Screen Orientation API Working Draft Adds allow-orientation-lock to the sandbox attribute.
Presentation API
The definition of 'allow-presentation' in that specification.
Candidate Recommendation Adds allow-presentation to the sandbox attribute

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 1 Yes Yes
Yes
The resize CSS property doesn't have any effect on this element due to bug 680823.
Yes Yes Yes
Yes
Safari has a bug that prevents iframes from loading if the iframe element was hidden when added to the page. iframeElement.src = iframeElement.src should cause it to load the iframe.
align 1 Yes Yes Yes Yes Yes
allow 60 No No No 47 11.1
allowfullscreen 27
27
17
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Yes 18
18
9
Prefixed
Prefixed Implemented with the vendor prefix: moz
11
Prefixed
11
Prefixed
Prefixed Implemented with the vendor prefix: ms
Yes 7
7
Yes
Prefixed
Prefixed Implemented with the vendor prefix: webkit
allowpaymentrequest No No No No No No
frameborder 1 Yes Yes Yes Yes Yes
height 1 Yes Yes Yes Yes Yes
longdesc 1 Yes Yes Yes Yes Yes
marginheight 1 Yes Yes Yes Yes Yes
marginwidth 1 Yes Yes Yes Yes Yes
name 1 Yes Yes Yes Yes Yes
referrerpolicy 51 No 50 No 38 11.1
scrolling 1 Yes Yes Yes Yes Yes
sandbox 4 Yes 17 10 15 5
sandbox="allow-popups" Yes Yes 28 ? ? ?
sandbox="allow-popups-to-escape-sandbox" 46 No 49 No 32 ?
sandbox="allow-modals" ? No 49 No ? ?
sandbox="allow-presentation" 53 No 50 No 40 ?
sandbox="allow-storage-access-by-user-activation" No No 65
Disabled
65
Disabled
Disabled From version 65: this feature is behind the dom.storage_access.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No No 11.1
11.1
Currently only available on macOS High Sierra 10.13.4 beta, and in Safari Technology Preview 47+.
sandbox="allow-top-navigation-by-user-activation" 58 No No No 45 ?
src 1 Yes Yes Yes Yes Yes
srcdoc 20 No 25 No 15 6
width 1 Yes Yes Yes Yes Yes
mozbrowser No No No
No
See bug 1318532
Available only to WebExtensions.
No No No
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes Yes Yes Yes
Yes
The resize CSS property doesn't have any effect on this element due to bug 680823.
Yes Yes
Yes
Safari has a bug that prevents iframes from loading if the iframe element was hidden when added to the page. iframeElement.src = iframeElement.src should cause it to load the iframe.
Yes
align Yes Yes Yes Yes Yes Yes Yes
allow 60 60 No No 47 No No
allowfullscreen ? ? Yes 18
18
9
Prefixed
Prefixed Implemented with the vendor prefix: moz
No 7
7
Yes
Prefixed
Prefixed Implemented with the vendor prefix: webkit
?
allowpaymentrequest No No No No No No No
frameborder Yes Yes Yes Yes Yes Yes Yes
height Yes Yes Yes Yes Yes Yes Yes
longdesc Yes Yes Yes Yes Yes Yes Yes
marginheight Yes Yes Yes Yes Yes Yes Yes
marginwidth Yes Yes Yes Yes Yes Yes Yes
name Yes Yes Yes Yes Yes Yes Yes
referrerpolicy 51 51 No 50 38 No 7.2
scrolling Yes Yes Yes Yes Yes Yes Yes
sandbox Yes Yes Yes 17 ? 4.2 Yes
sandbox="allow-popups" ? ? No 27 ? ? ?
sandbox="allow-popups-to-escape-sandbox" 46 46 No 49 32 ? 5.0
sandbox="allow-modals" ? ? No 49 ? ? ?
sandbox="allow-presentation" No 53 No 50 40 ? 6.0
sandbox="allow-storage-access-by-user-activation" No No No 65
Disabled
65
Disabled
Disabled From version 65: this feature is behind the dom.storage_access.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No 11.1
11.1
Currently only available on iOS 11.3 beta.
?
sandbox="allow-top-navigation-by-user-activation" 58 58 No No 45 ? 7.0
src Yes Yes Yes Yes Yes Yes Yes
srcdoc ? ? No 25 ? ? ?
width Yes Yes Yes Yes Yes Yes Yes
mozbrowser No No No No
No
See bug 1318532
Available only to WebExtensions.
No No No

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe