<iframe>

  • Revision slug: HTML/Element/iframe
  • Revision title: iframe
  • Revision id: 43092
  • Created:
  • Creator: Ecmanaut
  • Is current revision? No
  • Comment Added omitted webkitallowfullscreen attribute. Tested in Chrome 17, likely working earlier.; 22 words added

Revision Content

Summary

The HTML inline frame element (<iframe>) represents a nested browsing context, effectively embedding another HTML page into the current page. 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.

Usage context

Permitted content Normal character data (text and character references)
Tag omission None, must have both a start tag and an end tag.
Permitted parent elements Any element that accepts phrasing content.
Normative document HTML 5, section 4.8.2; HTML 4.01, section 16.5

Attributes

Like all HTML elements, this element supports the global attributes.

{{ htmlattrdef("align") }} {{ deprecated_inline() }} in {{ HTMLVersionInline("4") }}, {{ obsolete_inline() }} in {{ HTMLVersionInline("5") }}
The alignment of this element with respect to the surrounding context.
{{ htmlattrdef("frameborder") }} {{ HTMLVersionInline("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.
{{ htmlattrdef("height") }}
Indicates the height of the frame {{ HTMLVersionInline("5") }} in CSS pixels, or {{ HTMLVersionInline("4") }} in pixels or as a percentage.
{{ htmlattrdef("longdesc") }} {{ HTMLVersionInline("4") }} only
A URI of a long description of the frame. Due to widespread misuse, this is not helpful for non-visual browsers.
{{ htmlattrdef("marginheight") }} {{ HTMLVersionInline("4") }} only
The amount of space in pixels between the frame's content and its top and bottom margins.
{{ htmlattrdef("marginwidth") }} {{ HTMLVersionInline("4") }} only
The amount of space in pixels between the frame's content and its left and right margins.
{{ htmlattrdef("mozallowfullscreen") }} {{ non-standard_inline() }}
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 {{ domxref("element.mozRequestFullScreen()") }} method. If this isn't set, the element can't be placed into full screen mode.
{{ htmlattrdef("name") }}
A name for the embedded browsing context (or frame). This can be used as the value of the target attribute of an {{ HTMLElement("a") }} or {{ HTMLElement("form") }} element, or the formtarget attribute of an {{ HTMLElement("input") }} or {{ HTMLElement("button") }} element.
{{ htmlattrdef("scrolling") }} {{ HTMLVersionInline("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.
{{ htmlattrdef("sandbox") }} {{ HTMLVersionInline("5") }} 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 be 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 the same origin as the containing document. 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 from 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.
{{ htmlattrdef("seamless") }} {{ HTMLVersionInline("5") }} 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).
{{ htmlattrdef("src") }}
The URL of the page to embed.
{{ htmlattrdef("srcdoc") }} {{ HTMLVersionInline("5") }} only
The content of the page that the embedded context is to contain.
{{ htmlattrdef("width") }}
Indicates the width of the frame {{ HTMLVersionInline("5") }} in CSS pixels, or {{ HTMLVersionInline("4") }} in pixels or as a percentage.

DOM interface

Scripting

Inline frames, like {{ HTMLElement("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

<script type="text/javascript">
  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>

 Notes

{{ gecko_callout_heading("6.0") }}

Starting in Gecko 6.0 {{ geckoRelease("6.0") }}, rendering of inline frames correctly respects the borders of their containing element when they're rounded using {{ cssxref("border-radius") }}.

The above example is not valid. You cannot access iframe.contentWindow properties anymore.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
sandbox 4 {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
seamless 4 {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
srcdoc 4 {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
mozallowfullscreen {{ CompatNo() }} {{ CompatGeckoDesktop("9.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
webkitallowfullscreen 17 (maybe 15+?) {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
sandbox {{ CompatUnknown() }} {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
seamless {{ CompatUnknown() }} {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
srcdoc {{ CompatUnknown() }} {{ CompatNo() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
mozallowfullscreen {{ CompatNo() }} {{ CompatGeckoMobile("9.0") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

{{ HTML:Element_Navigation() }}

{{ languages( { "de": "de/HTML/Element/iframe" } ) }}

Revision Source

<h2>Summary</h2>
<p>The HTML inline frame element (<code>&lt;iframe&gt;</code>) represents a nested browsing context, effectively embedding another HTML page into the current page. Each browsing context has its own session history and active document. The browsing context that contains the embedded content is called the <dfn id="parent">parent</dfn> browsing context. The <dfn id="top-level">top-level</dfn> browsing context (which has no parent) is typically the browser window.</p>
<h2>Usage context</h2>
<table class="standard-table"> <tbody> <tr> <td>Permitted content</td> <td>Normal character data (text and character references)</td> </tr> <tr> <td>Tag omission</td> <td>None, must have both a start tag and an end tag.</td> </tr> <tr> <td>Permitted parent elements</td> <td>Any element that accepts <a href="/en/HTML/Content_categories#phrasing_content" title="en/HTML/Content categories#phrasing content">phrasing content</a>.</td> </tr> <tr> <td>Normative document</td> <td><a class="external" href="http://www.w3.org/TR/html5/the-iframe-element.html#the-iframe-element" title="http://www.w3.org/TR/html5/the-iframe-element.html#the-iframe-element">HTML 5, section 4.8.2</a>; <a class="external" href="http://www.w3.org/TR/html401/present/frames.html#h-16.5" title="http://www.w3.org/TR/html401/present/frames.html#h-16.5">HTML 4.01, section 16.5</a></td> </tr> </tbody>
</table>
<h2>Attributes</h2>
<p>Like all HTML elements, this element supports the <a href="/en/HTML/Global_attributes" title="en/HTML/Global attributes">global attributes</a>.</p>
<dl> <dt>{{ htmlattrdef("align") }} {{ deprecated_inline() }} in {{ HTMLVersionInline("4") }}, {{ obsolete_inline() }} in {{ HTMLVersionInline("5") }}</dt> <dd>The alignment of this element with respect to the surrounding context.</dd> <dt>{{ htmlattrdef("frameborder") }} {{ HTMLVersionInline("4") }} only</dt> <dd>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.</dd> <dt>{{ htmlattrdef("height") }}</dt> <dd>Indicates the height of the frame {{ HTMLVersionInline("5") }} in CSS pixels, or {{ HTMLVersionInline("4") }} in pixels or as a percentage.</dd> <dt>{{ htmlattrdef("longdesc") }} {{ HTMLVersionInline("4") }} only</dt> <dd>A URI of a long description of the frame. Due to widespread misuse, this is not helpful for non-visual browsers.</dd> <dt>{{ htmlattrdef("marginheight") }} {{ HTMLVersionInline("4") }} only</dt> <dd>The amount of space in pixels between the frame's content and its top and bottom margins.</dd> <dt>{{ htmlattrdef("marginwidth") }} {{ HTMLVersionInline("4") }} only</dt> <dd>The amount of space in pixels between the frame's content and its left and right margins.</dd> <dt>{{ htmlattrdef("mozallowfullscreen") }} {{ non-standard_inline() }}</dt> <dd>In Gecko 9.0 or later, this attribute can be set to <code>true</code> if the frame is allowed to be placed into full screen mode by calling its {{ domxref("element.mozRequestFullScreen()") }} method. If this isn't set, the element can't be placed into full screen mode.</dd> <dt>{{ htmlattrdef("name") }}</dt> <dd>A name for the embedded browsing context (or frame). This can be used as the value of the <strong>target</strong> attribute of an {{ HTMLElement("a") }} or {{ HTMLElement("form") }} element, or the formtarget attribute of an {{ HTMLElement("input") }} or {{ HTMLElement("button") }} element.</dd> <dt>{{ htmlattrdef("scrolling") }} {{ HTMLVersionInline("4") }} only</dt> <dd>Enumerated attribute indicating when the browser should provide a scroll bar (or other scrolling device) for the frame: <ul> <li>auto: Only when needed.</li> <li>yes: Always provide a scroll bar.</li> <li>no: Never provide a scoll bar.</li> </ul> </dd> <dt>{{ htmlattrdef("sandbox") }} {{ HTMLVersionInline("5") }} only</dt> <dd>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 be a space-separated list of tokens that lift particular restrictions. Valid tokens are: <ul> <li><code>allow-same-origin</code>: Allows the content to be treated as being from the same origin as the containing document. If this keyword is not used, the embedded content is treated as being from a unique origin.</li> <li><code>allow-top-navigation</code>: Allows the embedded browsing context to navigate (load) content from the top-level browsing context. If this keyword is not used, this operation is not allowed.</li> <li><code>allow-forms</code>: Allows the embedded browsing context to submit forms. If this keyword is not used, this operation is not allowed.</li> <li><code>allow-scripts</code>: 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.</li> </ul> </dd> <dt>{{ htmlattrdef("seamless") }} {{ HTMLVersionInline("5") }} only</dt> <dd>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 <code>&lt;iframe&gt;</code> 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).</dd> <dt>{{ htmlattrdef("src") }}</dt> <dd>The URL of the page to embed.</dd> <dt>{{ htmlattrdef("srcdoc") }} {{ HTMLVersionInline("5") }} only</dt> <dd>The content of the page that the embedded context is to contain.</dd> <dt>{{ htmlattrdef("width") }}</dt> <dd>Indicates the width of the frame {{ HTMLVersionInline("5") }} in CSS pixels, or {{ HTMLVersionInline("4") }} in pixels or as a percentage.</dd>
</dl>
<h2>DOM interface</h2>
<ul> <li><a href="/en/DOM/HTMLIFrameElement" title="en/DOM/HTMLIFrameElement">HTMLIFrameElement</a></li>
</ul>
<h2>Scripting</h2>
<p>Inline frames, like {{ HTMLElement("frame") }} elements, enter the <code><a class="internal" href="/en/DOM/window.frames" title="en/DOM/window.frames">window.frames</a></code> pseudo-array.</p>
<p>From the DOM <code>iframe</code> element, scripts can get access to the <a class="internal" href="/en/DOM/window" title="en/DOM/window"><code>window</code></a> object of the included HTML page via the <code>contentWindow</code> property. The <code>contentDocument</code> property refers to the document element inside the <code>iframe</code> (this is equivalent to <code>contentWindow.document</code>), but is not supported by Internet Explorer versions before IE8.</p>
<p>From the inside of a frame, a script can get a reference to the parent window via <code><a class="internal" href="/en/DOM/window.parent" title="en/DOM/window.parent">window.parent</a></code>.</p>
<p>Scripts trying to access a frame's content are subject to the <a class="internal" href="/en/Same_origin_policy_for_JavaScript" title="en/Same origin policy for JavaScript">same-origin policy</a>, 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 <code><a class="internal" href="/en/DOM/window.postMessage" title="en/DOM/window.postMessage">window.postMessage</a></code>.</p>
<h2>Example</h2>
<pre class="brush: html">&lt;script type="text/javascript"&gt;
  var iframe = window.getElementsByTagName( "iframe" )[ 0 ];
  alert( "Frame title: " + iframe.contentWindow.title );
&lt;/script&gt;


&lt;iframe src="page.html" width="300" height="300"&gt;
  &lt;p&gt;Your browser does not support iframes.&lt;/p&gt;
&lt;/iframe&gt;</pre>
<h2> Notes</h2>
<div class="geckoVersionNote"> <p>{{ gecko_callout_heading("6.0") }}</p> <p>Starting in Gecko 6.0 {{ geckoRelease("6.0") }}, rendering of inline frames correctly respects the borders of their containing element when they're rounded using {{ cssxref("border-radius") }}.</p> <p>The above example is not valid. You cannot access <code>iframe.contentWindow</code> properties anymore.</p>
</div>
<h2 name="Browser_Compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Chrome</th> <th>Firefox (Gecko)</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari (WebKit)</th> </tr> <tr> <td>Basic support</td> <td>1.0</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> </tr> <tr> <td><code>sandbox</code></td> <td>4</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>seamless</code></td> <td>4</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>srcdoc</code></td> <td>4</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>mozallowfullscreen</code></td> <td>{{ CompatNo() }}</td> <td>{{ CompatGeckoDesktop("9.0") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> <tr> <td><code>webkitallowfullscreen</code></td> <td>17 (maybe 15+?)</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Android</th> <th>Firefox Mobile (Gecko)</th> <th>IE Phone</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> </tr> <tr> <td><code>sandbox</code></td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>seamless</code></td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>srcdoc</code></td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> <tr> <td><code>mozallowfullscreen</code></td> <td>{{ CompatNo() }}</td> <td>{{ CompatGeckoMobile("9.0") }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> <td>{{ CompatNo() }}</td> </tr> </tbody> </table>
</div>
<p>{{ HTML:Element_Navigation() }}</p>
<p>{{ languages( { "de": "de/HTML/Element/iframe" } ) }}</p>
Revert to this revision