👁️ Live DOM Sandbox

HTML Realtime Live Preview & Viewer

Write or paste raw HTML code and inline CSS to preview live DOM rendering safely in an isolated iframe.

HTML Source Code
Live Browser Preview

How the preview renders your code

The editor contents are passed to an iframe through its srcdoc attribute, which tells the browser to treat the string as a complete HTML document. The browser parses it exactly as it would a page loaded from a server: it builds a DOM, applies styles and runs scripts, all inside the frame.

Because the document has no real address, its base URL is about:srcdoc. Relative paths such as images/logo.png or /styles.css have nothing to resolve against and will not load. Use absolute URLs starting with https:// for any image, font or stylesheet you want to test. Those requests go directly from your browser to whichever server hosts the file, just as they would on a live page.

What the sandbox allows and blocks

The iframe's sandbox attribute is set to allow-scripts and nothing else. Each sandbox flag removes one restriction, so everything not explicitly allowed stays blocked. In practice that means:

  • JavaScript runs, so click handlers, DOM manipulation and timers work.
  • The frame has a unique opaque origin. Accessing localStorage, sessionStorage or document.cookie throws a security error, and code that depends on them will fail at that line.
  • alert(), confirm() and prompt() do nothing, because allow-modals is absent. Use console.log and your browser's developer tools to inspect script output.
  • Forms do not submit and links cannot navigate the parent page.

Worked example: testing a responsive breakpoint

Media queries inside the preview respond to the iframe's width, not the browser window. That makes the panel useful for checking breakpoints without resizing your whole browser. Paste a style block such as .card { display: flex; } @media (max-width: 500px) { .card { display: block; } } and two child divs inside a .card element.

If the preview panel is wider than 500px the children sit side by side. Narrow the browser until the panel drops below 500px and they stack. The same principle applies to container widths and viewport units: 100vw inside the frame equals the frame's width.

Common rendering surprises

Leaving out <!DOCTYPE html> puts the document into quirks mode, where the browser emulates old rendering bugs. Table cells may ignore font sizes, and images inside table cells can gain extra gaps. If a layout looks subtly wrong in the preview, add the doctype at the top before debugging anything else.

The browser also corrects broken markup silently. An unclosed div or a p element containing a div will be repaired according to the HTML parsing rules, which may not match what you intended. If you see unexpected nesting, run the markup through a validator such as the W3C Nu checker to find the error.

Finally, default browser styles apply inside the frame. Body has an 8px margin and headings have their own sizes unless your code resets them, so the preview may not match a site with a CSS reset.

HTML Live Preview: frequently asked questions

Why are my images not showing in the preview?

Relative image paths cannot be resolved because the preview document has no base address. Replace them with full https:// URLs, or embed small images as data URIs. Also check that the hosting server allows the image to be loaded from other sites.

Why does my script throw an error when it uses localStorage?

The preview frame runs in a sandbox without allow-same-origin, which gives it a unique origin with no storage access. Calls to localStorage or cookies throw a security error. Wrap them in try and catch, or test that part of your code in a normal page.

Can I load external CSS frameworks or JavaScript libraries?

Yes. Link tags and script tags with absolute CDN URLs load normally, because the sandbox does not block network requests. The files are fetched by your browser directly from the CDN, so they need an internet connection.

What is the difference between srcdoc and a blob or data URL?

All three can show generated HTML in a frame. srcdoc is the simplest because the HTML string is set directly as an attribute, with no URL to create or clean up. Blob URLs are handy for opening generated pages in a new tab, but they need to be created and revoked in script.

All html & javascript utilities