An iframe with srcdoc contains a nested document supplied by an attribute, with its own browsing context and security rules.
HTML iframe srcdoc: isolate a small preview deliberately
Use it for a real task
A receipt editor previews a tiny static notice before publication. The srcdoc value is trusted, fixed markup in this example, not a string built from user input. An empty sandbox applies restrictions to the nested document, and title gives the frame a useful name. A preview that needs scripts or form submission should receive only the minimum permissions required after a separate threat review.
<iframe
title="Receipt notice preview"
sandbox=""
width="480"
height="180"
srcdoc="<!doctype html><html lang='en'><body><p>Receipt R-47 is awaiting review.</p></body></html>">
</iframe>What the markup guarantees
Do not concatenate untrusted HTML into srcdoc. The sandbox is not a sanitizer; unsafe markup still needs a controlled input and output policy. The frame's size is an initial viewport, not a promise that its contents will fit.
Cost and limits
Each frame adds a document, layout work, and isolation overhead. Many previews can be expensive; reuse a single frame or render safe static text when isolation is unnecessary.
Common Mistakes
- Do not treat sandbox as HTML sanitization.
- Do not combine permissions without reviewing their interaction.
- Do not omit a descriptive frame title.
