The iframe element in HTML is used to embed another HTML document within the current document. This allows you to display external content such as webpages, videos, and interactive maps directly within your webpage.
Here is a basic example of an iframe element:
<iframe src="https://www.lets-coding.com" width="600" height="400"></iframe>
The iframe element comes with several attributes that allow you to control its behavior and appearance. Below are some of the most commonly used attributes:
Attribute | Description | Example |
---|---|---|
src | Specifies the URL of the page to embed. |
Try yourself
|
width | Specifies the width of the iframe. |
Try yourself
|
height | Specifies the height of the iframe. |
Try yourself
|
name | Specifies the name of the iframe. |
Try yourself
|
frameborder | Specifies whether or not to display a border around the iframe. |
Try yourself
|
allowfullscreen | Specifies whether the iframe can be displayed in full-screen mode. |
Try yourself
|
Here is how you can apply the iframe element in your HTML document:
Example: IFrame with src
<iframe src="https://www.lets-coding.com"></iframe>
This example shows an iframe element that embeds the content from the specified URL.
<iframe src="https://www.lets-coding.com" width="600" height="400"></iframe>
This example shows an iframe element with specified width and height.
<style>
.iframe-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
}
.iframe-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
<div class="iframe-container">
<iframe src="https://www.lets-coding.com"></iframe>
</div>
This example shows how to make an iframe responsive using CSS.
Here are some important notes and best practices when using the iframe element: