iframe HTML

HTML <iframe> Tag

To display a web page within a web page or a nested web page, the HTML <iframe> Tag is used. HTML Iframes are also known as Inline frames. As the name itself suggests, it defines an inline frame.

Syntax:

<iframe src="URL"></iframe>

Parameters:

  • src: It is an attribute used to specify the URL.
  • URL: It defines the web address of the inline frame page.

Set Height and Width:

The height and width attributes are used to specify the iframe’s size. The size is defined in pixels by default.

Example: In Pixels.

<!DOCTYPE html>
<html>
<body>
<iframe src="https://www.w3schools.blog/" height="300" width="400"></iframe>
</body>
</html>

Explanation:

In the above example, we set the width and height of iframe in pixels.

Example: In Percentage.

<!DOCTYPE html>
<html>
<body>
<iframe src="https://www.w3schools.blog/" height="100%" width="60%"></iframe>
</body>
</html>

Explanation:

In the above example, we set the width and height of iframe in percentage.

Example: With CSS.

<!DOCTYPE html>
<html>
<body>
<iframe src="https://www.w3schools.blog/" style="height:300px; width:400px"></iframe>
</body>
</html>

Explanation:

In the above example, we set the width and height of the iframe using CSS.

Remove the Iframe Border:

The style attribute and the CSS border property can be used to remove the default border present around an Iframe.

Example:

<!DOCTYPE html>
<html>
<body>
<iframe src="https://www.w3schools.blog" style="height:300px; width:400px; border:none;"></iframe>
</body>
</html>

Explanation:

In the above example, we removed the default border present around the Iframe.

Modify the Iframe Border:

The style attribute and the CSS border property can be used to modify the size, style, and color of the default border present around an Iframe.

Example:

<!DOCTYPE html>
<html>
<body>
<iframe src="https://www.w3schools.blog/" style="height:300px; width:400px; border:4px solid crimson;"></iframe>
</body>
</html>

Explanation:

In the above example, we modified the default border present around the Iframe.

Target Frame for a Link:

To use an iframe as the target frame for a link, the specified target attribute of the link should refer to the name attribute of the iframe.

Example:

<!DOCTYPE html>
<html>
<body>
<iframe src="example.html" height="300px" width="400" name="iframe_example"></iframe>
<p><a href="https://www.w3schools.blog/" target="iframe_example" rel="noopener">w3schools.com</a></p>
</body>
</html>

Explanation:

In the above example, the respective link opens in the iframe on clicking, since the specified target of the respective link matches the name of the iframe.

Iframe to Embed YouTube video:

To add a YouTube video on a webpage follow the below steps:

  • Open the YouTube video to embed.
  • Click on the SHARE video option and then on the Embed option.
  • Copy and paste the generated HTML code into the desired HTML file.
  • If required, modify the height, width, and other properties of the video.

Example:

<!DOCTYPE html>
<html>
<body style="color: crimson">
<iframe width="560" height="315" src="https://www.youtube.com/embed/m8hDA--txk4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>  
<h2>Click on the video to watch.</h2>
</body>
</html>

Explanation:

In the above example, we embedded a YouTube video on a webpage.

Iframe attributes:

Below are the various attributes used with an HTML <iframe> tag.

Attribute Value Uses
allowfullscreen TRUE/ FALSE Used to open frame in full screen.
height Pixels Used to specify the height of the iframe (default height = 150 px).
name text Used to provide a name to the iframe.
frameborder 1 or 0 Used to specify whether an iframe should have a border or not.
width Pixels Used to specify the width of the embedded frame (default width is 300 px).
src URL Used to define the web address of the inline frame page.
sandbox Used to apply extra restrictions for the content of a frame.
sandbox allow-forms Used to allow submission of the form which otherwise is blocked.
sandbox allow-popups Used to enable popups that otherwise will not open.
sandbox allow-scripts Used to enable the script to run.
sandbox allow-same-origin Used to treat the embedded resource as downloaded from the same source.
srcdoc Used to show the HTML content in the inline iframe.
scrolling Used to specify whether to provide a scroll bar for the iframe or not.
scrolling auto Used to display if the content of the iframe is larger than its dimensions.
scrolling yes Used to always display the scroll bar for the iframe.
scrolling no Used to never display the scrollbar for the iframe.

Global Attributes:

The HTML GLobal attributes are supported by the HTML <iframe> tag.

Event Attributes:

The HTML Event attributes are supported by the HTML <iframe> tag.

Supporting Browsers:

Chrome, IE, Firefox, Opera, and Safari.