HTMLBeginner

HTML iframe In Depth: Embedding, sandbox and Security

Learn the HTML iframe element properly: src, width/height, the required title attribute, sandbox for security, lazy loading, and why some sites block embedding.

All HTML lessons

What you will learn

<iframe> lets you embed an entire other web page inside your own — a YouTube player, a Google Map, a payment form, an embedded tweet. It’s powerful, and that power comes with real security considerations most tutorials skip. In this lesson you will learn <iframe> properly: sizing it, the accessibility attribute almost every example online forgets, sandbox for security, why some sites simply refuse to be embedded, and when not to reach for an iframe at all.

What is an <iframe>?

iframe stands for inline frame. It creates a little window inside your page that loads and displays a completely separate document — its own HTML, its own CSS, its own JavaScript, running independently of your page.

<iframe src="https://example.com" width="600" height="400" title="Example website"></iframe>

Think of it as a picture-in-picture window: whatever is happening inside the iframe (scrolling, clicking, playing a video) stays inside that little rectangle and doesn’t affect the rest of your page, and — just as importantly — your page’s CSS and JavaScript generally cannot reach inside it either. This separation is a deliberate security boundary, not a limitation.

Basic attributes

<iframe
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  width="560"
  height="315"
  title="A YouTube video"
></iframe>
  • src — the URL of the page to embed. Must be a real, complete URL.
  • width / height — the iframe’s size, in plain pixel numbers, exactly like <video> and <img>. width="500px" is wrong; width="500" is correct.
  • title — a short description of what the iframe contains. This one is not optional in practice, even though the browser won’t stop you from leaving it out. Without it, a screen reader announces an embedded frame with no way to tell the visitor what’s inside — “frame” with no label. Always write one: title="YouTube video: HTML tutorial", title="Map showing our office location".

Common uses

Embedding a video (YouTube, Vimeo, etc.)

You met this briefly in the video lesson — a hosted video platform’s page cannot be dropped into <video src="...">, because it’s a whole interactive page, not a raw video file. <iframe> embeds that platform’s own player instead:

<iframe
  width="560"
  height="315"
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  title="Product demo video"
  allowfullscreen
  loading="lazy"
></iframe>

Notice the URL uses YouTube’s special /embed/VIDEO_ID format, not the normal /watch?v=VIDEO_ID link you’d copy from your browser’s address bar — video platforms provide a dedicated embed link (usually via their own “Share → Embed” button) built specifically for this.

Embedding a map

<iframe
  width="600"
  height="400"
  src="https://maps.google.com/maps?q=New+York&output=embed"
  title="Map showing New York"
  loading="lazy"
></iframe>

Embedding a page from your own site

<iframe> is also used for splitting a large page into independently-scrolling sections, though this is far less common today than it used to be, since CSS Flexbox and Grid handle most layout needs better.

allowfullscreen

<iframe src="..." allowfullscreen title="Video player"></iframe>

A boolean attribute that lets the embedded content (like a video player) request fullscreen mode. Without it, the fullscreen button inside an embedded YouTube player, for example, won’t work.

loading="lazy"

<iframe src="..." loading="lazy" title="Embedded map" width="600" height="400"></iframe>

Exactly like on <img> (from the images lesson), loading="lazy" defers loading the iframe’s content until it’s about to scroll into view. Since an iframe loads an entire separate web page — with its own scripts, images and network requests — this can meaningfully speed up your page if you have an embed far down it, like a map in a “Contact Us” section at the bottom of the page.

Why some websites refuse to be embedded

Try this and you’ll likely see a blank box or an error instead of the actual site:

<iframe src="https://www.google.com" width="500" height="500" title="Google"></iframe>

Many websites deliberately block themselves from being embedded in someone else’s iframe, using an HTTP response header (X-Frame-Options or a Content-Security-Policy with frame-ancestors) that the browser respects. This is a security measure, not a bug — it protects against an attack called clickjacking, where a malicious site embeds your bank’s login page inside an invisible iframe, tricks you into clicking what looks like an innocent button, and that click actually lands on the hidden, real login page underneath.

Most sites built specifically to be embedded — YouTube, Google Maps, Twitter/X embeds, payment widgets like Stripe — explicitly allow it, because their whole business depends on it. Most ordinary websites (like a shopping site’s homepage) do not, and there is nothing wrong with your code when they refuse — it’s a deliberate choice made by that site’s owners.

sandbox: restricting what an embedded page can do

Because an iframe can run someone else’s JavaScript inside your page, embedding content you don’t fully trust (user-submitted content, an ad, a third-party widget) is a real risk. The sandbox attribute lets you restrict what the embedded page is allowed to do:

<iframe src="untrusted-content.html" sandbox title="Embedded content"></iframe>

