srcdoc

Embed inline HTML content in an iframe without a separate document. Useful for sandboxed previews, user-generated content isolation, and self-contained examples.

Overview

The srcdoc attribute on <iframe> lets you embed inline HTML content directly in the attribute value, without pointing to a separate URL. The iframe renders the provided HTML as a complete document in its own browsing context.

Applies to: <iframe>

When both srcdoc and src are present, srcdoc takes priority. The src serves as a fallback for browsers that do not support srcdoc (all modern browsers do).

Values

AttributeValueBehavior
srcdocHTML string (escaped)Renders the HTML as the iframe's content
srcURLFallback if srcdoc is absent or unsupported

Basic Usage

Provide HTML content as the attribute value. All HTML special characters must be escaped since they are inside an attribute.

Escaping Requirements

Because srcdoc content lives inside an HTML attribute, you must escape HTML entities. This is the most common pain point with srcdoc.

Sandboxed Content

Pair srcdoc with the sandbox attribute for secure content isolation. The content runs in a unique origin, completely separated from the parent page.

Priority over src

When both attributes are present, srcdoc wins. This originally allowed for graceful degradation to a server-rendered URL in older browsers.

Live Preview Pattern

A common use case is a live HTML preview that updates as the user types. Set the iframe's srcdoc property via JavaScript for dynamic content.

Email Rendering

Email clients often render HTML emails in iframes to isolate the email's styles and scripts from the application UI. The srcdoc + sandbox combination is ideal for this.

Accessibility

  • Always provide a title attribute on iframes using srcdoc. Screen readers announce the title to help users understand the iframe's purpose.
  • The content inside the iframe is a separate document with its own accessibility tree. Ensure the inline HTML is well-structured with headings and landmarks.
  • Sandboxed iframes that prevent keyboard navigation can trap focus. Test keyboard flow into and out of the iframe.

Limitations

  • HTML escaping makes complex content hard to read in source code. For longer content, setting iframe.srcdoc via JavaScript avoids the escaping burden.
  • The content has a unique, opaque origin. It cannot access the parent page's cookies, localStorage, or DOM (and the parent cannot access the iframe's DOM when sandboxed).
  • There is no way to link to a specific anchor within a srcdoc iframe from the parent page.
  • Very large srcdoc values can bloat the HTML document size, since the content is inline rather than loaded on demand.
  • The srcdoc content does not have a URL, so it cannot be bookmarked, shared, or opened in a new tab.

See Also

  • sandbox — restrict iframe capabilities for security
  • <iframe> — the iframe element
  • loading — lazy-load iframes below the fold
  • referrerpolicy — control referrer for iframe requests