HTML srcdoc Attribute

Beginner
4 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The srcdoc attribute embeds inline HTML inside an <iframe>. When present, it overrides src. Browsers that do not support srcdoc use the src URL instead (if provided).

The framed document’s location is about:srcdoc. Relative URLs inside resolve against the embedding page’s URL (MDN).

Baseline · Widely available.

Not deprecated. Stable on modern browsers (widely since about January 2020 for the DOM property; attribute support earlier in major engines).

XSS risk with untrusted HTML.

Do not put unsanitized user content in srcdoc. Prefer sandbox (avoid combining allow-scripts + allow-same-origin casually).

HTML Syntax

syntax.html
<iframe srcdoc=" html-string"></iframe>

Escape double quotes in the value as &quot; (or wrap the attribute in single quotes and escape '). Escape & as &amp; when you need a real character reference inside the string.

Attribute Values

The value is an HTML document string:

ValueDescriptionTry it
<p>Hello</p>Body-only markup (browsers usually accept it)Try it
Full document + src fallbacksrcdoc preferred; src for older browsersTry it
With sandboxRestrict scripts/forms for safer embedsTry it

Live Preview

Pick a snippet. The same iframe updates its srcdoc content.

Inline HTML only — no separate page URL required.

iframe

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Inline HTML

Render a small document without a separate HTML file.

HTML
<iframe
  title="Greeting"
  width="320"
  height="120"
  srcdoc="<p>Hello from <strong>srcdoc</strong>!</p>">
</iframe>
Try It Yourself

How It Works

  1. srcdoc holds the HTML shown inside the frame.
  2. title names the frame for accessibility.
  3. No src needed when every target browser supports srcdoc.
  4. Quotes — keep the attribute delimiters and inner HTML quotes consistent.

Example 2 — srcdoc + src Fallback

Prefer inline HTML; keep a URL for older browsers.

HTML
<iframe
  title="Demo with fallback"
  width="320"
  height="140"
  srcdoc="<p>Loaded via <code>srcdoc</code>.</p>"
  src="/html/attributes/src">
</iframe>
Try It Yourself

How It Works

  1. Supporting browsers show the srcdoc document.
  2. Unsupported browsers load the src URL instead.
  3. src is ignored when srcdoc is used and supported.
  4. Keep fallback useful — a real page, not a broken path.

Example 3 — Sandboxed srcdoc

Restrict what the inline document can do.

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

How It Works

  1. sandbox (empty) applies the strictest default restrictions.
  2. srcdoc still supplies the HTML content.
  3. Allow tokens (like allow-scripts) open capabilities deliberately.
  4. Avoid casually pairing allow-scripts with allow-same-origin — that can undo sandboxing.

Edge Cases & Pitfalls

Most issues are escaping, security, or confusing srcdoc with src.

escape

Escape quotes in the attribute

A bare " inside a double-quoted srcdoc ends the attribute early. Use &quot; or switch quote style.

override

srcdoc wins over src

In supporting browsers, the URL in src is not shown when srcdoc is set.

urls

Relative URLs use the parent page

MDN: relative links resolve against the embedding document. For in-frame anchors, use about:srcdoc#….

xss

Never trust raw user HTML

Sanitize first. Combine with sandbox when the frame should not reach the parent.

a11y

Always set a title

Screen readers need a name for the iframe — use the title attribute.

Browser Support

srcdoc is an HTML attribute on iframe for embedding inline HTML documents.

✓ Baseline · Widely available

srcdoc

Widely available in modern browsers. Keep a src fallback when you must support very old clients.

96% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer No support
Not supported
srcdoc Widely supported

Bottom line: Safe for modern sites. Provide src as a fallback and always title the iframe.

Key Takeaways

  • Role: inline HTML for an iframe (overrides src).
  • Fallback: keep src for browsers without srcdoc.
  • Security: sanitize + sandbox for untrusted content.
  • A11y: always give the iframe a title.

One line: srcdoc puts the HTML in the iframe attribute instead of loading a URL.

Frequently Asked Questions

srcdoc puts inline HTML into an iframe. When present, it overrides the src URL. Unsupported browsers fall back to src if you provide one.
Only iframe. It is not used on img, script, or other media elements.
Yes in supporting browsers. Keep src as a fallback document for older clients that do not understand srcdoc.
The framed document’s location becomes about:srcdoc. Relative URLs inside resolve against the embedding page’s URL unless you use an explicit about:srcdoc base for in-frame anchors.
Treat it carefully. Untrusted HTML can be an XSS risk. Prefer sandbox (without allow-same-origin when possible) and sanitize untrusted markup.
Ideally include a doctype and structure, but browsers often accept body-only content. Escape quotes in the attribute value.

Next: srclang

Continue with track language codes for media captions.

srclang 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.

8 people found this page helpful