Writing sandbox with no value applies the strictest possible restrictions: no JavaScript, no form submission, no popups, treated as if it were a completely separate, powerless origin. You then add back only what you actually need, space-separated:

<iframe
  src="widget.html"
  sandbox="allow-scripts allow-same-origin"
  title="Embedded widget"
></iframe>
Value Allows
allow-scripts Running JavaScript inside the iframe
allow-same-origin Being treated as being from its own origin (needed for many real embeds to function correctly)
allow-forms Submitting forms
allow-popups Opening new windows/tabs
allow-fullscreen Requesting fullscreen mode

The habit to build: when embedding content you trust completely and control yourself (your own site’s page, a well-known platform like YouTube), sandbox is usually unnecessary. When embedding anything from an unfamiliar or user-controlled source, start with plain sandbox and add back only the specific permissions you’ve confirmed you need.

referrerpolicy and allow

You may see these on embed codes copied from platforms like YouTube:

<iframe
  src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  title="Video"
  referrerpolicy="strict-origin-when-cross-origin"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
></iframe>
  • referrerpolicy controls how much information about your page’s URL is sent to the embedded site when it loads — strict-origin-when-cross-origin is a sensible, privacy-respecting default and is exactly what YouTube’s own embed codes use.
  • allow grants the embedded page permission to use specific browser features (camera, microphone, autoplay, fullscreen, and so on). As a beginner, the safest approach is to copy the exact allow value a trusted platform provides in its own official embed code, rather than writing one from scratch.

A word on frameborder

You may see old code (and some copy-pasted embed snippets) using frameborder="0":

<!-- Old style -->
<iframe src="..." frameborder="0" title="Video"></iframe>

frameborder is a legacy HTML attribute, no longer part of the modern standard. Iframes have no visible border by default in current browsers anyway, so it does nothing in practice today. If you need to add or remove a border, use CSS instead:

iframe {
  border: none;
}

Common mistakes

  • Leaving out title. It costs nothing to add and is the main thing that makes an iframe accessible to screen reader users.
  • Writing width/height with "px", like width="500px". Use plain numbers: width="500".
  • Assuming every site can be embedded. Many block it deliberately (via X-Frame-Options/CSP) as a security measure — this isn’t something you can override from your own page.
  • Embedding untrusted content without sandbox. If you don’t fully control or trust what’s being embedded, restrict it.
  • Copying a YouTube “watch” link into an iframe instead of the “embed” link. Use the platform’s dedicated embed URL format.
  • Forgetting loading="lazy" on iframes far down a long page, needlessly loading extra pages’ worth of content the visitor may never scroll to.
  • Using an iframe for page layout (splitting a page into “frames” the old-fashioned way). Modern CSS (Flexbox, Grid) is the right tool for arranging your own content — reserve <iframe> for embedding genuinely separate documents.

Interview-style questions

Why does an iframe’s title attribute matter? It gives screen reader users a description of what the embedded frame contains, since without it they only hear an unlabelled “frame” with no context.

Why might an <iframe> fail to display a particular website? The target site may deliberately block itself from being embedded, using the X-Frame-Options header or a Content-Security-Policy, as a defence against clickjacking attacks.

What does the sandbox attribute do? It restricts what an embedded page is allowed to do — by default blocking scripts, forms, and popups entirely — and you selectively re-enable only the specific permissions the embedded content actually needs.

Why is frameborder="0" no longer necessary? It’s a legacy attribute that has no effect in modern browsers, since iframes have no border by default; use CSS border: none if you need to explicitly control it.

Practice

  1. Use the Practice in Editor button. Change the title on both iframes to describe their specific content, and add sandbox="allow-scripts allow-same-origin" to the YouTube one to see what breaks (fullscreen or playback controls may stop working, showing why choosing the right sandbox values matters).

  2. Try embedding https://www.google.com in a new iframe and observe what happens. Explain in one sentence why it doesn’t display.

  3. Find a YouTube video, use its “Share → Embed” option to get the real embed code, and compare it with a plain <video> or a guessed <iframe src="...watch?v=..."> — note the differences.

  4. Fix this code (it has two mistakes: a missing title, and width/height written with px):

    <iframe src="https://maps.google.com/maps?q=Delhi&output=embed" width="600px" height="400px"></iframe>

Recap

  • <iframe> embeds a whole separate web page inside your own, in an isolated little window.
  • Always include a descriptive title — it’s the main thing making an iframe accessible.
  • width/height are plain pixel numbers, with no units.
  • Many sites block themselves from being embedded (via X-Frame-Options/CSP) as protection against clickjacking — this is expected, not a bug in your code.
  • Use sandbox to restrict what untrusted embedded content is allowed to do, adding back only the specific permissions you need.
  • loading="lazy" defers loading iframes further down the page; frameborder is legacy — use CSS for borders instead.