The HTML <iframe> element embeds another HTML page or external resource inside the current page. Common uses include videos, maps, documents and selected web applications.
<iframe
src="targetpg.html"
title="Example embedded page"
width="600"
height="400"
loading="lazy">
</iframe>
title. It helps users of assistive technologies understand what the embedded content contains.| Attribute | Purpose |
|---|---|
src | URL of the document or resource to embed. |
title | Accessible description of the iframe content. |
name | Names the browsing context, for example as a target for a link or form. |
width | Sets the iframe width. CSS can also control responsive sizing. |
height | Sets the iframe height. CSS can also control sizing. |
loading | Can request lazy loading for off-screen iframe content. |
allow | Controls selected browser features available to the embedded content. |
allowfullscreen | Allows the embedded content to enter fullscreen where supported. |
referrerpolicy | Controls referrer information sent when loading the iframe. |
sandbox | Applies restrictions to the embedded document, with optional permissions. |
hidden | Global HTML attribute that hides the iframe from normal rendering. |
The earlier version of this tutorial used several presentation attributes that should not be used in modern HTML.
| Old attribute | Modern approach |
|---|---|
frameborder | Use CSS such as border:0;. |
scrolling | Do not rely on this obsolete attribute. Let the embedded document/browser manage scrolling. |
vspace | Use CSS margin. |
hspace | Use CSS margin. |
marginwidth | Do not use this obsolete iframe attribute; control layout with CSS and the embedded document. |
marginheight | Do not use this obsolete iframe attribute; control layout with CSS and the embedded document. |
frameborder, scrolling, hspace, vspace, marginwidth or marginheight. Use modern HTML attributes and CSS instead.A fixed-width iframe can overflow a narrow screen. One simple responsive pattern is to let the iframe use the available width and give it an aspect ratio.
<iframe
src="targetpg.html"
title="Example embedded page"
style="width:100%; aspect-ratio:16/9; border:0;"
loading="lazy">
</iframe>
For reusable layouts, move the style rules into a stylesheet instead of repeating inline styles.
The original Plus2net page uses this YouTube video URL. Here is the same embed modernized by removing the obsolete frameborder attribute and adding responsive sizing.
<iframe
src="https://www.youtube.com/embed/e4ZJwowtIFo?si=3dYRClPanO_veIz9"
title="YouTube video player"
style="width:100%; aspect-ratio:16/9; border:0;"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowfullscreen
loading="lazy">
</iframe>
Google Maps can provide iframe embed code for a location. The original Plus2net example shows Mumbai.
<iframe
src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d241316.67337813933!2d72.71603275896192!3d19.082501695895846!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x3be7c6306644edc1%3A0x5da4ed8f8d648c69!2sMumbai%2C%20Maharashtra!5e0!3m2!1sen!2sin!4v1694417704855!5m2!1sen!2sin"
title="Map showing Mumbai, Maharashtra"
style="width:100%; height:300px; border:0;"
allowfullscreen
loading="lazy"
referrerpolicy="no-referrer-when-downgrade">
</iframe>
For an iframe that begins below the visible portion of the page, loading="lazy" can delay loading until the browser determines that the iframe is near the viewport.
<iframe
src="report.html"
title="Embedded report"
loading="lazy">
</iframe>
This can reduce unnecessary initial page work when embedded content is not immediately visible.
The sandbox attribute applies restrictions to embedded content. With no tokens, it enables a broad set of restrictions:
<iframe
src="external-content.html"
title="Restricted embedded content"
sandbox>
</iframe>
Specific capabilities can be added only when required:
<iframe
src="trusted-app.html"
title="Embedded application"
sandbox="allow-forms allow-scripts">
</iframe>
The allow attribute can control selected browser features available inside an iframe.
<iframe
src="video.html"
title="Embedded video"
allow="fullscreen">
</iframe>
Video providers often supply a longer allow value for features their player can use. Keep only the permissions needed by the embed.
referrerpolicy controls how much referrer information is sent when the iframe resource is requested.
<iframe
src="https://example.com/widget"
title="Example widget"
referrerpolicy="no-referrer">
</iframe>
Use a policy that fits the embedded service and your application's privacy requirements.
The original tutorial demonstrates the global hidden attribute:
<iframe
name="myiframe"
src="uploadck.php"
width="300"
height="300"
hidden>
</iframe>
The iframe is not rendered normally while hidden is present.
An iframe creates a separate browsing context. When an iframe loads content from another origin, browser security rules normally prevent the parent page from directly reading or modifying the embedded document's DOM.
sandbox when you need to restrict embedded content.allow only when required.frameborder is obsolete. Use CSS, for example border:0;.
These old iframe presentation attributes should not be used in modern HTML. Use CSS and normal browser scrolling behavior.
Give the iframe a useful title describing its embedded content.
Fixed widths can overflow mobile screens. Use responsive CSS where appropriate.
Consider loading="lazy" for iframe content that is not initially visible.
Use the smallest set of allow or sandbox permissions that meets the embed's requirements.
Remote sites can prevent framing through browser-enforced security policies.
<frameset> and <frame> approach belongs to legacy HTML. Modern pages use normal document structure; <iframe> remains available for embedding a separate browsing context where appropriate.An iframe embeds another document or resource inside the current HTML page in a separate browsing context.
Do not use frameborder in modern HTML. Control iframe borders with CSS.
The title helps users of assistive technologies understand the purpose of the embedded content.
Use CSS such as width:100% and an appropriate height or aspect ratio instead of relying only on a fixed width.
It allows the browser to defer loading an off-screen iframe until it is closer to being needed.
Sandbox applies restrictions to the embedded document. Optional tokens can selectively restore capabilities the content needs.
No. A remote site can send browser-enforced security policies that prevent the site from being displayed inside a frame.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
09-03-2023 | |
| is used to write code for floating frame | |