An iframe (inline frame) is an HTML element that allows you to embed another web page, video, or external content within your own page. The <iframe> tag acts like a window to display content from another source, such as a YouTube video or a Google Map. This tutorial explains how to use iframes effectively, with clear examples, to help beginners enhance their websites.
The <iframe> tag creates a rectangular frame within your webpage to display external content. It’s a self-closing tag that can embed entire web pages, media, or widgets. Iframes are widely used for integrating third-party content without hosting it directly on your site.
The basic structure of an iframe looks like this:
<iframe src="https://example.com" width="600" height="400">Your browser does not support iframes.</iframe>
Key components:
Let’s explore common use cases for iframes with examples.
You can embed an entire webpage within your site, such as a blog or documentation page.
<iframe src="https://www.example.com" width="800" height="600">Your browser does not support iframes.</iframe>
This displays the content of example.com inside a 800x600px frame.
Iframes are commonly used to embed YouTube videos. Here’s an example using the provided YouTube embed code:
<iframe width="560" height="315" src="https://www.youtube.com/embed/EkRuAOsmXm0?si=-UXdydJ-gMuTLSR7" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
This embeds a YouTube video with controls for playback, full-screen, and other features.
Preview
You can embed maps using iframes provided by Google Maps.
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3151.835434509374!2d144.9537363153167!3d-37.81627977975146!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x6ad642af0f11fd81%3A0x5045675218ce6e0!2sMelbourne%20VIC%2C%20Australia!5e0!3m2!1sen!2sus!4v1634567890123" width="600" height="450" style="border:0;" allowfullscreen loading="lazy"></iframe>
The <iframe> tag supports several attributes to customize its behavior and appearance:
<iframe src="https://example.com" width="100%" height="500"></iframe>
<iframe src="https://example.com" frameborder="0"></iframe>
<iframe src="https://example.com" allow="autoplay; fullscreen"></iframe>
<iframe src="https://www.youtube.com/embed/EkRuAOsmXm0" allowfullscreen></iframe>
<iframe src="https://example.com" loading="lazy"></iframe>
To make your iframes effective, secure, and user-friendly:
width="100%"
or CSS) for different devices.<iframe src="https://example.com" title="Example Website"></iframe>
Beginners often make these errors:
aspect-ratio
) for a polished look.
Create a simple HTML file and experiment with iframes. Try embedding a YouTube video or a webpage and test different attributes.
<!DOCTYPE html>
<html lang="en">
<body>
<iframe width="560" height="315" src="https://www.youtube.com/embed/EkRuAOsmXm0?si=-UXdydJ-gMuTLSR7" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</body>
</html>
By mastering the <iframe> tag, you can integrate external content like videos, maps, or websites into your pages, making them more dynamic and interactive. Practice regularly to get comfortable!