如何使用
The <iframe> tag, which is also known as inline frame which helps us load external objects inside another document on a web page. <iframe> is like a mini web browser within a web page, which is allowing us to include content from other sources into our web pages. They are commonly used to embed Google Maps in to the contact page of an website or ad sense or to add a Youtube videos to a webpage, which help us to hold the user to our web page.
Syntax
The basic syntax for adding an iframe to a web page can be given with −
<iframe src="URL"></iframe>
The src attribute of <iframe> tag expects a URL that eventually point to the location of external object or the web page we want to embed.
This particular code below will embed an Youtube video of tutorialspoint in the current webpage.
Example
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe)</h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg"></iframe> </body> </html>
As we have seen above that using the “src” attribute we were able to embed the video into the web page. Apart from the global attributes of HTML there are other attribute that help us in create a seamless iframe.
Adjusting width and height of iframe
The default width of an iframe is 300 CSS pixels and height is 150 pixels. In order change these dimension we make use of “width” and “height” attribute. By default, attribute values are set in CSS pixels (Apart from CSS pixels percentages can also be used to set the dimension of an iframe).
Example
To understand this better, let us consider the example below.
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg" height='300' width='600'></iframe> </body> </html>
Example
Following the example, we are using percentages to set the height and width of an iframe.
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg" height='50%' width='70%'></iframe> </body> </html>
Example
CSS styling also be used to set the height and width of an iframe.
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg" style=" height: 300px; width: 600px;"></iframe> </body> </html>
Getting Rid of <iframe> border
Example
An <iframe> come with a default border around it. In order to get rid or remove the border the best practice is to use border property of CSS styling, as shown below −
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg" style=" height: 300px; width: 600px; border:none"></iframe> </body> </html>
Example
In order to understand this better, let’s apply a border of our choice and render the iframe.
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <p>Document content goes here...</p> </div> <iframe src="https://www.youtube.com/embed/y0aCH5pimmg" style=" height: 300px; width: 600px; border:5px solid orange "></iframe> </body> </html>
Note − Earlier “frameborder” attribute was used to set the border of an iframe. This attribute has been deprecated and no longer supported by web browsers.
<iframe> as Link Target
To set a link to open inside an iframe we use the “target” attribute of <a> or <form> or <base> elements.
The link target attribute you specify must refer to the iframe’s name attribute (At any given point both the values of “name” and “target” of both the element should be identical). When a link with a target attribute with that name as value is clicked, the linked resource will open in that iframe.
Example
To have better idea of this, let’s try an example −
<!DOCTYPE html> <html> <head> <title>Creating an in line frame in HTML web page </title> <style> iframe { width: 100%; height: 250px; } </style> </head> <body> <h1>Welcome to Tutorialspoint </h1> <div> <h3>Creating a simple inline frame (iframe) </h3> <a href="https://tutorialspoint.com/market/courses" target="myFrame">Click here to open courses at Tutorialspoint </a> <p>Document content goes here...</p> </div> <iframe src="https://tutorialspoint.com/html/menu.htm" name="myFrame"></iframe> </body> </html>
The example above shows an <iframe> with name attribute set to ‘myWebFrame’ and the <a> target attribute set to ‘myWebFrame’. Once we click the link it opens the web URL in href attribute of <a> tag inside the iframe.
Security concerns of an iframe
Though iframe have been around for a long time and widely used, however, there are several security risks that could open doors for an attacker.
We will list down few security threats that one need to be aware before using an iframe
- iframe injection
- cross-frame scripting
- Clickjacking
- iframe phishing
- Dialog box threats
Browsers that support iframe
Web Browsers | |
Google Chrome | Fully Supported |
Edge | Fully Supported |
Firefox | Supported ( but has bug that prevents iframes from loading if the iframe element was hidden when added to the page.) |
Internet Explorer | Fully Supported |
Opera | Fully Supported |
Safari | Supported ( but has bug that prevents iframes from loading if the iframe element was hidden when added to the page.) |
Mobile Browsers | |
Chrome Andriod | Fully Supported |
Firefox Andriod | Supported (The resize CSS property doesn't have any effect on this element due to bug) |
Opera Andriod | Fully Supported |
Safari iOS | Supported ( but has bug that prevents iframes from loading if the iframe element was hidden when added to the page.) |
Samsung Internet | Fully Supported |
Webview Andriod | Fully Supported |