**`<iframe>`** loads an entirely separate document — often from another domain — inside a rectangular frame within the current page: a YouTube video embed, an embedded map, a payment widget, or a third-party comment system are all typically `<iframe>`s. The **`sandbox`** attribute can restrict what the embedded content is allowed to do (block scripts, forms, popups, etc.) for security when embedding untrusted content, and **`title`** is required for accessibility, since screen readers need a way to describe what the embedded frame contains.
1Understanding <iframe>
`<iframe>` loads an entirely separate document — often from another domain — inside a rectangular frame within the current page: a YouTube video embed, an embedded map, a payment widget, or a third-party comment system are all typically <iframe>s. The `sandbox` attribute can restrict what the embedded content is allowed to do (block scripts, forms, popups, etc.) for security when embedding untrusted content, and `title` is required for accessibility, since screen readers need a way to describe what the embedded frame contains.
Content inside an iframe from a different origin (domain) can't be accessed or manipulated by your page's JavaScript at all by default — this cross-origin isolation is a deliberate browser security boundary, not a bug to work around.
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
width="560" height="315"
title="YouTube video player"
allowfullscreen>
</iframe>2Practical Example
Here is a real-world application of <iframe> showing how it is used in production HTML.
<!-- Sandboxing an untrusted embed: no scripts, no form submission -->
<iframe src="https://untrusted-widget.example.com" sandbox="allow-same-origin" title="Third-party widget"></iframe>3Best Practices
Follow these guidelines when working with <iframe>:
1. Always include a descriptive title attribute for accessibility
2. Use the sandbox attribute to restrict untrusted embedded content's capabilities
3. Set explicit width/height (or CSS aspect-ratio) to avoid layout shift while the iframe loads
Tip: Content inside an iframe from a different origin (domain) can't be accessed or manipulated by your page's JavaScript at all by default — this cross-origin isolation is a deliberate browser security boundary, not a bug to work around.
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ"
width="560" height="315"
title="YouTube video player"
allowfullscreen>
</iframe>