HTML: <iframe> tag
In this series of learning HTML, we will teach you how to use HTML iframe tag with proper syntax and lots of examples. Let’s start on the HTML iframe tag.
Description of HTML iframe tag
The HTML “iframe” tag stands for “inline frame” and is used to embed a separate HTML document or web page within another HTML document. The “iframe” tag allows you to display content from another source while still keeping the current web page intact.
Here is an example of how to use the “iframe” tag in HTML:
<iframe src="http://www.example.com"></iframe>
In the example above, the “src” attribute specifies the URL of the external web page that should be displayed within the “iframe” tag. The external web page will be displayed as a separate frame within the current web page.
The “iframe” tag can also include other attributes to customize the appearance and behavior of the embedded web page, such as width, height, scrolling, border, and more.
It’s important to note that the use of “iframe” tags can have security implications, as the content displayed within the iframe may be from an external and potentially untrusted source. Therefore, it is important to exercise caution when using “iframe” tags and ensure that any content being displayed within an iframe is trusted and secure.
Syntax of HTML iframe tag
The <iframe>
tag in HTML is used to embed another HTML document within the current document. Here’s the basic syntax of the <iframe>
tag:
<iframe src="URL" frameborder="0" width="width" height="height"></iframe>
The src
attribute is used to specify the source URL of the document to be embedded. The frameborder
attribute is used to specify whether or not the iframe should have a border around it, with a value of “0” for no border or “1” for a border. The width
and height
attributes are used to specify the dimensions of the iframe in pixels.
Here’s an example of using the <iframe>
tag to embed another webpage:
<iframe src="https://www.example.com" frameborder="0" width="800" height="600"></iframe>
Note that the content within the iframe can be styled using CSS and can be accessed and manipulated using JavaScript. However, loading external content into an iframe can also pose security risks, such as cross-site scripting attacks, so it’s important to be cautious and ensure that the embedded content is trustworthy.
Global Attributes
The iframe
tag in HTML is used to embed an HTML document within another HTML document. It has several global attributes that can be used to control its behavior and appearance, including:
src
: This attribute specifies the URL of the document to be embedded within theiframe
.height
: This attribute sets the height of theiframe
. It can be specified in pixels, percentages, or other units.width
: This attribute sets the width of theiframe
. It can be specified in pixels, percentages, or other units.name
: This attribute specifies a name for theiframe
. It can be used to target theiframe
from other elements or scripts.sandbox
: This attribute is used to enable a sandbox mode for theiframe
. This mode restricts the actions that can be performed within theiframe
to prevent malicious code from executing.scrolling
: This attribute controls whether scrollbars are displayed within theiframe
. It can be set toyes
,no
, orauto
.allowfullscreen
: This attribute enables theiframe
to be displayed in fullscreen mode.frameborder
: This attribute controls whether a border is displayed around theiframe
. It can be set to0
to remove the border or1
to display it.longdesc
: This attribute provides a long description of the content within theiframe
for users with disabilities who may not be able to view it.title
: This attribute provides a title for theiframe
, which can be used by screen readers and other assistive technologies to describe its purpose.
These global attributes can be used to customize the appearance and behavior of the iframe
tag in HTML. By using them effectively, you can create embedded documents that are secure, accessible, and user-friendly.
Event Attributes
Event attributes on HTML elements allow you to add JavaScript code that is executed when a specific event occurs, such as when a user clicks a button or hovers over an element. The iframe
tag in HTML also supports several event attributes, including:
onload
: This event occurs when theiframe
has finished loading its contents. You can use this event to perform actions after theiframe
has loaded, such as modifying its contents or resizing it.onunload
: This event occurs when theiframe
is about to be unloaded, such as when the user navigates away from the page. You can use this event to perform actions before theiframe
is unloaded, such as saving any data entered into theiframe
.onblur
: This event occurs when theiframe
loses focus, such as when the user clicks away from it. You can use this event to perform actions when theiframe
is no longer the active element.onfocus
: This event occurs when theiframe
gains focus, such as when the user clicks on it. You can use this event to perform actions when theiframe
becomes the active element.onresize
: This event occurs when the size of theiframe
changes, such as when the user resizes the browser window. You can use this event to perform actions when the size of theiframe
changes, such as adjusting the layout of its contents.onscroll
: This event occurs when the user scrolls within theiframe
. You can use this event to perform actions when the user scrolls, such as updating the contents of theiframe
.
These event attributes can be used to enhance the functionality and interactivity of the iframe
tag in HTML. By adding JavaScript code to these attributes, you can create dynamic and responsive embedded documents that respond to user input and adapt to changes in their environment.
Other Attributes
In addition to global and event attributes, the iframe
tag in HTML also supports several attributes that are specific to the iframe
element. These attributes include:
allow
: This attribute specifies a list of features that are allowed to be used within theiframe
, such as fullscreen mode or autoplay for videos.allowpaymentrequest
: This attribute specifies whether theiframe
can initiate a payment request through the Payment Request API.referrerpolicy
: This attribute specifies how the browser should send the Referer header when fetching the content for theiframe
.srcdoc
: This attribute allows you to specify the content of theiframe
directly within the HTML document, rather than linking to a separate file.seamless
: This attribute specifies whether theiframe
should appear as part of the parent document or as a separate document with its own styles and layout.loading
: This attribute specifies when theiframe
should start loading its content, such as when the page loads or when the user scrolls to it.importance
: This attribute specifies the relative importance of theiframe
‘s content for preloading and resource prioritization.aria-*
: These attributes are used to add accessibility information to theiframe
for users with disabilities. Examples includearia-label
,aria-describedby
, andaria-labelledby
.
By using these attributes effectively, you can create more powerful and flexible iframe
elements in your HTML documents. For example, you can control which features are allowed within the iframe
, specify how the browser should handle Referer headers, and provide more detailed accessibility information for users with disabilities.
Notes on HTML iframe tag
The iframe
tag in HTML is a powerful tool for embedding external content within a web page. However, there are several important notes to keep in mind when using the iframe
tag:
- Security risks: Embedding content from external sources can pose security risks to your website, as it allows for potential cross-site scripting (XSS) attacks. It is important to use the
sandbox
attribute to restrict the behavior of theiframe
and ensure that it cannot access or modify the parent page. - Accessibility considerations: The
iframe
tag can create accessibility challenges for users with disabilities, such as screen reader users. It is important to use appropriatearia-*
attributes to provide alternative text and descriptions for theiframe
content. - Performance implications: Embedding external content within an
iframe
can have performance implications, as it requires additional network requests to retrieve the content. It is important to optimize theiframe
‘s loading behavior and use lazy loading techniques to improve page load times. - Browser compatibility: The
iframe
tag is supported by most modern web browsers, but there may be compatibility issues with older browsers or mobile devices. It is important to test youriframe
content across different browsers and devices to ensure that it displays correctly. - Content ownership: It is important to ensure that you have the necessary permissions and ownership of the content that you are embedding within an
iframe
. Unauthorized use of copyrighted material can lead to legal issues and damage to your website’s reputation.
By keeping these important notes in mind, you can effectively and responsibly use the iframe
tag to enhance your website’s functionality and user experience.
Browser Compatibility
The HTML iframe tag has essential support with the following browsers:
- Chrome
- Internet Explorer (IE)
- Opera
- Safari (WebKit)
- Firefox (Gecko)
- Android
- Firefox Mobile (Gecko)
- Edge Mobile
- Opera Mobile
- Safari Mobile
Examples of HTML iframe tag
We will discuss the <iframe> tag below, exploring examples of how to use the <iframe> tag in HTML5, HTML 4.01 Transitional, and XHTML 1.0 Transitional.
XHTML 1.0 Transitional Document
Here’s an example of how to use the iframe
tag in XHTML 1.0 Transitional to embed a webpage:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>My Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Welcome to My Website</h1>
<p>Check out this cool website:</p>
<iframe width="100%" height="500" src="https://www.example.com" frameborder="0" scrolling="auto"></iframe>
</body>
</html>
In this example, we have an XHTML 1.0 Transitional document with a title
element and a body
element containing a heading (h1
) and a paragraph (p
). We use the iframe
tag to embed an external website, with the following attributes:
width
andheight
: These attributes set the width and height of theiframe
element in pixels. In this example, we usewidth="100%"
to make theiframe
fill the entire width of the webpage, andheight="500"
to set the height to 500 pixels.src
: This attribute specifies the URL of the website that we want to embed within theiframe
.frameborder
: This attribute specifies whether or not to show a border around theiframe
. In this case, we set it to0
to remove the border.scrolling
: This attribute specifies whether or not to show scrollbars within theiframe
. In this case, we set it toauto
to allow the browser to decide whether or not to show scrollbars based on the size of theiframe
content.
When you load this XHTML document in a web browser, you should see the external website embedded in the iframe
element. You can use the scrollbars to navigate the content within the iframe
. Note that because this example is using XHTML 1.0 Transitional, it does not support the sandbox
attribute or other advanced features of the iframe
tag available in HTML5.
XHTML 1.0 Strict Document
Here’s an example of how to use the html iframe tag in an XHTML 1.0 Strict Document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Example of HTML iframe Tag in XHTML 1.0 Strict Document</title>
</head>
<body>
<h1>Example of HTML iframe Tag in XHTML 1.0 Strict Document</h1>
<p>Below is an example of using the iframe tag to embed a web page within another web page:</p>
<iframe src="https://www.example.com/" width="100%" height="500px">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
In this example, we have an XHTML 1.0 Strict Document with a head section containing a title element and a body section containing an h1 element and a p element with some text explaining the example.
The iframe element is used to embed another web page within the current web page. In this case, we set the src attribute to “https://www.example.com/” to load the example website. We also set the width attribute to 100% to make the iframe fill the entire width of the parent element, and the height attribute to 500px to set the height of the iframe to 500 pixels.
Lastly, we include some fallback content inside the iframe element, which will be displayed if the user’s browser does not support iframes. In this case, we just include a simple p element with some text.
Overall, this example demonstrates how to use the html iframe tag in an XHTML 1.0 Strict Document to embed external web content within a web page.
XHTML 1.1 Document
Here’s an example of how to use the html iframe tag in an XHTML 1.1 Document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
<title>Example of HTML iframe Tag in XHTML 1.1 Document</title>
</head>
<body>
<h1>Example of HTML iframe Tag in XHTML 1.1 Document</h1>
<p>Below is an example of using the iframe tag to embed a web page within another web page:</p>
<iframe src="https://www.example.com/" width="100%" height="500px">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>
In this example, we have an XHTML 1.1 Document with a head section containing a title element and a body section containing an h1 element and a p element with some text explaining the example.
The iframe element is used to embed another web page within the current web page. In this case, we set the src attribute to “https://www.example.com/” to load the example website. We also set the width attribute to 100% to make the iframe fill the entire width of the parent element, and the height attribute to 500px to set the height of the iframe to 500 pixels.
Note that in an XHTML 1.1 Document, the Content-Type meta tag in the head section should specify “application/xhtml+xml” instead of “text/html” as in previous versions of HTML/XHTML. This tells the browser that the document is an XML document and should be processed accordingly.
Lastly, we include some fallback content inside the iframe element, which will be displayed if the user’s browser does not support iframes. In this case, we just include a simple p element with some text.
Overall, this example demonstrates how to use the html iframe tag in an XHTML 1.1 Document to embed external web content within a web page, and also illustrates the proper Content-Type meta tag for an XHTML 1.1 Document.