HTML <iframe>: Embed Another Page Inside a Page

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>
Always give an iframe a meaningful title. It helps users of assistive technologies understand what the embedded content contains.

Modern iframe Attributes Top ↑

AttributePurpose
srcURL of the document or resource to embed.
titleAccessible description of the iframe content.
nameNames the browsing context, for example as a target for a link or form.
widthSets the iframe width. CSS can also control responsive sizing.
heightSets the iframe height. CSS can also control sizing.
loadingCan request lazy loading for off-screen iframe content.
allowControls selected browser features available to the embedded content.
allowfullscreenAllows the embedded content to enter fullscreen where supported.
referrerpolicyControls referrer information sent when loading the iframe.
sandboxApplies restrictions to the embedded document, with optional permissions.
hiddenGlobal HTML attribute that hides the iframe from normal rendering.

Obsolete iframe Attributes Top ↑

The earlier version of this tutorial used several presentation attributes that should not be used in modern HTML.

Old attributeModern approach
frameborderUse CSS such as border:0;.
scrollingDo not rely on this obsolete attribute. Let the embedded document/browser manage scrolling.
vspaceUse CSS margin.
hspaceUse CSS margin.
marginwidthDo not use this obsolete iframe attribute; control layout with CSS and the embedded document.
marginheightDo not use this obsolete iframe attribute; control layout with CSS and the embedded document.
Do not copy old iframe examples containing frameborder, scrolling, hspace, vspace, marginwidth or marginheight. Use modern HTML attributes and CSS instead.

Responsive iframe Top ↑

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.

Embed a YouTube Video Top ↑

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>

Embed Google Maps Top ↑

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>

Lazy Loading Top ↑

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.

sandbox for Embedded Content Top ↑

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>
Do not add sandbox permissions automatically. Every extra token relaxes a restriction, so allow only capabilities the embedded content actually needs.

allow and Permissions Top ↑

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 Top ↑

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.

Hidden iframe Top ↑

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.

Hiding an iframe is not a security feature and should not be used to conceal sensitive application logic or data.

Security and Cross-Origin Restrictions Top ↑

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.

  • Embed only content you intend to display.
  • Use sandbox when you need to restrict embedded content.
  • Grant iframe permissions through allow only when required.
  • Use HTTPS for both the parent page and embedded resources when possible.
  • Do not assume an iframe can embed every website. A remote site can send security policies that prevent framing.
  • Do not trust data merely because it came from an embedded page.

Common iframe Mistakes Top ↑

Using frameborder Top ↑

frameborder is obsolete. Use CSS, for example border:0;.

Using scrolling, hspace or vspace Top ↑

These old iframe presentation attributes should not be used in modern HTML. Use CSS and normal browser scrolling behavior.

Omitting the title Top ↑

Give the iframe a useful title describing its embedded content.

Using only fixed dimensions Top ↑

Fixed widths can overflow mobile screens. Use responsive CSS where appropriate.

Loading every below-the-fold iframe immediately Top ↑

Consider loading="lazy" for iframe content that is not initially visible.

Giving an iframe unnecessary permissions Top ↑

Use the smallest set of allow or sandbox permissions that meets the embed's requirements.

Assuming every site can be embedded Top ↑

Remote sites can prevent framing through browser-enforced security policies.

The older <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.

Frequently Asked Questions Top ↑

Q1: What is an iframe in HTML?

An iframe embeds another document or resource inside the current HTML page in a separate browsing context.

Q2: Is frameborder still valid in HTML?

Do not use frameborder in modern HTML. Control iframe borders with CSS.

Q3: Why should an iframe have a title?

The title helps users of assistive technologies understand the purpose of the embedded content.

Q4: How do I make an iframe responsive?

Use CSS such as width:100% and an appropriate height or aspect ratio instead of relying only on a fixed width.

Q5: What does loading="lazy" do on an iframe?

It allows the browser to defer loading an off-screen iframe until it is closer to being needed.

Q6: What does sandbox do?

Sandbox applies restrictions to the embedded document. Optional tokens can selectively restore capabilities the content needs.

Q7: Can an iframe embed any website?

No. A remote site can send browser-enforced security policies that prevent the site from being displayed inside a frame.


HTML Advantages of frames FRAMES



plus2net.com







09-03-2023

is used to write code for floating frame



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer