Learn how to safely embed external content using iframes, the sandbox attribute, srcdoc, and the deprecated object and embed elements.
The <iframe> (inline frame) embeds another HTML document inside the current page. Common uses:
src — URL of the document to embed.title — Required for accessibility. Screen readers use this to describe the iframe content.width / height — Dimensions of the frame.loading="lazy" — Defers loading until the iframe is near the viewport.allowfullscreen — Permits the embedded content to go fullscreen (needed for video players).Embedding third-party content is a security risk. The sandbox attribute restricts what the embedded page can do:
<iframe src="widget.html" sandbox></iframe>With sandbox alone (no values), the iframe:
You can selectively re-enable features:
sandbox="allow-scripts" — Enables JavaScript.sandbox="allow-forms" — Enables form submission.sandbox="allow-same-origin" — Treats the iframe as same-origin.sandbox="allow-popups" — Allows window.open and target="_blank".Warning: Combining allow-scripts and allow-same-origin essentially removes the sandbox. Avoid this unless you trust the content completely.
The allow attribute controls browser features (permissions policy):
<iframe src="video.html" allow="camera; microphone; fullscreen"></iframe>The srcdoc attribute lets you provide HTML content directly, without loading an external URL:
<iframe srcdoc="<h1>Hello from srcdoc!</h1><p>This is inline HTML.</p>"
sandbox
title="Inline demo">
</iframe>This is useful for:
When both src and srcdoc are present, srcdoc takes priority.
Before HTML5 standardised <video>, <audio>, and enhanced <iframe>, two elements were used for embedding:
<object>A generic container for embedded content — PDFs, Flash, SVG, even other HTML pages.
<object data="document.pdf" type="application/pdf" width="600" height="400">
<p>Your browser cannot display PDFs. <a href="document.pdf">Download it</a>.</p>
</object><embed>A void element (no closing tag) for plugin content:
<embed src="animation.swf" type="application/x-shockwave-flash" width="400" height="300">Today, <object> is primarily used for PDF embedding. <embed> is rarely needed. Prefer <iframe>, <video>, and <audio> for modern use cases.
<!-- Embedded video with sandbox -->
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
title="YouTube video player"
width="560"
height="315"
loading="lazy"
allowfullscreen
></iframe>
<!-- Sandboxed srcdoc iframe -->
<iframe
srcdoc="<h2>Sandboxed Content</h2><p>This runs in an isolated sandbox.</p>"
sandbox
title="Sandboxed demo"
width="400"
height="200"
></iframe>
<!-- PDF embedding with object -->
<object data="report.pdf" type="application/pdf" width="600" height="400">
<p>Cannot display PDF. <a href="report.pdf">Download instead</a>.</p>
</object>What does the sandbox attribute without any values do to an iframe?