To display a web page within a web page or a nested web page, the HTML Iframes are used. HTML Iframes are also known as Inline frames. As the name itself suggests, it defines an inline frame.
Syntax:
<iframe src="URL"></iframe>
Parameters:
src: It is an attribute used to specify the URL.
URL: It is used to define the web address of the inline frame page.
Set Height and Width:
To specify the size of the iframe, the height and width attributes are used. The size is defined in pixels by default.
Example: In Pixels:
<!DOCTYPE html> <html> <body> <iframe src="https://www.w3schools.blog" height="300" width="400"></iframe> </body> </html>
Explanation:
In the above example, we set the width and height of iframe in pixels.
Example: In Percentage:
<!DOCTYPE html> <html> <body> <iframe src="https://www.w3schools.blog" height="100%" width="60%"></iframe> </body> </html>
Explanation:
In the above example, we set the width and height of iframe in percentage.
Example: With CSS.
<!DOCTYPE html> <html> <body> <iframe src="https://www.w3schools.blog" style="height:300px; width:400px"></iframe> </body> </html>
Explanation:
In the above example, we set the width and height of the iframe using CSS.
Remove the Iframe Border:
The style attribute and the CSS border property can be used to remove the default border present around an Iframe.
Example:
<!DOCTYPE html> <html> <body> <iframe src="https://www.w3schools.blog" style="height:300px; width:400px; border:none;"></iframe> </body> </html>
Explanation:
In the above example, we removed the default border present around the Iframe.
Modify the Iframe Border:
The style attribute and the CSS border property can be used to modify the size, style, and color of the default border present around an Iframe.
Example:
<!DOCTYPE html> <html> <body> <iframe src="https://www.w3schools.blog" style="height:300px; width:400px; border:4px solid crimson;"></iframe> </body> </html>
Explanation:
In the above example, we modified the default border present around the Iframe.
Target Frame for a Link:
To use an iframe as the target frame for a link, the specified target attribute of the link should refer to the name attribute of the iframe.
Example:
<!DOCTYPE html> <html> <body> <iframe src="example.html" height="300px" width="400" name="iframe_example"></iframe> <p><a href="https://www.w3schools.blog" target="iframe_example" rel="noopener noreferrer">w3schools.com</a></p> </body> </html>
Explanation:
In the above example, the respective link opens in the iframe on clicking, since the specified target of the respective link matches the name of the iframe.
Iframe to Embed YouTube video:
To add a YouTube video on a webpage follow the below steps:
- Open the YouTube video to embed.
- Click on the SHARE video option and then on the Embed option.
- Copy and paste the generated HTML code into the desired HTML file.
- If required, modify the height, width, and other properties of the video.
Example:
<!DOCTYPE html> <html> <body style="color: crimson"> <iframe width="560" height="315" src="https://www.youtube.com/watch?v=0GY0gGWs4J8" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> <h2>Click on the video to watch.</h2> </body> </html>
Iframe attributes:
Below are the various attributes used with an HTML Iframe.
Attribute | Value | Description |
allowfullscreen | TRUE/ FALSE | It is used to open the frame in full screen. |
height | Pixels | It specifies the height of the iframe (default height = 150 px). |
name | text | To provide a name to the iframe. |
frameborder | 1 or 0 | It defines whether an iframe should have a border or not. |
width | Pixels | It is used to specify the width of the embedded frame (default width is 300 px). |
src | URL | For the web address of the inline frame page. |
sandbox | It applies extra restrictions for the content of a frame. | |
sandbox | allow-forms | It allows submission of the form which otherwise is blocked. |
sandbox | allow-popups | This is to enable popups that otherwise will not open. |
sandbox | allow-scripts | It enables the script to run. |
sandbox | allow-same-origin | To treat the embedded resource as downloaded from the same source. |
srcdoc | It shows the HTML content in the inline iframe. | |
scrolling | To define whether to provide a scroll bar for the iframe or not. | |
scrolling | auto | Used to display if the content of the iframe is larger than its dimensions. |
scrolling | yes | It is used to always display the scroll bar for the iframe. |
scrolling | no | Used to never display the scrollbar for the iframe. |