HTML sandbox Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The sandbox attribute on <iframe> applies security restrictions to the embedded page. An empty sandbox turns on the full set of restrictions. A space-separated list of allow-* tokens lifts only those specific limits.

Use it when embedding untrusted or third-party content. Pair with a clear title, and often src or srcdoc.

Baseline · Widely available.

Not deprecated. Presence alone enables restrictions; tokens are opt-outs from the lockdown.

Never combine allow-scripts + allow-same-origin on same-origin embeds.

That pair can let the framed page remove sandbox, undoing the protection (MDN).

HTML Syntax

syntax.html
<iframe src="…" sandbox>
<iframe src="…" sandbox=" allow-scripts allow-forms …">

In JavaScript: iframe.sandbox is a DOMTokenList (add/remove tokens).

Attribute Values

Empty = all restrictions. Common tokens that allow a capability:

Value / tokenEffectTry it
sandbox (empty)Maximum restrictions for the frameTry it
allow-scriptsRun scripts (not popups by itself)Try it
allow-formsAllow form submissionTry it
allow-popupsAllow window.open / target="_blank"Try it
allow-same-originTreat as normal origin (cookies/storage APIs may work)Try it
allow-top-navigationNavigate the top-level browsing contextUse sparingly

Also: allow-downloads, allow-modals, allow-pointer-lock, allow-popups-to-escape-sandbox, allow-presentation, allow-orientation-lock, allow-storage-access-by-user-activation, allow-top-navigation-by-user-activation, allow-top-navigation-to-custom-protocols.

Live Preview

Compare an empty sandbox (scripts blocked) with allow-scripts. Both frames use srcdoc for a self-contained demo.

Inside the frame, a script tries to set the status text. Empty sandbox blocks it; allow-scripts lets it run.

iframe

sandbox (empty) — scripts blocked

HTML Examples

Short demos with srcdoc. Use Try It Yourself for a live editor.

Example 1 — Empty sandbox

Lock the frame down. Scripts inside do not run.

HTML
<iframe
  title="Locked demo"
  width="320"
  height="100"
  sandbox
  srcdoc="<p id='s'>Hello</p><script>document.getElementById('s').textContent='Hacked';</script>">
</iframe>
Try It Yourself

How It Works

  1. sandbox with no value enables all restrictions.
  2. srcdoc embeds a tiny HTML document for the demo.
  3. Script cannot change the paragraph.
  4. title names the frame for accessibility.

Example 2 — allow-scripts (and forms)

Lift only what you need. Scripts can run; popups still need allow-popups.

HTML
<iframe
  title="Scripts allowed"
  width="320"
  height="120"
  sandbox="allow-scripts allow-forms"
  srcdoc="<p id='s'>Hello</p><script>document.getElementById('s').textContent='Script ran';</script>">
</iframe>
Try It Yourself

How It Works

  1. Tokens are space-separated allow-lists.
  2. allow-scripts lets the inline script update the text.
  3. allow-forms would permit submit (not used in this tiny doc).
  4. Other limits (popups, top navigation, …) stay on until you allow them.

Example 3 — Dangerous token pair (do not copy for same-origin)

Study the anti-pattern. Prefer not combining these when the embed is same-origin.

HTML
<!-- BAD when iframe is same-origin as the parent -->
<iframe
  title="Unsafe combination"
  src="/some-same-origin-page"
  sandbox="allow-scripts allow-same-origin">
</iframe>

<!-- Safer mindset: keep scripts OR same-origin access limited;
     serve untrusted content from a separate origin -->
Try It Yourself

How It Works

  1. allow-same-origin restores a normal origin for the frame.
  2. allow-scripts lets that frame run script.
  3. Together + same-origin parent → script can strip sandbox.
  4. Fix: avoid the pair, or host untrusted content on another origin.

Edge Cases & Pitfalls

Most sandbox bugs are over-allowing tokens or assuming the attribute alone is enough.

tokens

Empty = strict; tokens = exceptions

Add only the capabilities the embed truly needs.

danger

allow-scripts + allow-same-origin

Strongly discouraged for same-origin embeds — can defeat sandboxing.

popups

New tabs inherit sandbox

Unless you use allow-popups-to-escape-sandbox. Forms in those tabs may silently fail without allow-forms.

pdf

Built-in PDF viewer may be blocked

Sandbox is a poor fit for native PDF preview restrictions.

a11y

Always set title on iframes

Screen readers need a short name for the frame.

Browser Support

sandbox is an HTML attribute on <iframe>. Individual tokens may differ slightly by engine — check MDN for edge tokens.

✓ Baseline · Widely available

sandbox

Supported in modern browsers. Prefer the least privilege set of allow-* tokens.

96% Global support (approx.)
Google Chrome Yes
Full support
Mozilla Firefox Yes
Full support
Apple Safari Yes
Full support
Microsoft Edge Yes
Full support
Opera Yes
Full support
Samsung Internet Yes
Full support
Internet Explorer 10+
Partial / older tokens
sandbox Widely supported

Bottom line: Safe to use for hardening iframes. Review newer tokens before relying on them in older browsers.

Key Takeaways

  • Role: restrict an iframe; empty = strictest.
  • Tokens lift only the capabilities you list.
  • Avoid allow-scripts + allow-same-origin together.
  • Complement with a separate origin + CSP when content is untrusted.

One line: sandbox locks down an iframe; allow-* tokens carefully open holes.

Frequently Asked Questions

On an iframe, sandbox applies a set of security restrictions to the embedded browsing context. An empty sandbox enables all restrictions. Space-separated allow-* tokens lift specific ones.
Maximum lockdown for that frame: no scripts, no forms, no popups, treated as a unique origin, and more — until you add allow-* tokens.
If the framed page is same-origin as the parent, that pair lets the embed remove the sandbox attribute, undoing the protection. MDN strongly discourages the combination.
No. Sandbox restricts the iframe. CSP is a separate, complementary defense for the document. Use both when appropriate.
By default, new tabs/windows inherit the sandbox. allow-popups-to-escape-sandbox lets those new contexts run without the same flags.
No. It is a Baseline widely available security attribute on iframe.

Next: scheme

Learn the obsolete meta scheme attribute (and modern alternatives).

scheme tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful