Table of Content
The iframe tag embeds another HTML page inside the current page. It loads content from a separate source.
Understand the <iframe> Tag in HTML
An <iframe> creates a rectangular region inside a page. This region displays another document from a different URL. The user can scroll or interact with it like any page.
You can use the <iframe> tag to load the following external sources:
- Maps
- Ads
- Videos
- Full web pages
Here is the basic tag of <iframe>:
<iframe src="URL" width="value" height="value" title="description"></iframe>src: The URL of the page to show.width: Width in pixels or percent.height: Height in pixels or percent.title: Describes the frame for accessibility.
For example:
<iframe src="https://example.com" width="600" height="400" title="Example Site"></iframe>This loads “example.com” inside the current web page. The frame has a width of 600px and a height of 400px. Screen readers use the title for context.
The <iframe> Tag Attributes in HTML
You can use extra attributes to control security and behavior.
sandboxlimits access. Blocks scripts and form submissions.allowgrants special permissions like camera or fullscreen.referrerpolicycontrols what referrer to send.srcdocreplacessrcloads of HTML code directly.allowfullscreenlet the frame enter full screen.
Here’s a structure with advanced options:
<iframe src="https://example.com" width="500" height="300" title="Advanced Example" sandbox allow="fullscreen; microphone" referrerpolicy="no-referrer" allowfullscreen></iframe>Main Attributes:
srcsets the source page.widthsets the width.heightsets the height.titledescribes the content.nameassigns a name to the frame.loadingchooses lazy or eager loading.
Here are the use cases:
- Use
srcto load a remote or internal page. - Use
widthandheightto fix layout space. - Use
titlefor screen reader users. - Use
nameto target the frame in links or scripts. - Use
loading="lazy"to load after scroll. It improves load time.
Here are two examples that show you these attribute usage:
Basic Frame With Lazy Load:
<iframe src="https://maps.google.com" width="600" height="400" title="Map" loading="lazy"> </iframe>This loads a map, and it delays the load until the user scrolls. It improves speed on large pages.
Frame With Target and Name
<iframe
src="page.html"
name="contentFrame"
width="400"
height="300"
title="Local Page">
</iframe>
<a href="other-page.html" target="contentFrame">Load New Page</a>This creates a link that loads inside the frame. The frame uses the name to catch the link’s target.
Examples
Embed YouTube Video:
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube Video"
allowfullscreen>
</iframe>This shows a YouTube video inside a frame. It fits in a fixed-size box. The allowfullscreen tag lets users open the video full screen.
Use srcdoc Instead of URL:
<iframe
width="400"
height="200"
srcdoc="<h1><iframe> with srcdoc</h1><p>This comes from HTML code.</p>"
title="Inline HTML">
</iframe>This loads custom HTML without a URL. It shows text from the srcdoc. The content is self-contained and does not rely on another page.
Sandbox With Restricted Access:
<iframe
src="https://example.com"
width="500"
height="300"
title="Sandboxed Frame"
sandbox>
</iframe>This loads a page but blocks scripts and plugins. It adds security and keeps the frame isolated.
Frame With Microphone and Fullscreen Access:
<iframe
src="https://example.com/app"
width="800"
height="600"
title="Advanced Permissions"
allow="microphone; fullscreen"
allowfullscreen>
</iframe>This gives the page access to the microphone and full screen. It is useful for apps or video calls.
Wrapping Up
In this article, you learned what an <iframe> is and how to use it. You saw real code and use cases. You learned about both basic and advanced attributes.
Here is a quick recap:
<iframe>loads another page inside a frame.src,width,height, andtitleare essential.sandbox,allow,srcdoc, andloadingcontrol behavior.- Use
nameto target the frame from links. - Choose
srcdocto embed inline HTML. - Use
allowfullscreenfor video and game content.
FAQs
What does <iframe> mean in HTML?
<iframe src="page.html"></iframe>How do I use <iframe> to embed a YouTube video?
<iframe src="https://www.youtube.com/embed/ID" allowfullscreen></iframe>What is the use of the srcdoc attribute in <iframe>?
<iframe srcdoc="<h1>Hello</h1>"></iframe>What is the difference between <frame> and <iframe>?
<iframe src="file.html"></iframe>Similar Reads
The <footer> tag defines a footer section in HTML. It creates a clear endpoint for a page or section. Browsers…
The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…
HTML global attributes work on most elements. They do not depend on the tag name. You can use them with…
HTML link attributes tell the browser how a link works. They set where the link goes, how it opens, and…
The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…
The abbr tag refers to an abbreviation in HTML. It helps you save space and improve accessibility. The screen readers…
The HTML button tag creates interactive controls on a web page. You can use this element to trigger actions or…
The <code> tag in HTML shows short code, command names, or output. Use it to display text that the browser…
The HTML bdi tag marks text that has an unknown or mixed direction. It makes sure that this part does…
The HTML <pre> tag keeps the original format of your text. It shows spaces and line breaks exactly as you…