An iframe, or Inline Frame, is an HTML element represented by the <iframe> tag. It functions as a 'window' on your webpage through which visitors can view and interact with another webpage from a different source.
iframes are used for various purposes like:
<iframe src="URL" title="description"></iframe>
The src attribute specifies the URL of the document you want to embed, and iframes can include videos, maps, or entire web pages from other sources.
<iframe> Tag| Attribute | Description |
|---|---|
src |
Specifies the URL of the document to embed in the <iframe>. |
height |
Sets the height of the <iframe> element. |
width |
Sets the width of the <iframe> element. |
name |
Specifies the name of the <iframe> for targeting its content or for referencing it in JavaScript. |
loading |
Specifies how the content of the <iframe> should be loaded. |
scrolling |
Controls whether or not the <iframe> should have scrollbars. |
sandbox |
Specifies an extra set of restrictions for the content in the <iframe>. |
srcdoc |
Specifies the HTML content of the page to display in the <iframe>. |
allow |
Specifies a set of extra restrictions on the content that can be loaded in an <iframe>. |
allowfullscreen |
Indicates whether the <iframe> can be displayed in fullscreen mode. |
allowpaymentrequest |
Enables payment requests for content inside the <iframe>. |
referrerpolicy |
Sets the referrer policy for the <iframe> content. |
HTML iframes are used to embed external web pages, videos, or documents inside a webpage without reloading the main page.
In this example, an iframe is used to display another webpage within the current webpage.
<!DOCTYPE html>
<html>
<head>
<title>HTML iframe Tag</title>
</head>
<body style="text-align: center">
<h2>HTML iframe Tag</h2>
<iframe src="https://www.example.com"
height="370"
width="400">
</iframe>
</body>
</html>
src: Specifies the URL of the page to display within the iframe.width and height: Defines the size of the iframe on your page.The height and width attributes are used to specify the size of the iframe. The attribute values are specified in pixels by default. You can use pixels or percentages (e.g., “80%”).
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>Content goes here</p>
<iframe src="https://www.example.com"
height="395"
width="400">
</iframe>
</body>
</html>
By default, iframe has a border around it. To remove the border, we must use the style attribute and use the CSS border property.
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>Content goes here</p>
<iframe src="https://www.example.com"
height="300"
width="400"
style="border: none">
</iframe>
</body>
</html>
You can change the size, style, and color of the iframe border using CSS.
<!DOCTYPE html>
<html>
<body>
<p>Content goes here</p>
<iframe src="https://www.example.com"
height="400"
width="400"
style="border: 4px solid orange">
</iframe>
</body>
</html>
You can target an iframe with links by using the name attribute of the iframe and the target attribute of the link.
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>Click the link text</p>
<iframe src="https://via.placeholder.com/350x400.png?text=Original+Content"
height="400"
width="350"
name="iframe_a">
</iframe>
<p>
<a href="https://www.example.com" target="iframe_a">Load Example in Iframe</a>
</p>
</body>
</html>
While <iframe> elements provide flexibility for embedding external content, they should be used carefully to maintain performance, security, and accessibility.
sandbox attribute to restrict iframe capabilities unless the source is fully trusted.title attributes inside the <iframe> tag for users who cannot interact with it (e.g., those using screen readers).Which attribute defines the URL of the document you want to embed in an <iframe>?