Explain it simply
Explain it simply
<iframe> cuts a window into your page and shows another web page through it. YouTube videos, Google Maps and payment widgets are most often added this way.
The page behind that window is a separate house with its own HTML, CSS and JavaScript. Because you are letting a stranger into your house, you set the house rules with the sandbox and allow attributes: what the embedded page may and may not do.
Problem → Why → Solution
The problem: a free widget with no house rules
A blog adds a "free weather widget" it found through a search, using the code below. There is no title, no sandbox and no size, so a tiny 300×150 default box appears.
The widget's script can track visitors with its own site's cookies. After a single click inside the widget, it can open popups or send the whole tab to another site. A screen reader announces the box only as an unnamed "frame", so its users cannot tell what it is for.
<!-- No title, no sandbox, no size: the widget gets every default power -->
<iframe src="https://free-weather-widget.example/embed"></iframe>Why this happens
By default an iframe gives the embedded page the same powers as an ordinary website: run scripts, submit forms, open popups. The browser's same-origin policy stops a cross-origin frame from reading your page's DOM, but it does not limit what the frame does on its own. You have to add those limits yourself.
Core concept: a page inside a page
Each iframe is a nested browsing context: a small page with its own document, its own CSS, its own JavaScript and its own origin. The parent's CSS does not reach inside it, and when the origins differ, neither side can read the other's DOM. You control an iframe with four groups of attributes.
1. What it is: src or srcdoc, and title
src loads a URL, while srcdoc lets you write a whole HTML document inside the attribute; if both are present, srcdoc wins. title is the frame's accessible name, so screen readers announce something like "Opening hours, frame".
2. How big, and when it loads
Without width/height the box is 300×150. loading="lazy" delays an iframe below the fold until the user scrolls near it, which speeds up pages full of maps and videos.
3. What it may do: sandbox
An empty sandbox applies every restriction: no scripts, no form submission, no popups, no navigating the parent tab, no alert(), no downloads, and the document is treated as a unique opaque origin, so it cannot use its own cookies or storage. Re-enable only what you need with tokens such as allow-scripts, allow-forms, allow-popups and allow-same-origin.
4. Which device features: allow
allow is the Permissions Policy. Features such as camera, microphone and geolocation are off for cross-origin iframes by default and are delegated only when you write, for example, allow="fullscreen; picture-in-picture". referrerpolicy controls how much of your page URL is sent with the iframe request.
Be careful with allow-scripts + allow-same-origin
If the embedded document has the same origin as your page, giving both tokens lets its script reach the iframe element in the parent, delete the sandbox attribute and reload itself, which is the same as having no sandbox. For cross-origin widgets the pair is common and acceptable.
- src/srcdoc: which page to show
- title: name the window
- sandbox: what it may do
- allow: which device features it gets
Start with every restriction and add tokens one at a time only when the embed breaks; taking powers away later is much harder.
General pattern
<iframe
src="https://trusted.example/embed/ID"
title="What is inside the frame"
width="W" height="H"
loading="lazy"
sandbox="only-the-tokens-you-need"
allow="feature-1; feature-2"
referrerpolicy="strict-origin-when-cross-origin">
</iframe>src should come from a provider you trust. title and size serve the user, loading serves speed, and sandbox, allow and referrerpolicy serve security and privacy.
A sandboxed iframe you can try offline
This example uses srcdoc so it works without internet access, and it adds an empty sandbox: the framed document needs no scripts, so every restriction can stay on. Whenever you embed a page, start from an empty sandbox and add back only the permissions it really needs. For a real provider embed you would use their src URL instead of srcdoc.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Store page with an embedded widget</title>
</head>
<body>
<h1>Yangon Book Corner</h1>
<iframe
title="Opening hours"
width="360" height="150"
sandbox
srcdoc='<h2 style="font-family: sans-serif">Open today</h2>
<p style="font-family: sans-serif">9:00 to 18:00</p>'>
</iframe>
<p>The box above is a separate document inside this page.</p>
</body>
</html>Code breakdown
- Accessible name
title="Opening hours"A screen reader announces "Opening hours" before entering the frame, so the user can decide whether to go in or skip it.
- Explicit size
width="360" height="150"The box size is fixed up front so the layout does not jump. An iframe does not grow to fit its content, so you size it yourself to fit.
- Every restriction on
sandboxA sandbox without tokens blocks scripts, forms, popups and top-level navigation. Add a <script> inside and the Console reports "Blocked script execution in 'about:srcdoc'".
- A document in an attribute
srcdoc='<h2 style="font-family: sans-serif">Open today</h2>The HTML in srcdoc is a separate document and does not inherit the parent's CSS, which is why the style is written inside it. The attribute is wrapped in single quotes, so double quotes can be used freely inside.
Under the "Yangon Book Corner" heading, a bordered 360×150 box shows an "Open today" heading and "9:00 to 18:00" in a sans-serif font, followed by the line "The box above is a separate document inside this page." There are no console errors.Real provider embeds and postMessage
Embed code from video providers often carries a long allow list. Keep only what your page really needs, such as fullscreen and picture-in-picture, and delete the rest. A video in a cross-origin iframe can only autoplay with sound if the parent also delegates allow="autoplay", although letting the user press play is still the better choice.
<iframe src="https://video.example/embed/abc123"
title="Product tour video (3 min)"
width="560" height="315" loading="lazy"
allow="fullscreen; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"></iframe>When the parent and the iframe have different origins they cannot read each other's variables, but they can exchange messages with window.postMessage(). The receiving side must always check event.origin and ignore anything from an unexpected origin; otherwise any site could send commands to your page.
<script>
window.addEventListener("message", (event) => {
if (event.origin !== "https://video.example") return; // ignore strangers
console.log("Player says:", event.data);
});
</script>In real projects
In real projects
Most business websites contain two or three iframes: the map on the Contact page, the video on a product page and the card fields at checkout. Payment providers put the card number field in an iframe on their own domain, so the merchant's JavaScript can never read the card number; here the iframe's isolation is the feature.
Teams review every new iframe with a checklist: do we trust the provider, is there a title, are sandbox and allow as narrow as possible, is it lazy if it sits below the fold, and does the privacy policy mention its third-party cookies? Some sites, banks for example, forbid framing with an X-Frame-Options or CSP frame-ancestors header, and the box then shows "refused to connect". That is the other site's decision, not a bug in your HTML.
Common mistakes
Common mistakes
Leaving out title
On a page with a map and a video, a screen reader user hears only "frame, frame". Give a title that says what is inside, such as "Office location map"; words like "iframe" or "embed" do not help.
Sandboxing a same-origin page with allow-scripts allow-same-origin
Showing user-uploaded HTML from your own site with both tokens lets its script remove the sandbox. Serve user content from a separate domain, or drop allow-same-origin.
Pasting a provider's allow list without reading it
Copy-pasted embeds often carry permissions such as clipboard-write, gyroscope and autoplay. Remove whatever your feature does not need; each one is a power handed to a third party.
Fighting a site that forbids framing
When the box says "refused to connect", no attribute change will fix it. Check whether the site offers an official embed URL; if not, use a normal link.
Mini challenge
A widget board with two sandbox levels
Add two iframe widgets to a shop dashboard page. The first is a static notice, so keep every restriction on. The second writes today's date with JavaScript, so allow scripts and nothing else.
- Both iframes have a descriptive title and width/height
- The notice widget has a sandbox with no tokens
- The date widget has sandbox="allow-scripts" only, without allow-same-origin
- Content is written with srcdoc, so it works without internet access
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shop dashboard</title>
</head>
<body>
<h1>Shop dashboard</h1>
<!-- TODO: iframe 1 - title, size, sandbox with no tokens -->
<!-- srcdoc: a short notice, e.g. "Closed on Sunday" -->
<!-- TODO: iframe 2 - title, size, sandbox="allow-scripts" -->
<!-- srcdoc: <p>Today is <span id="d"></span></p> plus a <script> -->
<!-- that puts new Date().toDateString() into #d -->
</body>
</html>Expected: Two bordered boxes side by side under the heading (they wrap onto two lines on a narrow screen): the first shows the "Closed on Sunday" notice, the second shows today's date such as "Today is Mon Nov 09 2026" (the date depends on the day you open it). No console errors.
Show hint
Wrap srcdoc in single quotes and use only double quotes in the HTML inside it. Try putting a script into the first iframe as well, and watch the Console report it as blocked.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shop dashboard</title>
</head>
<body>
<h1>Shop dashboard</h1>
<iframe title="Shop notice" width="320" height="110" sandbox
srcdoc='<p style="font-family: sans-serif">Closed on Sunday</p>'>
</iframe>
<!-- Scripts only: no allow-same-origin, so no cookies or parent access -->
<iframe title="Today's date" width="320" height="110"
sandbox="allow-scripts"
srcdoc='<p style="font-family: sans-serif">
Today is <span id="d"></span></p>
<script>
document.getElementById("d").textContent =
new Date().toDateString();
</script>'>
</iframe>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- An iframe is a nested page with its own document, CSS, JavaScript and origin
- Give every iframe a title that says what is inside, plus width/height
- Start with an empty sandbox and add tokens one at a time when needed
- allow delegates device features, so list only the ones you need
- Always check event.origin when receiving cross-origin messages