HTML <iframe> Tag

The HTML <iframe> tag is used to embed another HTML document within the current webpage. This powerful tag allows developers to display external content such as videos, maps, or entire web pages inside a frame without leaving the original site. By using the <iframe> tag, you can enhance the user experience by integrating rich media or dynamic content from external sources directly into your web pages.

Syntax of the <iframe> Tag

html
<iframe src="URL" width="width" height="height"></iframe>
  • src: Specifies the URL of the content to be embedded.
  • width: Sets the width of the iframe.
  • height: Sets the height of the iframe.

Developers can also add additional attributes to control the behaviour and appearance of the iframe.

Attributes of the <iframe> Tag

The <iframe> tag supports several useful attributes that allow developers to customize the appearance and behavior of the embedded content. Here’s a list of key attributes:

AttributeDescription
srcSpecifies the URL of the embedded content.
widthDefines the width of the iframe (in pixels or percentage).
heightDefines the height of the iframe (in pixels or percentage).
frameborderSpecifies whether the iframe should have a border (0 means no border, 1 means border).
allowfullscreenEnables fullscreen mode for the embedded content, if supported by the browser or media.
sandboxApplies extra restrictions on the content inside the iframe (e.g., no scripts, forms, etc.).
nameDefines a name for the iframe, useful for targeting form submissions or links.
loadingControls when the iframe should load (values: "lazy" for delayed loading, "eager" for immediate).
referrerpolicySpecifies how much referrer information should be passed when navigating the iframe content.
allowSpecifies permissions for the iframe content, such as autoplay, camera, or microphone access.

Examples of HTML <iframe> Tag

Example 1: Embedding a YouTube Video

html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Embedded YouTube Video</title>
</head>
<body>
    <h1>Watch This Video</h1>
    <iframe width="560" height="315" src="https://www.youtube.com/embed/VIDEO_ID" 
            frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
            allowfullscreen>
    </iframe>
</body>
</html>

In this example, a YouTube video is embedded inside the iframe using the video URL.

Example 2: Embedding Google Maps

html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Embedded Google Map</title>
</head>
<body>
    <h1>Our Location</h1>
    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!..." 
            width="600" height="450" frameborder="0" style="border:0;" 
            allowfullscreen="" aria-hidden="false" tabindex="0">
    </iframe>
</body>
</html>

In this example, a Google Map is embedded in the iframe to show the location of a business or place.

FAQs About HTML <iframe> Tag

Q1: What is the purpose of the HTML <iframe> tag?
A: The <iframe> tag is used to embed external content like videos, maps, or web pages within the current webpage without redirecting the user.

Q2: Can I control the size of the iframe?
A: Yes, you can control the size of the iframe by setting the width and height attributes in pixels or percentages.

Q3: Does the <iframe> tag support security restrictions?
A: Yes, the sandbox attribute allows developers to impose restrictions such as blocking scripts, forms, and navigation inside the iframe.

Q4: Can an iframe be made fullscreen?
A: Yes, using the allowfullscreen attribute, you can enable the embedded content to be displayed in fullscreen mode, such as with videos.

Q5: How does the loading attribute impact performance?
A: The loading attribute allows you to control when the iframe content is loaded. Using loading ="lazy" delays loading the iframe until it's needed, which improves page load time and performance.

tools

HTML

Related Articles