HTML <iframe> Tag

The <iframe> element creates a nested browsing context: another document shown inside a rectangular frame on the current page.

Load a URL with src, or put markup directly in srcdoc. Always give a meaningful title for accessibility.

Baseline · Widely available.

Not deprecated. Core support has been stable across browsers since about July 2015. Some attributes (for example loading) still vary by browser.

Performance warning.

Each iframe is a full document environment and costs memory and network. Prefer fewer frames, and use loading="lazy" when the embed is off-screen.

Deprecated attributes.

frameborder, align, scrolling, marginwidth, marginheight, and longdesc are obsolete — use CSS for borders and overflow instead.

HTML Syntax

syntax.html
<iframe src=" url" title="label"></iframe>

Both start and end tags are required. Content between the tags is not a reliable fallback in modern browsers; put a normal link outside the frame if users may need another way in.

Attributes

The attributes you will use most often on <iframe>:

AttributeDescriptionTry it
srcURL of the page to embed. Use about:blank for an empty same-origin frameTry it
srcdocInline HTML to embed. Overrides src when supported; otherwise the browser uses srcTry it
titleAccessible name for the frame (required in practice for screen readers)Try it
width / heightFrame size in CSS pixels. Defaults are about 300 × 150Try it
sandboxApplies extra restrictions. Empty value = all restrictions; space-separated tokens lift someTry it
allowPermissions Policy for features inside the frame (camera, fullscreen, etc.)See FAQ / MDN
allowfullscreenLegacy boolean for fullscreen. Prefer allow="fullscreen" on modern pagesSee allow
loadingeager (default) or lazy to defer off-screen loads. Support variesTry it
nameTargetable browsing-context name for links and forms (target)See name docs
referrerpolicyWhich referrer to send when fetching the frame resourceSee referrerpolicy

Global Attributes

The <iframe> tag also supports the Global Attributes in HTML.

Live Preview

Switch demo content rendered with srcdoc and a real <iframe>.

A real <iframe> with srcdoc. Pick a demo, then Apply (or tap a chip).

Preview

srcdoc hello · no sandbox

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Embed a Page with src

Load another origin and label the frame for assistive technology.

HTML
<iframe
  src="https://example.com"
  title="Example.com homepage"
  width="480"
  height="200"
></iframe>
Try It Yourself

How It Works

  1. Set src to the page URL you want inside the frame.
  2. Add title so screen readers can announce what the embed is.
  3. Set width / height (or use CSS) so the box has a stable size.
  4. Leave the element empty — nested browsing contexts do not use child markup as a modern fallback.

Example 2 — Inline HTML with srcdoc

Embed small HTML without a separate URL. Useful for demos and sandboxed previews.

HTML
<iframe
  title="Inline HTML sample"
  width="480"
  height="120"
  srcdoc="<p>Hello from <strong>srcdoc</strong>.</p>"
></iframe>
Try It Yourself

How It Works

  1. Put markup in srcdoc as an HTML string (escape quotes carefully in attributes).
  2. Keep title so the frame still has an accessible name.
  3. Know the override: when srcdoc is supported, it wins over src.
  4. Optional fallback: still set src for older clients that ignore srcdoc.

Example 3 — Restrict Content with sandbox

Apply the strongest default restrictions, then lift only what you need.

HTML
<iframe
  title="Sandboxed preview"
  width="480"
  height="120"
  sandbox
  srcdoc="<p>Scripts and forms are blocked by default.</p>"
></iframe>
Try It Yourself

How It Works

  1. Add empty sandbox to enable all sandbox restrictions.
  2. Lift selectively with tokens such as allow-scripts or allow-forms only when required.
  3. Avoid the risky pair: do not combine allow-scripts and allow-same-origin on same-origin embeds — the frame can remove its own sandbox.
  4. Use for untrusted HTML (user content, third-party widgets) when you need defense in depth.

Example 4 — Lazy Load and Clear Title

Defer an off-screen embed and keep the accessible name specific.

HTML
<iframe
  src="https://example.com"
  title="Example.com preview (lazy)"
  width="480"
  height="200"
  loading="lazy"
  referrerpolicy="strict-origin-when-cross-origin"
></iframe>
Try It Yourself

How It Works

  1. Set loading="lazy" so the browser can wait until the frame is near the viewport.
  2. Keep a precise title — lazy loading does not replace accessibility labeling.
  3. Optional referrerpolicy limits what referrer data the embed request sends.
  4. Treat as enhancement: if loading is ignored, the iframe still works with eager load.

Edge Cases & Pitfalls

a11y

Missing title

Without a title, assistive tech users must enter the frame to learn what it is. Always label embeds.

security

sandbox + same origin

Combining allow-scripts and allow-same-origin can let the framed page escape the sandbox.

cost

Too many frames

Every iframe has its own document. Check performance before stacking many embeds.

legacy

Prefer CSS over frameborder

Use border / overflow in CSS. Deprecated presentational attributes should not appear in new code.

events

error is unreliable

Browsers generally do not fire error on iframes, and load may still fire when content fails — do not probe URLs that way.

pdf

PDF + sandbox

sandbox can block the built-in PDF viewer. Offer a normal link outside the frame as a backup.

Browser Support

The <iframe> element is core HTML embedding in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<iframe>

Widely available since about July 2015 (and earlier as HTML frames). Chrome, Firefox, Safari, Edge, and Opera all support it. Attributes such as loading and some Permissions Policy features vary.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<iframe> Widely supported

Bottom line: Safe to use everywhere for basic embeds. Treat loading, allow, and newer sandbox tokens as progressive enhancements.

Key Takeaways

  • Role: <iframe> embeds another document in a nested browsing context.
  • Must-haves: meaningful title, explicit size, and a clear src or srcdoc.
  • Safety: use sandbox / allow thoughtfully for third-party or untrusted content.
  • Practice: prefer CSS over deprecated frame attributes; lazy-load off-screen embeds.

One line: embed with <iframe>, always title it, and sandbox untrusted content.

Default CSS Settings

User agents treat <iframe> as a replaced element. Typical defaults (exact values can vary):

default.css
iframe:not([hidden]) {
  /* approximate UA defaults */
  width: 300px;   /* HTML width attribute default */
  height: 150px;  /* HTML height attribute default */
  border: 2px inset;
}

Override size and border with CSS when you need a borderless or fluid embed. object-fit has no effect on iframes.

Frequently Asked Questions

The iframe element embeds another browsing context — typically another HTML page — inside the current page. Common uses are maps, videos, docs, and sandboxed previews.
Yes for accessibility. Screen reader users rely on title to know what the frame contains before entering it. Use a short, specific label.
src loads a URL. srcdoc embeds HTML markup directly in the attribute and overrides src when both are present. Browsers that lack srcdoc fall back to src.
Use sandbox when you embed untrusted or third-party content and want extra restrictions (scripts, forms, top navigation, and more). Add tokens only for what you must allow.
No. frameborder, align, scrolling, marginwidth, marginheight, and longdesc are deprecated. Control borders and scroll with CSS instead.
Same-origin frames can use contentWindow / contentDocument. Cross-origin access is blocked by the same-origin policy; use window.postMessage for safe messaging.

Next: image

Continue with the HTML image tag tutorial.

image tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful