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.
| Attribute | Description | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
src | Location of the frame contents (for example, the HTML page to be loaded into the frame). | ||||||||||||||||||
srcdoc | Inline HTML to embed, overriding the If the browser doesn't support the |
||||||||||||||||||
name | Assigns a name to a frame. This is useful for loading contents into one frame from another. | ||||||||||||||||||
sandbox | Enables 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:
|
||||||||||||||||||
allowfullscreen | Specifies 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).
|
||||||||||||||||||
allowpaymentrequest | Whether the iframe's contents are allowed to use the Payment Request API to make payment requests.
|
||||||||||||||||||
width | Specifies the width of the inline frame. | ||||||||||||||||||
height | Specifies 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.
accesskeyautocapitalizeclasscontenteditabledata-*dirdraggablehiddenidinputmodeisitemiditempropitemrefitemscopeitemtypelangpartslotspellcheckstyletabindextitletranslate
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.
onabortonauxclickonbluroncanceloncanplayoncanplaythroughonchangeonclickoncloseoncontextmenuoncopyoncuechangeoncutondblclickondragondragendondragenterondragexitondragleaveondragoverondragstartondropondurationchangeonemptiedonendedonerroronfocusonformdataoninputoninvalidonkeydownonkeypressonkeyuponlanguagechangeonloadonloadeddataonloadedmetadataonloadstartonmousedownonmouseenteronmouseleaveonmousemoveonmouseoutonmouseoveronmouseuponpasteonpauseonplayonplayingonprogressonratechangeonresetonresizeonscrollonsecuritypolicyviolationonseekedonseekingonselectonslotchangeonstalledonsubmitonsuspendontimeupdateontoggleonvolumechangeonwaitingonwheel
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.