HTML Iframes: Embedded Pages and Sandbox
Embed a separate document
An iframe creates a nested browsing context: a document inside the current page. src identifies a URL; srcdoc supplies literal HTML and takes precedence when both are present. Give each frame a descriptive title for assistive technology.
iframe title="Workshop notice" srcdoc="<p>Workshop starts at 10.</p>" width="300" height="90"></iframe>The nested document displays the notice in its own frame.
Restrict embedded content
An empty sandbox attribute applies restrictions including disabling scripts, form submissions and ordinary same-origin access. Tokens selectively relax restrictions. Never combine allow-scripts and allow-same-origin for untrusted same-origin content; it can undermine the sandbox. Only put trusted or safely sanitized HTML in srcdoc.
iframe title="Restricted announcement" sandbox srcdoc="<p>Read-only announcement.</p>" width="300" height="90"></iframe>The static text appears with script and origin restrictions.
Control permissions and provide a link
allow delegates selected features subject to Permissions Policy; it cannot override a stricter parent policy. referrerpolicy controls referrer information sent with a frame request. Remote pages may refuse embedding using CSP frame-ancestors or X-Frame-Options. Supply an adjacent normal link when a separate page is available.
iframe title="Workshop details" sandbox referrerpolicy="no-referrer" srcdoc="<p>See the details below.</p>"></iframe><p id="details">Workshop details: bring a notebook.</p><a href="#details">Read the details outside the frame</a>The text outside the frame remains accessible without relying on the nested document.
Common mistakes and practice
A frame is not a substitute for a link or a security boundary by itself. Do not assume a remote page permits embedding, or that a sandbox automatically makes arbitrary HTML safe.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Iframes: Embedded Pages and Sandbox — Knowledge Check
Apply the media or embedding concepts from this lesson.
Which attribute describes the frame for assistive technology?
What does an empty sandbox do?
What can prevent embedding a remote page?
What does srcdoc provide?
Why should frame permissions be granted selectively?