Summary
The HTML <iframe> Element (or HTML inline frame element) 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 frame-set, but not both a body and a frame-set. 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 contentSpecial, see prose
- Tag omission None, both the starting and ending tag are mandatory.
- Permitted parent elements Any element that accepts embedded content.
- DOM interface
HTMLIFrameElement
Attributes
This element includes the global attributes.
-
alignDeprecated since HTML4.01, Obsolete since HTML5 - The alignment of this element with respect to the surrounding context.
-
allowfullscreen -
This attribute can be set to
trueif the frame is allowed to be placed into full screen mode by calling itselement.mozRequestFullScreen()method. If this isn't set, the element can't be placed into full screen mode. -
frameborderHTML 4 only - 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.
-
height - Indicates the height of the frame HTML5 in CSS pixels, or HTML 4.01 in pixels or as a percentage.
-
longdescHTML 4 only - A URI of a long description of the frame. Due to widespread misuse, this is not helpful for non-visual browsers.
-
marginheightHTML 4 only - The amount of space in pixels between the frame's content and its top and bottom margins.
-
marginwidthHTML 4 only - The amount of space in pixels between the frame's content and its left and right margins.
-
mozallowfullscreen -
Use
allowfullscreeninstead. In Gecko 9.0 or later, this attribute can be set totrueif the frame is allowed to be placed into full screen mode by calling itselement.mozRequestFullScreen()method. If this isn't set, the element can't be placed into full screen mode. -
webkitallowfullscreen -
Use
allowfullscreeninstead. In Chrome 17 or later (and maybe earlier), this attribute can be set totrueif the frame is allowed to be placed into full screen mode by calling itselement.webkitRequestFullScreen()method. If this isn't set, the element can't be placed into full screen mode. -
mozappOnly available on Firefox OS - For frames hosting an open web app, this specifies the URL of the app manifest. This ensures that the app is loaded with the right permissions. See Using the Browser API for details. Available in Gecko 13.0 and later.
-
mozbrowserOnly available on Firefox OS -
Indicates that the frame is to appear like a top-level browser window to the embedded content. This means that
window.top,window.parent,window.frameElement, etc. will not reflect the frame hierarchy. This allows for a web browser UI to be implemented entirely with web technology, given the right permissions. See Using the Browser API for details. Available in Gecko 13.0 and later. -
name -
A name for the embedded browsing context (or frame). This can be used as the value of the
targetattribute of an<a>or<form>element, or the formtarget attribute of an<input>or<button>element. -
remoteOnly available on Firefox OS - Load the frame's page in a separate content process.
-
scrollingHTML 4 only -
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 scoll bar.
-
sandboxHTML5 only -
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-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-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-forms: Allows the embedded browsing context to submit forms. If this keyword is not used, this operation is not allowed.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-pointer-lock: Allows the embedded browsing context to use the Pointer Lock API.
Note:
- When the embedded document has the same origin as the main page, it is strongly discouraged to use both
allow-scriptsandallow-same-originat the same time, as that allows the embedded document to programmatically remove thesandboxattribute. Although it is accepted, this case is no more secure than not using thesandboxattribute. - 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
sandboxattribute is not supported in Internet Explorer 9 and earlier versions, or in Opera.
-
seamlessHTML5 only -
This Boolean attribute indicates that the browser should render the inline frame in a way that makes it appear to be part of the containing document, for example by applying CSS styles that apply to the
<iframe>to the contained document before styles specified in that document, and by opening links in the contained documents in the parent browsing context (unless another setting prevents this). In XHTML, attribute minimization is forbidden, and the seamless attribute must be defined as<iframe seamless="seamless">. -
src - The URL of the page to embed.
-
srcdocHTML5 only -
The content of the page that the embedded context is to contain. This attribute is expected to be used together with the sandbox and seamless attributes. If a browser supports the
srcdocattribute, it will override the content specified in thesrcattribute (if present). If a browser does NOT support thesrcdocattribute, it will show the file specified in thesrcattribute instead (if present). -
width - Indicates the width of the frame HTML5 in CSS pixels, or HTML 4.01 in pixels or as a percentage.
Scripting
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.
Example 1
<script> var iframe = window.getElementsByTagName( "iframe" )[ 0 ]; alert( "Frame title: " + iframe.contentWindow.title ); </script> <iframe src="page.html" width="300" height="300"> <p>Your browser does not support iframes.</p> </iframe>
Example 2: Open link inside iframe in another tab
<base target="_blank" /> <iframe width="400" height="215" 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>
Result
Notes
Note: Starting in Gecko 6.0, rendering of inline frames correctly respects the borders of their containing element when they're rounded using border-radius.
Specifications
| Specification | Status | Comment |
|---|---|---|
| WHATWG HTML Living Standard | Living Standard | |
| HTML5 | Candidate Recommendation | |
| HTML 4.01 Specification | Recommendation |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 1.0 | (Yes) | (Yes) | (Yes) | (Yes) |
sandbox |
4 | 17.0 (17.0) | 10 | 15 | 5 |
seamless |
Not supported | Not supported | Not supported | Not supported | 6 |
srcdoc |
20 | 25.0 (25.0) | Not supported | 15 | ? |
allowfullscreen |
17 webkit Nightly build |
9.0 (9.0) moz 18.0 (18.0) |
Not supported | (Yes) | Not supported |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
sandbox |
2.2 | 17.0 (17.0) | 10 | Not supported | 4.2 |
seamless |
? | Not supported | Not supported | ? | ? |
srcdoc |
? | 25.0 (25.0) | Not supported | ? | ? |
allowfullscreen |
? | 9.0 (9.0)moz 18.0 (18.0) |
Not supported | Not supported | Not supported |