Definition and Usage
The <iframe> tag creates an inline frame within a web page, allowing another document to be embedded and navigated independently.
For example, it can be used to embed YouTube videos or Google Maps.
The <iframe> tag is short for "inline frame".
"Inline" means that it is positioned within the flow of the page's other content, while "frame" refers to a container in which content is displayed independently.
Basic Structure
The basic structure of the <iframe> tag is as follows:
<iframe src="URL of the document to embed"></iframe>
The <iframe> tag uses the src attribute to specify the URL of the document to embed.
Examples
YouTube Videos
<iframe
src="https://www.youtube.com/embed/M7lc1UVf-VE?si=Kgf3a19a9LJAeSeo"
width="300"
height="150"
title="YouTube video player"
style="border: none;"
referrerpolicy="strict-origin-when-cross-origin"
allow="fullscreen"></iframe>
Google Maps
<iframe
src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d1657.5047190881044!2d-117.92097027358513!3d33.81206951440678!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x80dcd7d12b3b5e6b%3A0x2ef62f8418225cfa!2z65SU7KaI64uI656c65OcIO2MjO2BrA!5e0!3m2!1sko!2skr!4v1721806287983!5m2!1sko!2skr"
width="300"
height="200"
style="border: none;"
loading="lazy"
referrerpolicy="no-referrer-when-downgrade"
allow="fullscreen"></iframe>
Embedding Another Web Page
The <iframe> tag displays a document within the frame when you specify the URL of the document to embed using the src attribute.
The following example embeds the page at https://example.org in an <iframe> tag.
<iframe
src="https://example.org/"
title="iframe example"
width="300"
height="200"></iframe>
The <iframe> Tag Creates an Independent Frame
An independent frame means that the web page and the frame created by the <iframe> tag operate in separate environments. This has several important implications, including the following.
Style and Script Isolation
- The content inside the
<iframe>tag is not affected by the styles and scripts of the embedding web page. - n other words, the appearance and behavior of the content inside the
<iframe>tag can be configured independently of the embedding web page. - This is useful for integrating web pages with different designs into a single web page or embedding specific functionality.
Cookie and Storage Data Isolation
- Unlike the embedding web page, the content inside an
<iframe>tag can have its own cookies and storage data. - In other words, the storage data within the
<iframe>tag is managed separately from that of the embedding web page. - However, for security reasons, if the content inside the
<iframe>tag and the embedding web page have different origins, access to each other's cookies and storage data may be restricted. - However, for security reasons, if the content inside the
<iframe>tag and the embedding web page have different origins, access to each other's cookies and storage data may be restricted. - This is an important feature for privacy protection and helps safely embed content from different websites.
Document Object Model (DOM) Isolation
- Unlike the embedding web page, the content inside an
<iframe>tag has its own DOM tree. - In other words, elements inside the
<iframe>tag have a separate hierarchy from the DOM elements of the embedding web page, and each DOM can be manipulated independently. - This helps reduce the complexity of a web page and makes code easier to manage.
Enhanced Security
- An independent frame can help protect a web page from malicious code.
- Even if the content inside an
<iframe>tag contains malicious code, restrictions on access between documents with different origins can help reduce the impact of that code on the embedding web page. - However, embedding untrusted content using the
<iframe>tag can pose security risks, so caution is necessary.
Using Related Attributes
Let's learn about the attributes related to the <iframe> tag.
src Attribute
Specifies the URL of the document to embed.
This URL can be the address of a page on another website or a different page on the same website.
Example
The following example embeds the https://www.example.com page within a web page using the <iframe> tag:
<iframe src="https://www.example.com"></iframe>
Not a Required Attribute
Although the src attribute plays a very important role in specifying the URL of the document to embed in the <iframe> tag, it is not a required attribute. In other words, you can use the <iframe> tag without the src attribute.
If the src attribute is not specified, the <iframe> tag simply does not embed a document from an external source.
You can also directly insert a document into the <iframe> tag using the srcdoc attribute or JavaScript, without embedding a document from an external source.
The following example uses JavaScript to embed a document in the <iframe> tag without using the src attribute:
<iframe id="my-iframe"></iframe>
<script>
const iframe = document.getElementById("my-iframe");
const iframeDoc = iframe.contentWindow.document;
iframeDoc.open();
iframeDoc.write("<p>I inserted a document using JavaScript!</p>");
iframeDoc.close();
</script>
srcdoc Attribute
The srcdoc attribute specifies the inline HTML content to embed in the <iframe> tag. When this attribute is used, it takes precedence over the src attribute.
The HTML included in the srcdoc attribute must follow the syntax of a complete HTML document, but in most cases, you only need to include the content of the <body> tag. In other words, you generally only need to include the content you want to display.
The following example uses the srcdoc attribute to embed a document in the <iframe> tag without using the src attribute:
<iframe srcdoc="<p>Hello, world!</p>"></iframe>
width Attribute
Specifies the <iframe> width in CSS pixels. The value must be a unitless integer.
<iframe src="https://www.example.com" width="600"></iframe>
If the width attribute is not specified, the browser uses its default width.
When the HTML width attribute and the CSS width property are both used, CSS takes precedence. CSS has a higher priority than the HTML width attribute, so when the image width is specified in CSS, the HTML width attribute is ignored.
height Attribute
Specifies the <iframe> height in CSS pixels. The value must be a unitless integer.
<iframe src="https://www.example.com" height="300"></iframe>
If the height attribute is not specified, the browser uses its default height.
When the HTML height attribute and the CSS height property are both used, CSS takes precedence. CSS has a higher priority than the HTML height attribute, so when the image height is specified in CSS, the HTML height attribute is ignored.
frameborder Attribute
## frameborder Attribute
Specifies whether to display a border around the <iframe> tag. The value is set to 0 or 1.
0 |
No border |
|---|---|
1 |
Border |
<iframe src="https://www.example.com" frameborder="0"></iframe>
The frameborder attribute is deprecated in HTML5. Instead, using CSS to control the border of the <iframe> tag is recommended.
border property.
<iframe src="https://www.example.com" style="border: none;"></iframe>
allowfullscreen Attribute
Specifies whether to allow the content inside the <iframe> tag to use fullscreen mode.
This attribute allows fullscreen mode to be activated by calling the JavaScript requestFullscreen() method.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<iframe src="https://www.example.com" allowfullscreen></iframe>
Note!
According to MDN, the allowfullscreen attribute has been redefined as allow="fullscreen *". To follow modern web standards, it is recommended to use allow="fullscreen" instead of allowfullscreen.
<iframe src="https://www.example.com" allow="fullscreen"></iframe>
sandbox Attribute
Specifies restrictions on the permissions of content inside the <iframe> tag.
This sets security restrictions to prevent content inside the <iframe> tag from performing certain functions. Multiple attribute values can be used to configure the restrictions in detail.
Values
allow-forms |
Allows forms represented by the <form> tag to be submitted from the embedded document. |
|---|---|
allow-modals |
Allows modal windows implemented in JavaScript (Window.alert(), Window.confirm(), and Window.prompt()) to be opened from the embedded document. However, the <dialog> element can be opened regardless of this value. It also allows the document to receive the BeforeUnloadEvent event. |
allow-pointer-lock |
Allows the Pointer Lock API to be used from the embedded document. |
allow-popups |
Allows popups (for example, those created using JavaScript's Window.open() method) to be opened from the embedded document. |
allow-same-origin |
Allows the embedded document to retain its own origin. |
allow-scripts |
Allows scripts to run in the embedded document. |
allow-top-navigation |
Allows the embedded document to navigate the top-level browsing context. |
allow-popups-to-escape-sandbox |
Used together with the allow-popups value. Adding this value allows content inside the <iframe> tag to escape the sandbox through popup windows and interact with the surrounding web page. In other words, popup windows are no longer restricted by the sandbox and can operate as independent web pages. |
The sandbox attribute can have multiple space-separated values.
<iframe src="https://www.example.com" sandbox="allow-scripts allow-forms"></iframe>
Caution
Using the allow-scripts and allow-same-origin values together in an <iframe> tag is not recommended. This is because, when the document in the <iframe> is served from the same origin, this combination can allow the document to remove the sandbox attribute. This disables the security provided by the sandbox attribute, making the security no different from using an <iframe> without sandbox restrictions. In other words, it is as if no sandbox restrictions were applied at all.
referrerpolicy Attribute
Specifies how referrer information is handled in requests made when the <iframe> tag is loaded.
Values
strict-origin-when-cross-origin |
Default value.
Strictly controls how the Referer header is sent for cross-origin requests.
Sends the full URL for same-origin requests and sends only the origin for requests where the security level remains the same (HTTPS→HTTPS). It does not send the header to less secure destinations (HTTPS→HTTP). |
|---|---|
no-referrer |
Does not send referrer information. |
origin |
Sends only the origin as referrer information. The origin includes the scheme, host, and port. |
origin-when-cross-origin |
Sends the full URL for same-origin requests and only the origin for cross-origin requests. The origin includes the scheme, host, and port. |
unsafe-url |
Sends the full URL, but does not include the fragment, username, or password. This value is unsafe because URL information from a resource protected by TLS may be exposed to a less secure origin. |
no-referrer-when-downgrade |
Does not send referrer information to less secure destinations (HTTPS→HTTP). Otherwise, it sends the full URL. |
strict-origin |
Sends only the document's origin as referrer information when the security level remains the same (HTTPS→HTTPS), but does not send it to less secure destinations (HTTPS→HTTP). |
name Attribute
The name attribute specifies a name that identifies the browsing context of the frame.
This name is used to specify a browsing context as the navigation target through the target attribute of the <a> and <form> tags, the target attribute of the <base> tag, and JavaScript methods such as window.open().
<iframe src="https://www.example.com" name="example"></iframe>
Accessibility Considerations
You can use the title attribute to provide accessibility-related information, such as a description of the embedded content in the <iframe> tag, to users who are blind and use screen readers.
<iframe title="Wikipedia page about HTML" src="https://en.wikipedia.org/wiki/HTML"></iframe>
Technical Summary
| Permitted parents | Any element that accepts embedded content. |
|---|---|
| Permitted content | Nothing. |
Implicit ARIA role |
No corresponding role |
Permitted ARIA roles |
application,
document,
img,
none,
presentation |
Browser Compatibility
| Tag and Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<iframe>
|
1 | 12 | 1 | 4 |
allow
|
60 | 79 | 74 | 11.1 |
allowfullscreen
|
38 | 12 | 18 | 10.1 |
width
|
1 | 12 | 1 | 3 |
height
|
1 | 12 | 1 | 3 |
src
|
1 | 12 | 1 | 4 |
srcdoc
|
20 | 79 | 25 | 6 |
sandbox
|
5 | 12 | 17 | 5 |
referrerpolicy
|
51 | 79 | 50 | 14 |
name
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
<iframe>
|
HTML Standard #the-iframe-element |