The <iframe>
represents a nested browsing context, effectively embedding another HTML page into the current page. In HTML 4.01, a document may contain a head
and a body
or a head
and a frameset
, but not both a body
and a frameset
. However, an <iframe>
can be used within a normal document body. Each 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 top-level browsing context (which has no parent) is typically the browser window.
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 |
This element includes the global attributes.
align
Deprecated since HTML4.01, Obsolete since HTML5
allowfullscreen
true
if the frame is allowed to be placed into full screen mode by calling its Element.requestFullscreen()
method. If this isn't set, the element can't be placed into full screen mode.allowpaymentrequest
true
if the contents of a cross-origin <iframe>
should be allowed to invoke the Payment Request API.frameborder
HTML 4 onlyheight
longdesc
HTML 4 onlymarginheight
HTML 4 onlymarginwidth
HTML 4 onlymozallowfullscreen
allowfullscreen
instead. In Gecko 9.0 or later, this attribute can be set to true
if the frame is allowed to be placed into full screen mode by calling its element.mozRequestFullScreen()
method. If this isn't set, the element can't be placed into full screen mode.webkitallowfullscreen
allowfullscreen
instead. In Chrome 17 or later (and maybe earlier), this attribute can be set to true
if the frame is allowed to be placed into full screen mode by calling its element.webkitRequestFullScreen()
method. If this isn't set, the element can't be placed into full screen mode.name
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
"no-referrer"
meaning that the Referer:
header will not be sent.no-referrer-when-downgrade
" meaning that 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"
meaning that the referrer will be the origin of the page, that is roughly the scheme, the host and the port."origin-when-cross-origin"
meaning that 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"
meaning that 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.scrolling
HTML 4 onlyauto
: Only when needed.yes
: Always provide a scroll bar.no
: Never provide a scroll bar.sandbox
HTML5 onlyallow-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-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.Note:
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.iframe
. It is recommended that such content should be served from a separate dedicated domain, to limit the potential damage.sandbox
attribute is not supported in Internet Explorer 9 and earlier versions.src
srcdoc
HTML5 onlysandbox
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
Inline frames, like <frame>
elements, enter the window.frames
pseudo-array.
From the DOM 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
), but is not supported by Internet Explorer versions before IE8.
From the inside of a frame, a script can get a reference to the parent window via 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 with Window.postMessage()
.
Here are a few examples demonstrating the use of the <iframe>
element.
<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.
<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>
<iframe>
in another tabIn this example, a Google map is displayed in a frame;
<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&source=s_q&hl=es-419&geocode=&q=buenos+aires&sll=37.0625,-95.677068&sspn=38.638819,80.859375&t=h&ie=UTF8&hq=&hnear=Buenos+Aires,+Argentina&z=11&ll=-34.603723,-58.381593&output=embed"> </iframe> <br> <small> <a href="https://maps.google.com/maps?f=q&source=embed&hl=es-419&geocode=&q=buenos+aires&sll=37.0625,-95.677068&sspn=38.638819,80.859375&t=h&ie=UTF8&hq=&hnear=Buenos+Aires,+Argentina&z=11&ll=-34.603723,-58.381593" style="color:#0000FF;text-align:left"> See bigger map </a> </small>
Starting in Gecko 6.0, rendering of inline frames correctly respects the borders of their containing element when they're rounded using border-radius
.
Specification | Status | Comment |
---|---|---|
Referrer Policy The definition of 'referrerpolicy attribute' in that specification. | Editor's Draft | 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 |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | Yes | Yes1 | Yes | Yes | Yes2 |
align |
1 | Yes | Yes | Yes | Yes | Yes |
allowfullscreen |
27 17 webkit |
Yes |
18 9 moz |
11 ms | Yes |
7 Yes 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 | ? | ? |
scrolling |
1 | Yes | Yes | Yes | Yes | Yes |
sandbox |
4 | Yes | 17 | 10 | 15 | 5 |
src |
1 | Yes | Yes | Yes | Yes | Yes |
srcdoc |
20 | No | 25 | No | 15 | 6 |
width |
1 | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes1 | Yes | Yes | Yes2 |
align |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
allowfullscreen |
? | ? | Yes |
18 9 moz |
No | No |
7 Yes 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 | No | ? | ? |
scrolling |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
sandbox |
Yes | Yes | Yes | 17 | 10 | ? | 4.2 |
src |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
srcdoc |
? | ? | No | 25 | No | ? | ? |
width |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
1. The resize
CSS property doesn't have any effect on this element due to bug 680823.
2. 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.
© 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