# iframe, sandbox & Embeds

> An `<iframe>` carves out a completely separate browsing context inside the page. The `sandbox` attribute walls it off from the parent; `title` makes it accessible.

Subject: HTML | Lesson 22 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/iframe-and-embeds
Previous: [video, audio & Captions](https://codewithanimation.in/html/video-audio-and-captions.md) | Next: [Table Structure & Caption](https://codewithanimation.in/html/table-structure-and-caption.md)

## What it is

`<iframe>` embeds another document inside the current page in its own isolated browsing context. That context has its own history stack, its own JavaScript scope, and its own cookie jar. The embedded content and the host page run side by side but cannot read each other's data unless they share the same origin and both opt in.

The `sandbox` attribute restricts what embedded content can do. An empty `sandbox=""` applies the most restrictive set: no scripts, no form submission, no popups, no navigation of the top page, and no same-origin privileges even if the URL matches the host. Each `allow-*` token explicitly restores one capability.

## Why it matters

Without `sandbox`, an embedded third-party page can run JavaScript, redirect the top-level window, submit forms, and read any cookies shared by the origin. A sandboxed iframe limits the blast radius of a compromised or malicious embed. Every third-party widget, every payment form in an iframe, every user-uploaded HTML snippet should run behind a sandbox.

Clickjacking is the reverse problem: someone frames your page inside their site and tricks users into clicking buttons they cannot see. The defense is a server header, not HTML: `X-Frame-Options: DENY` or `Content-Security-Policy: frame-ancestors 'none'` prevents your page from being embedded at all. `sandbox` does not help here, because you control your own page, not the attacker's.

## How it works

The minimum useful iframe: `<iframe src="https://example.com" title="Example site" width="800" height="600" loading="lazy">`. The `title` attribute is required for screen readers to identify the frame. Without it, a screen reader announces only "iframe" with no context. `loading="lazy"` defers the embed until it is near the viewport, the same as images.

Add `sandbox` to restrict the embed, then selectively restore capabilities with tokens: `sandbox="allow-scripts allow-forms"` enables JavaScript and form submission but keeps popups and top-navigation blocked. The dangerous combination to avoid is `allow-scripts allow-same-origin` on a same-origin embed: same-origin content can access the DOM, find the iframe element, and remove the `sandbox` attribute entirely, defeating it.

The `allow` attribute controls browser features via Permissions Policy: `allow="fullscreen"` lets the embed go fullscreen, `allow="autoplay"` allows media autoplay. YouTube's embed code uses a long `allow` list because it needs camera, clipboard, encrypted media, and more. Unlike `sandbox` which starts closed and opens permissions, `allow` starts with the browser defaults and you explicitly grant extras.

## Common mistake: sandbox="allow-scripts allow-same-origin" on a same-origin embed

This combination is effectively no sandbox for same-origin content. An embedded page at the same origin that can run scripts can query the DOM, find its own `<iframe>` element in the parent, and remove or replace the `sandbox` attribute. The escape works because the browser updates the live attribute, and the browsing context re-evaluates permissions. If you need scripts and same-origin in a sandboxed iframe, the content must be on a separate origin.

## Tip: Always add title; always add sandbox for third-party content

Two rules apply to every iframe. First: add a descriptive `title`. Screen reader users hear the title before entering the frame, so it should name the content, not just say "embedded content". Second: add `sandbox` for any third-party or user-supplied content and restore only the tokens you actually need. The extra minutes spent identifying the required tokens are worth it against the risk of a compromised embed.

## Try it: Try it

See the sandbox boundary light up to signal isolation, and explore which tokens are locked vs open by default.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/iframe-and-embeds

## Check yourself

### Question 1

What does sandbox="" (empty) do to an iframe?

- A. Applies the most restrictive set: no scripts, no forms, no popups, no top navigation, no same-origin
- B. Has no effect; you must list specific tokens for sandbox to do anything
- C. Enables the default sandbox with scripts allowed

Correct answer: A

An empty `sandbox` attribute applies all restrictions. Think of it as starting from a completely locked state where each `allow-*` token is a key that opens one door.

- Why not B: An empty sandbox attribute is the most restrictive state. It blocks everything with no tokens needed. Each allow- token you add opens a specific capability.
- Why not C: Scripts are explicitly blocked by the empty sandbox. You must add allow-scripts to enable them.

### Question 2

Why is sandbox="allow-scripts allow-same-origin" dangerous for a same-origin embed?

- A. Same-origin scripts can access the DOM and remove the sandbox attribute from the iframe
- B. Scripts and same-origin together bypass the Content Security Policy
- C. Browsers automatically disable sandbox when both tokens are present

Correct answer: A

A same-origin page has DOM access to the parent. With `allow-scripts`, it can find the `<iframe>` element and remove the `sandbox` attribute, escaping the restriction entirely. Put same-origin sandboxed content on a separate origin to close this gap.

- Why not B: The issue is not CSP. The embedded page can use JavaScript to find its own iframe element in the parent DOM and remove the sandbox attribute, restoring all capabilities.
- Why not C: Browsers do not disable the sandbox automatically. The embedded page's own scripts do it by modifying the DOM. The vulnerability is logic-level, not browser-level.

### Question 3

What does a screen reader announce for an <iframe> with no title attribute?

- A. Just "iframe", with no context about the content
- B. The embedded page's <title> element
- C. Nothing; iframes are ignored by screen readers

Correct answer: A

Screen readers announce the `title` attribute as the accessible name of the iframe. Without it, users hear only "iframe" and have no way to know what is embedded. The `title` should describe the content, not just say "embedded content".

- Why not B: The screen reader announces the iframe element itself, not the content inside. Without a title attribute on the iframe, the announcement is just the element type.
- Why not C: Screen readers do announce iframes and allow users to enter them. Without a title, the announcement is "iframe" with no label, which gives users no context.

## Remember this

- `<iframe title="...">` is required: screen readers announce the title before the user enters the frame.
- `sandbox=""` applies all restrictions; each `allow-*` token restores one specific capability.
- Never combine `allow-scripts` and `allow-same-origin` on a same-origin embed: the content can remove its own sandbox.
- `X-Frame-Options` or `Content-Security-Policy: frame-ancestors` prevents your page from being framed, not sandbox.
