<iframe>
HTML <iframe> Tag
The <iframe> tag creates an inline frame for embedding third-party content (media, applets, etc.). Although the content of the frame and the web page are independent, they can interact through JavaScript.
Syntax
Syntax
The <iframe> tag comes in pairs. The content is written between the opening (<iframe>) and closing (</iframe>) tags.
Example of an HTML <iframe> Tag:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<iframe src="https://www.w3cdoc.com"></iframe>
</body>
</html>
To set the size of iframe, use the height and width attributes, or use CSS. The attribute values are set in pixels by default, but they can also be in percent.
Example of an HTML <iframe> Tag With the Height and Width Attributes
<!DOCTYPE html>:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<iframe src="https://www.w3cdoc.com" width="80%" height="300"></iframe>
</body>
</html>
By default, an iframe is surrounded by a border. To remove the border, you can use CSS border property.
Example of an HTML <iframe> Tag With the CSS Border Property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<iframe src="https://www.w3cdoc.com" width="80%" height="300" style="border: none"></iframe>
</body>
</html>
The new loading attribute
The new loading attribute
There is a new HTML loading attribute that allows deferring image and iframe loading until they are close to being shown. For this feature, the WHATWG has a pull request, and it is already a part of Chromium (as of v76).
Supported values for the loading attribute include:
“lazy”, which defers the load until the image or iframe reaches a distance threshold from the viewport.
“eager”, which loads the resource immediately.
“auto”, which is the default behavior, eagerly load the resource.
You can use the lazy value to take advantage of browser-native lazy loading:
<iframe src="video-player.html" loading="lazy"></iframe>
Lazy loading is a set of techniques in web and application development that defers the loading of resources on a page to a later point in time when those resources are needed instead of loading them upfront. These techniques help in improving performance, better utilization of the device’s resources, and reducing associated costs.
Attributes
Attributes
Attribute | Value | Description |
---|---|---|
align | leftrighttopbottommiddle | Specifies how text is aligned and wrapped around the frame. |
Not supported in HTML5. | ||
allowfullscreen | Defines that the frame can be opened in a full screen mode. | |
frameborder | 10 | Defines if the iframe border around the frame should be displayed or not. |
Not supported in HTML 5. | ||
height | pixels | Defines the height of the frame (default height 150 pix). |
longdesc | URL | Defines a page which has a long description of the content. |
Not supported in HTML 5. | ||
marginheight | pixels | Defines the top and bottom margins of the frame. |
Not supported in HTML 5. | ||
marginwidth | pixels | Defines the left and right margins of the frame.Not supported in HTML 5. |
name | text | Defines the name of the frame. |
sandbox | ||
Brings extra restrictions for the content inside the frame. | ||
"" | Applies all restrictions. | |
allow-forms | Allows submission of forms or an embedded page. | |
allow-same-origin | Considers the attached document as a document downloaded from the same source as the parent document. | |
allow-scripts | Enables execution of scripts on a nested page. | |
allow-top-navigation | Allows the contents of the attached document to access top-level elements (documents, windows). | |
scrolling | yesnoauto | Defines whether the scroll bar should be displayed or not. |
Not supported in HTML 5. | ||
seamless | seamless | Specifies that the contents of the attached document should be displayed as part of the parent document. |
src | URL | Specifies the address of the document whose contents will be loaded into the frame. |
srcdoc | HTML_code | Stores the contents of the frame directly in the attribute. |
width | pixels | Defines the width of the frame. (default width is 300px). |
The <iframe> tag supports the Global attributes and the Event Attributes.