Images & Media

HTML <iframe> element

Creates a nested browsing context. Give it a descriptive title and apply appropriate sandbox restrictions for untrusted content. An iframe creates a separate browsing context. Give it a meaningful title, restrict untrusted content with sandbox and permissions policy, and consider lazy loading for offscreen frames. Same-origin policy controls script access across origins. Inline srcdoc content needs the same trust and escaping care as other HTML. It accepts global HTML attributes; individual global features may be inapplicable or restricted.

Formal Syntax & Structure

html syntaxhtml
<iframe title="Preview" src="page.html"></iframe>

Parameters & Attributes

NameTypeDescription
srcURLSpecifies the resource to load for the given element, such as an image, script or media file. The resource type, loading behavior and security rules depend on the element.
srcdocHTML document textSupplies inline HTML for an iframe and takes precedence over src. Treat untrusted markup as dangerous; use a restrictive sandbox and sanitize as appropriate.
namestringProvides an element-specific name: a form field key, radio group name, named frame, metadata key, slot name or other defined purpose. Its meaning depends on the element; it is not a unique DOM identifier.
sandboxspace-separated permission tokensApplies restrictions to an iframe. An empty attribute applies the full sandbox; allow-* tokens selectively relax restrictions. Combining allow-scripts with allow-same-origin for same-origin content can undermine isolation.
allowpermissions policyRestricts or delegates supported browser features for an iframe using a permissions policy. It cannot override a more restrictive policy imposed by the parent or response headers.
allowfullscreenbooleanPermits fullscreen requests from an iframe subject to permissions policy and user-agent rules. Prefer an appropriate allow policy for new code.
widthnon-negative integerDefines element-specific width, commonly in CSS pixels. On images it can establish an intrinsic aspect ratio together with height; on canvas it sets the bitmap width.
heightnon-negative integerSets an element-specific height, commonly in CSS pixels. On images, pair it with width matching the intrinsic aspect ratio to reserve layout space. It is not a CSS length with units.
referrerpolicyreferrer policy keywordControls the referrer information sent with an eligible fetch or navigation. no-referrer omits it; strict-origin-when-cross-origin is a common default policy. It does not prevent all tracking.
loadingenumerated: eager, lazyHints whether supported images or iframes should be loaded immediately or deferred until near the viewport. Avoid lazy-loading the likely largest visible content at the top of the page.

Return Value

HTMLIFrameElement

Content model: No author child content in HTML; use src or srcdoc.

Working Example

Example Snippethtml
<!doctype html><html lang="en">  <head>    <meta charset="utf-8">    <title>HTML example</title>  </head>  <body>    <iframe title="Welcome preview" sandbox srcdoc="&lt;p&gt;Welcome&lt;/p&gt;"></iframe>  </body></html>

Browser Compatibility

Minimum supported versions across modern rendering engines

Chrome
1
Yes
Edge
12
Yes
Firefox
1
Yes
Safari
≤4
Yes
Opera
≤15
Yes
Checked desktop BCD snapshot: iframe element, not all embedding policies. Versions denote the recorded start of support (≤ means at least that early), not a guarantee of all behavior. Unresolved mirror and preview rows omitted. BCD records historical Safari hidden-frame and Firefox CSS resize caveats.
HTML Living Standard
HTML Reference