An HTML <iframe>
(short for inline frame) is used to embed another HTML document or web page within the current document. It allows you to display content from a different source or URL within a specific area of your webpage.
The <iframe>
element is self-contained and behaves as a separate window within the current page. Here’s an example of how to use the <iframe>
element:
<iframe src="https://www.example.com" width="500" height="300"></iframe>
In this example, the <iframe>
element is used to embed the content from the URL “https://www.example.com” within the current page. The src
attribute specifies the source URL of the content you want to display. The width
and height
attributes define the dimensions of the iframe in pixels.
You can also use other attributes with the <iframe>
element:
sandbox
: Specifies a sandbox environment for the embedded content, restricting certain features to enhance security.frameborder
: Indicates whether to display a border around the iframe. Set it to “0” to remove the border.allowfullscreen
: Allows the iframe content to be displayed in fullscreen mode.scrolling
: Specifies whether to display scrollbars within the iframe. Set it to “yes”, “no”, or “auto” to control scrolling behavior.
Here’s an example that includes some of these attributes:
<iframe src="https://www.example.com" width="500" height="300" sandbox frameborder="0" allowfullscreen scrolling="auto"></iframe>
It’s worth noting that embedding content from external sources using <iframe>
may have security implications. Make sure to use trusted sources and consider the potential risks associated with displaying content from external websites.
Additionally, the <iframe>
element is not recommended for responsive web design as it may require additional adjustments to ensure proper display on different devices and screen sizes.
Overall, the <iframe>
element is useful when you want to include content from another source within your webpage while keeping it isolated from the rest of the page’s content.