HTML <iframe> Tag
In this post, we will talk about Tag iframe examples. Aspiring to meet the demands of learning.
The HTML <iframe> tag is used to embed another HTML document within the current document. The content of the <iframe> tag is typically a separate HTML file, although it can also be a PDF file, an image file, or any other type of document that can be displayed in a web browser.
The markup for an inline frame is as follows:
Example: 
Setting and removing iframe borders using CSS:
Example: 
Here is an example of <iframe> to embed youtube video:
Example: 
Tag iframe Usage
The Tag iframe <iframe> refers to an inline frame.
Inline frames allow another document to be embedded within the current HTML document.
Key-Features and Uses
Here are some key features and uses of the <iframe> tag:
- The <iframe> tag is commonly used to embed external content from another website, such as a YouTube video, a Google Map, or a social media post. This allows web developers to display content from other sources within their own website.
- The <iframe> tag can be used to create an inline frame within a web page. This can be useful for displaying content that needs to be scrolled or that needs to be displayed separately from the surrounding content.
- The <iframe> tag allows web developers to specify the dimensions and location of the embedded content. This can be useful for controlling the display area of the embedded content and for integrating it seamlessly with the surrounding content.
- The use of the <iframe> tag can present security risks, such as the potential for cross-site scripting (XSS) attacks. As such, it is important to properly sanitize any user-generated content that is embedded within an <iframe> tag, and to ensure that the embedded content is trusted and secure.
- The <iframe> tag is supported by all major web browsers, and is widely used in web development for embedding external content within web pages.
Attributes
Attribute | Value | Overview |
---|---|---|
height | pixels | An iframe’s height is set here. 150 pixels is the default height. |
loading | eager lazy | Indicates whether an <iframe> should be loaded immediately or if it should be delayed until certain conditions are met. |
allow | A feature policy is defined for the <iframe> | |
allowfullscreen | true false | It is set to true if the <iframe> is capable of activating fullscreen mode with the requestFullscreen() method. |
allowpaymentrequest | true false | Enable cross-origin iframes to access the Payment Request API by setting it to true |
srcdoc | HTML_code | Displays the HTML content of a page within an <iframe>. |
name | text | Provide the name of an <iframe> |
referrerpolicy | no-referrer no-referrer-when-downgrade origin origin-when-cross-origin same-origin strict-origin-when-cross-origin unsafe-url | This property determines which referrer data is to be sent when fetching the iframe. |
src | URL | Indicate the address of the document to embed in the <iframe>. |
width | pixels | <iframe> width is specified here. A width of 300 pixels is the default. |
sandbox | allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-top-navigation | Allows extra restrictions to be applied to content in an <iframe> |
Browser Compatibility
Element | |||||
---|---|---|---|---|---|
<iframe> | Yes | Yes | Yes | Yes | Yes |
<!–
Advertisement
–><!–
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
–>
Attributes
Global
The Tag iframe <iframe> in HTML allows for and accepts Global Attributes.
Event
The Tag iframe <iframe> accepts Event Attributes in HTML.
Predefined CSS
It is common for browsers to render the <iframe> element with default values as follows:
iframe:focus { outline: none; } iframe[seamless] { display: block; }
HTML <iframe> Tag Advantages
The HTML <iframe> tag provides several advantages for web developers. Here are some of the key advantages:
- The <iframe> tag allows web developers to embed external content within a web page, such as videos, maps, or social media posts. This makes it easy to integrate content from other sources within a website, without having to create the content from scratch.
- The <iframe> tag provides a high degree of control over how the embedded content is displayed within the web page. Developers can set the size and position of the content, and can use CSS to style the content to match the rest of the page.
- Because the content is hosted externally, updates to the embedded content can be made independently of the web page itself. For example, if a video embedded in a page is updated, the changes will be automatically reflected in the embedded content, without any changes needed to the web page code.
- Because the embedded content is hosted externally, it can be loaded asynchronously, which can improve page load times and overall site performance.
- The <iframe> tag can be used to facilitate cross-domain communication between different web pages, which can be useful for web applications that require communication between different components.