HTML <iframe> Tag

The HTML <iframe> tag represents a nested browsing context in an HTML document.

The <iframe> tag allows you to embed another document within the current HTML document. It also allows you to provide a nested browsing context without using another document - by simply passing the content to the <iframe> via the srcdoc attribute.

Prior to HTML5, the <iframe> tag was said to create an inline frame. The HTML5 specification does not use the term inline frame, but uses nested browsing context instead.

Syntax

The <iframe> tag is written as <iframe></iframe> with the applicable attributes inserted into the start tag.

Like this:

<iframe src="">
</iframe>

Or using the srcdoc attribute:

<iframe srcdoc="">
</iframe>

Using the src, width, height, and seamless attributes:

<iframe src="" width="" height="" seamless>
</iframe>

Examples

Basic tag usage

The following example uses the src attribute to specify an external document that the nested browsing context is to contain. In this example, we don't use the width or height attribute.

The width and height Attributes

Here, we add the width and height attributes to the previous example.

The seamless Attribute

The WHATWG HTML Living Standard and HTML 5.1 introduced the seamless attribute. The seamless attribute specifies that the element's browsing context is to be rendered in a manner that makes it appear to be part of the containing document (seamlessly included in the parent document). This results in the document's styles being applied to the nested content. It also results in any links being opened in the <iframe> element's parent browsing context.

Note that at the time of writing, browser support for the seamless attribute is poor.

The srcdoc Attribute

You can use the srcdoc attribute to specify the content that should appear in the nested browsing context.

Note that <!doctype> and <title> are optional when using the srcdoc attribute within a <iframe> tag (they are required on most other HTML documents).

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <iframe> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <iframe> tag/element.

AttributeDescription
srcLocation of the frame contents (for example, the HTML page to be loaded into the frame).
srcdocContent of the page that the inline frame is to contain. This (optional) attribute was introduced to allow embedding of potentially hostile content inline. It is expected to be used together with the sandbox and seamless attributes.

If the browser doesn't support the srcdoc attribute, it will use the URL provided by the src attribute instead, if supplied and valid, otherwise the <iframe> will remain blank.

nameAssigns a name to a frame. This is useful for loading contents into one frame from another.
sandboxEnables a set of extra restrictions on any content hosted by the <iframe>. The value of the sandbox attribute can be either the empty string (all the restrictions are applied), or a space-separated list of tokens that remove each respective restriction.

Possible values:

AttributeDescription
(empty string)By using the empty string, all sandbox restrictions are applied.
allow-top-navigationAllows the nested browsing context to navigate (load) content to the top-level browsing context.
allow-same-originAllows the content to be treated as being from its normal origin. Without this token, the content is forced into a unique origin, thus preventing it from accessing other content from the same origin. Also, without the allow-same-origin token, scripts are prevented from reading from or writing to the document.cookie IDL attribute, and blocks access to localStorage and openDatabase(). [WEBSTORAGE] [WEBSQL].
allow-formsAllows form submission (i.e. the nested browsing context can submit forms).
allow-scriptsAllows script execution (but not popup windows).
allow-pointer-lockEnables Pointer lock.

Pointer lock provides input methods based on the movement of the mouse over time, not just the absolute position of the mouse cursor. Pointer lock is useful for applications that require significant mouse input to control movements, rotate objects, and change entries. It is particularly essential for highly visual applications, such as those that use first-person perspective, as well as 3D views and modeling. By default, sandboxed iframes block Pointer lock. This attribute allows you to enable Pointer lock on sandboxed iframes.

allow-popupsAllows popup windows.
seamlessAllows the inline frame to appear as though it is being rendered as part of the containing document. For example, borders and scrollbars will not appear.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either seamless or seamless="seamless").

Possible values:

  • [Empty string]
  • seamless
Note that this attribute is only supported by the WHATWG HTML Living Standard. It is not supported by the W3C HTML5 specification.
allowfullscreenSpecifies that Document objects in the <iframe> element's browsing context are to be allowed to use requestFullscreen() (if it's not blocked for other reasons, e.g. there is another ancestor <iframe> without this attribute set).
Note that this attribute is only supported by the WHATWG HTML Living Standard. It is not supported by the W3C HTML5 specification.
widthSpecifies the width of the inline frame.
heightSpecifies the height of the inline frame.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <iframe> tag , as well as with all other HTML tags.

  • accesskey
  • class
  • contenteditable
  • contextmenu
  • dir
  • draggable
  • dropzone
  • hidden
  • id
  • inert
  • itemid
  • itemprop
  • itemref
  • itemscope
  • itemtype
  • lang
  • spellcheck
  • style
  • tabindex
  • title
  • translate

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <iframe> element, as well as any other HTML5 element.

  • onabort
  • oncancel
  • onblur
  • oncanplay
  • oncanplaythrough
  • onchange
  • onclick
  • oncontextmenu
  • ondblclick
  • ondrag
  • ondragend
  • ondragenter
  • ondragexit
  • ondragleave
  • ondragover
  • ondragstart
  • ondrop
  • ondurationchange
  • onemptied
  • onended
  • onerror
  • onfocus
  • onformchange
  • onforminput
  • oninput
  • oninvalid
  • onkeydown
  • onkeypress
  • onkeyup
  • onload
  • onloadeddata
  • onloadedmetadata
  • onloadstart
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup
  • onmousewheel
  • onpause
  • onplay
  • onplaying
  • onprogress
  • onratechange
  • onreadystatechange
  • onscroll
  • onseeked
  • onseeking
  • onselect
  • onshow
  • onstalled
  • onsubmit
  • onsuspend
  • ontimeupdate
  • onvolumechange
  • onwaiting

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

HTML5 introduced the following attributes:

  • srcdoc
  • sandbox

Both HTML 5.1 and the HTML Living Standard have introduced the following attributes:

  • seamless
  • allowfullscreen

HTML5 does not support the following attributes (which were supported in HTML 4 and earlier):

  • align
  • longdesc
  • frameborder
  • scrolling
  • marginwidth
  • marginheight

Also, in HTML 4 and previous versions you could create "fallback content" by placing content within the opening and closing <iframe> </iframe> tags. In HTML5 however, the <iframe> element never has fallback content.

To see more detail on the HTML 4 and HTML5 versions see HTML5 <iframe> Tag and HTML4 <iframe> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <iframe> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <iframe> Tag and HTML4 <iframe> Tag.

<iframe  
 src=""
 srcdoc=""
 name=""
 sandbox=""
 width=""
 height=""
 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>
</iframe>

Tag Details

For more details about the <iframe> tag, see HTML5 <iframe> Tag and HTML4 <iframe> Tag.

Specifications

Here are the official specifications for the <iframe> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.