The HTML srcdoc attribute
The HTML srcdoc attribute holds the HTML that an <iframe> displays, as in srcdoc="<p>Hello</p>". Double quotes and ampersands inside the value must be escaped. The frame's document gets the address about:srcdoc, and srcdoc wins when src is set too.
Overview
The HTML srcdoc attribute gives an <iframe> its content as a string of HTML. The browser builds a whole document from that string, so a frame can show a preview, a widget or an email body without a separate file or URL.
A srcdoc value needs only body content, since the doctype and the html, head and body tags are optional. A normal page without a doctype falls into quirks mode, the old rendering mode, but srcdoc documents are exempt. A value of <p>Hi</p> produces a complete page.
A srcdoc document always has the address about:srcdoc, the same name Chrome's console uses for the frame. Relative links inside it resolve against the parent page's base URL, not against about:srcdoc. It also runs under the parent page's Content Security Policy.
Without a sandbox attribute, a srcdoc frame shares its parent's origin, which gives its scripts complete access to the parent page. A bare sandbox attribute blocks those scripts. With sandbox="allow-scripts", they run but get a SecurityError when they reach for the parent.
Syntax
<iframe title="Order preview" sandbox
srcdoc="<p>Order <b>#1024</b> ships "today".</p>"></iframe>
Values
| Value |
|---|
| A string of HTML. |
Try it: srcdoc sandbox tester
Toggle real iframe sandbox flags, inspect about:srcdoc and window.origin live, and switch between escaped HTML attribute output and JavaScript el.srcdoc.
Example
<label for="code">HTML to preview (scripts won't run)</label><br>
<textarea id="code" rows="3" cols="38"><h2>Hello</h2>
<p>This came from srcdoc.</p></textarea><br>
<button id="show">Preview</button>
<iframe id="frame" title="HTML preview" sandbox height="110" srcdoc="<p>Press Preview to load the HTML.</p>"></iframe>
<script>
document.getElementById('show').addEventListener('click', function () {
document.getElementById('frame').srcdoc = document.getElementById('code').value;
});
</script>
Common srcdoc recipes
Server-side attribute escaping, automatic iframe height resizing, and Trusted Types compliance.
Escape & and " with htmlspecialchars() so untrusted markup cannot break out of the srcdoc attribute, and keep sandbox locked down.
<?php
$userHtml = '<p>Hello & welcome to "Acme".</p>';
$safeAttr = htmlspecialchars($userHtml, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
?>
<iframe
title="Message preview"
sandbox
srcdoc="<?= $safeAttr ?>"
></iframe>
Sandboxed allow-scripts frames have an opaque "null" origin, so report height back via postMessage and verify e.source === frame.contentWindow in the parent (inline scripts also inherit the parent CSP).
const frame = document.querySelector('#preview');
frame.setAttribute('sandbox', 'allow-scripts');
window.addEventListener('message', (e) => {
if (e.source !== frame.contentWindow) return;
if (Number.isFinite(e.data?.height)) {
frame.style.height = Math.max(0, Math.min(800, e.data.height)) + 'px';
}
});
frame.srcdoc = `<p>Dynamic content...</p><script>
const send = () => parent.postMessage({ height: document.body.scrollHeight }, '*');
window.addEventListener('load', send);
new ResizeObserver(send).observe(document.body);
<\/script>`;
Under a CSP enforcing require-trusted-types-for 'script', create a sanitization policy once at startup via trustedTypes.createPolicy() and keep DOMPurify.sanitize() in the fallback branch.
// Create policy once at startup (duplicate names throw TypeError under a trusted-types CSP allowlist)
const previewPolicy = window.trustedTypes?.createPolicy?.('srcdoc-preview', {
createHTML: (input) => DOMPurify.sanitize(input)
});
const frame = document.querySelector('#preview');
const rawHtml = '<p><strong>Verified</strong> preview markup.</p>';
frame.srcdoc = previewPolicy
? previewPolicy.createHTML(rawHtml)
: DOMPurify.sanitize(rawHtml);
srcdoc vs src vs data: URI vs Blob URL
Compare ways to load iframe HTML by origin isolation, inline support, and escaping requirements.
| Approach | Default origin | Inline | Escaping | Best fit |
|---|---|---|---|---|
srcdoc + sandbox |
Opaque (null) | Yes | HTML: &, " (JS: none) |
Sandboxed previews, emails, widgets |
srcdoc (no sandbox) |
Same as parent | Yes | HTML: &, " |
Trusted static snippets only |
src="https://..." |
Target origin | No (HTTP) | None | Hosted docs and third-party embeds |
data:text/html |
Opaque (null) | Yes | Percent-encoded | Legacy fallback (often CSP-blocked) |
createObjectURL(blob) |
Same unless sandboxed | Yes (Blob) | None (revoke URL) | Large client-generated previews |
Best practices
- Add
sandboxto every srcdoc frame that shows HTML you didn't write, and leave outallow-same-origin. Escaping keeps the markup inside the attribute but doesn't disarm it, since the browser decodes the attribute before building the frame. - Set markup from JavaScript through the
iframe.srcdocproperty. The property takes raw HTML, so you skip the quote and ampersand escaping that the attribute needs. - When you write the attribute by hand or on the server, turn each
&into&first and each"into"second. Done in that order, each quote ends up as", not&quot;. - Inside the frame, link to
about:srcdoc#endrather than#endwhen a link should jump within the framed page, since#endresolves against the parent page's base URL.
Accessibility
Label every srcdoc frame with a title that describes what it holds. People using assistive technology can use the title to identify an iframe, so a title such as "Order preview" helps more than "Frame" or "Preview 2".
Browser compatibility
Supported in all modern browsers since Edge switched to Chromium (Edge 79, January 2020). Legacy browsers without srcdoc fall back to the src attribute when both are present.
| Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
iframe srcdoc attributeEdge Legacy (<79) lacked support |
ChromeSupported since 20+ | EdgeSupported since 79+ | FirefoxSupported since 25+ | SafariSupported since 6+ |
iframe.srcdoc DOM property |
ChromeSupported since 20+ | EdgeSupported since 79+ | FirefoxSupported since 25+ | SafariSupported since 6+ |
sandbox isolation on about:srcdocOpaque unique origin by default |
ChromeSupported since 20+ | EdgeSupported since 79+ | FirefoxSupported since 25+ | SafariSupported since 6+ |
Frequently asked questions
What is about:srcdoc?
What is the difference between srcdoc and src on an iframe?
How do I escape quotes inside srcdoc?
" and each ampersand as &. An ampersand that the framed HTML itself escapes as & therefore becomes &amp; in the attribute.Is srcdoc safe for HTML that users submit?
Why does Chrome block scripts in about:srcdoc?
Blocked script execution in 'about:srcdoc', and the parent page's script-src policy can block the frame's scripts too.






















