The srcdoc attribute embeds inline HTML inside an <iframe>. When present, it overridessrc. 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).
Syntax
HTML Syntax
syntax.html
<iframe srcdoc="html-string"></iframe>
Escape double quotes in the value as " (or wrap the attribute in single quotes and escape '). Escape & as & when you need a real character reference inside the string.
sandbox (empty) applies the strictest default restrictions.
srcdoc still supplies the HTML content.
Allow tokens (like allow-scripts) open capabilities deliberately.
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 " 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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet ExplorerNo support
Not supported
srcdocWidely supported
Bottom line: Safe for modern sites. Provide src as a fallback and always title the iframe.
Remember
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.