<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

AttributeValueDescription
alignleftrighttopbottommiddleSpecifies how text is aligned and wrapped around the frame.
Not supported in HTML5.
allowfullscreenDefines that the frame can be opened in a full screen mode.
frameborder10Defines if the iframe border around the frame should be displayed or not.
Not supported in HTML 5.
heightpixelsDefines the height of the frame (default height 150 pix).
longdescURLDefines a page which has a long description of the content.
Not supported in HTML 5.
marginheightpixelsDefines the top and bottom margins of the frame.
Not supported in HTML 5.
marginwidthpixelsDefines the left and right margins of the frame.Not supported in HTML 5.
nametextDefines the name of the frame.
sandbox
Brings extra restrictions for the content inside the frame.
""Applies all restrictions.
allow-formsAllows submission of forms or an embedded page.
allow-same-originConsiders the attached document as a document downloaded from the same source as the parent document.
allow-scriptsEnables execution of scripts on a nested page.
allow-top-navigationAllows the contents of the attached document to access top-level elements (documents, windows).
scrollingyesnoautoDefines whether the scroll bar should be displayed or not.
Not supported in HTML 5.
seamlessseamlessSpecifies that the contents of the attached document should be displayed as part of the parent document.
srcURLSpecifies the address of the document whose contents will be loaded into the frame.
srcdocHTML_codeStores the contents of the frame directly in the attribute.
widthpixelsDefines the width of the frame. (default width is 300px).

The <iframe> tag supports the Global attributes and the Event Attributes.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部