HTML Iframes
Last Updated :
24 Sep, 2024
HTML iframes are used to embed external content such as videos, maps, or entire web pages into their web projects. This article will cover the syntax, attributes, and use cases of iframes, providing a solid foundation for leveraging them in web development.
What are HTML Iframes?
An iframe (short for inline frame) is an HTML element used to embed another HTML document inside the current document. The <iframe> tag specifies the URL of the external content, enabling seamless integration of videos, maps, forms, or entire web pages into your site.
Syntax:
<iframe src="URL" title="description"></iframe>
- The
src attribute specifies the URL of the document you want to embed. - Iframes can include videos, maps, or entire web pages from other sources.
URL Types for <iframe>
It contains a single value URL that specifies the URL of the document that is embedded in the iframe. There are two types of URL links which are listed below:
URL
| Descriptions
|
|---|
Absolute URL
| It points to another webpage.
|
Relative URL
| It points to other files of the same web page.
|
Supported Attributes of the <iframe> Tag
| Attributes | Description |
|---|
| 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>. |
| height | Sets the height of the <iframe> element. |
| width | Sets the width of the <iframe> element. |
| loading | Specifies how the content of the <iframe> should be loaded. |
| scrolling | Controls whether or not the <iframe> should have scrollbars. |
| name | Specifies the name of the <iframe> for targeting its content or for referencing it in JavaScript. |
| referrerpolicy | Sets the referrer policy for the <iframe> content. |
| sandbox | Specifies an extra set of restrictions for the content in the <iframe>. |
| src | Specifies the URL of the document to embed in the <iframe>. |
| srcdoc | Specifies the HTML content of the page to display in the <iframe>. |
HTML Iframes Examples
Example 1: Basic Iframe Embedding
In this example, an iframe is used to display another webpage within the current webpage.
HTML
<!DOCTYPE html>
<html>
<head>
<title>HTML iframe Tag</title>
</head>
<body style="text-align: center">
<h2>HTML iframe Tag</h2>
<iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
height="370"
width="400">
</iframe>
</body>
</html>
Output:

HTML Iframes Example Output
2. Using Height and Width attribute
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%”).
Example 2: Setting Width and Height
The width and height attributes allow you to control the iframe’s dimensions. The default unit is pixels, but percentages can also be used to create responsive iframes.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>
Content goes here
</p>
<iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
height="395"
width="400">
</iframe>
</body>
</html>
Output:

HTML Iframes Example Output
3. Removing Borders from Iframe
By default, iframe has a border around it. To remove the border, we must use the style attribute and use the CSS border property.
Example 3: Removing Iframe Borders
By default, iframes have borders. You can remove or style these borders using CSS.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>Content goes here</p>
<iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231227155729/jsonPrac3.html"
height="300"
width="400"
style="border: none">
</iframe>
</body>
</html>
Output:

HTML Iframes Example Output
4. Iframe Border Style
Changing the size, style, and color of the Iframe’s border.
Example 4: Customizing Iframe Border Style
You can change the size, style, and color of the iframe border using CSS.
HTML
<!DOCTYPE html>
<html>
<body>
<p>Content goes here</p>
<iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
height="400"
width="400"
style="border: 4px solid orange">
</iframe>
</body>
</html>
Output:

HTML Iframes border style Example Output
5. Iframe Target in Link
An iframe can be used as the target frame for a link. The target attribute of the link must refer to the name attribute of the iframe.
Example 5: Using an Iframe as a Target for a Link
You can target an iframe with links by using the name attribute of the iframe and the target attribute of the link.
HTML
<!DOCTYPE html>
<html>
<body>
<h2>HTML iframe Tag</h2>
<p>
Click the link text
</p>
<iframe src=
"https://media.geeksforgeeks.org/wp-content/uploads/20210910170539/gfg-221x300.png"
height="400"
width="350"
name="iframe_a">
</iframe>
<p>
<a href=
"https://media.geeksforgeeks.org/wp-content/uploads/20240206111438/uni2.html"
target="iframe_a">
Converter
</a>
</p>
</body>
</html>
Output:

HTML iframe Tag by using the target frame for a link.
Supported Browsers:
In Conclusion, HTML iframes serve as a versatile tool in web development, enabling the integration of external content directly into your webpage. With a clear understanding of the <iframe> tag and its attributes, you can leverage iframes to enhance the functionality and user experience of your web projects.
HTML Iframes – FAQs
What is an iframe?
An <iframe> embeds another HTML document within the current page, allowing you to display external content like videos or webpages.
How do you create an iframe?
Use <iframe src=”URL”></iframe> to embed content from the specified URL. Optionally, add attributes for width, height, and other settings.
What attributes can you use with iframes?
Common attributes include src (URL), width, height, title, frameborder, allowfullscreen, and sandbox.
How do you set the size of an iframe?
Use the width and height attributes to specify the iframe’s dimensions, e.g., <iframe width=”600″ height=”400″></iframe>.
What is the sandbox attribute?
The sandbox attribute restricts iframe content with security features like disabling scripts or forms. Use values like “allow-scripts” to lift specific restrictions.
Can you control iframe content with JavaScript?
Yes, you can interact with iframe content via JavaScript using the contentWindow and contentDocument properties if same-origin policies allow.