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:

Or using the srcdoc attribute:

Using the src, width, and height attributes:

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 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.

The <iframe> element accepts the following attributes.

AttributeDescription
srcLocation of the frame contents (for example, the HTML page to be loaded into the frame).
srcdoc

Inline HTML to embed, overriding the src attribute.

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.
allow-presentationLets the resource start a presentation session.
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).
allowpaymentrequestWhether the iframe's contents are allowed to use the Payment Request API to make payment requests.
widthSpecifies the width of the inline frame.
heightSpecifies the height of the inline frame.

Global Attributes

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

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

Event Handlers

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.

Most event handler content attributes can be used on all HTML elements, but some event handlers have specific rules around when they can be used and which elements they are applicable to.

For more detail, see HTML event handler content attributes.