HTML Formatter

Converter

Beautify or minify HTML with indentation from 1 to 8 spaces. Void elements, pre and textarea whitespace, script and style content, and comments are preserved.

HTMLInput
HTMLOutput

About HTML Formatter

Format and beautify HTML in your browser with the native DOMParser, or minify it to a single line. Markup is parsed into a DOM tree and re-emitted with indentation that follows tag nesting, adjustable from 1 to 8 spaces, with 2, 3 and 4 one click away. Void elements such as br, img, input, meta and link keep their original form and are never turned into self-closing tags. Content inside pre and textarea passes through verbatim, script and style bodies are left untouched, and attribute order and names are preserved while the parser settles quote style on double quotes. Runs of inline elements stay on one line, so no extra spaces appear where none existed, and minify mode drops indentation and inter-tag whitespace while keeping comments. Typical uses are cleaning up scraped markup, checking what the browser parser actually built, and compressing production HTML. A one-line fragment expands into an indented hierarchy where repaired tag pairs become visible. Format before review and nesting differences read faster. Whitespace inside preserved elements is content rather than layout, and recognising them is the first call in formatting. Elements that display text verbatim keep their spaces, newlines and tabs exactly as written, so any reflow changes what appears on the page; these often hold code or verse where layout is meaningful, and the damage is especially subtle. Skip their interiors and indent only around them. Whitespace between inline elements takes part in rendering, unlike block elements. A newline between two inline elements collapses to a space when rendered, so moving them to separate lines inserts a gap where two words were tight and alters the layout. Treat a run of inline elements as a unit that must not be freely reflowed, or remove the whitespace with an explicit comment. Quote form and void-element writing are not free to change, because variants behave differently in parsing and in later processing. The parser normalises quotation to double quotes, but it does not decide whether your pipeline needs XML-style slashes, so a self-closed img comes out as a plain img. If a downstream tool only accepts XML, use an XML formatter rather than expecting this one to keep the slash. Template syntax and custom tags add risk, since their content rules come from a framework rather than a standard. Conditional and loop markers mix with ordinary tags, and some frameworks place structural meaning in attributes with indentation rules unlike the standard, so reflowing by standard rules can change behaviour. Use an implementation that recognises the framework. The purpose decides how far to go, which needs settling beforehand. For readability alone, adjusting indentation between elements is safe and sufficient, while sorting attributes or normalising spaces inside text belongs to style conventions that need a team agreement and usually an automated check, or different people still produce different results. This tool only re-indents and collapses whitespace between tags. Finally, formatting always produces broad diffs, so keep it in a separate commit from content changes. Mixing the two hides the real changes and makes review and rollback harder, whereas a dedicated formatting commit runs once and leaves later diffs clean. This habit applies to any formatting tool.

How It Works Internally

Formatting re-indents by tag nesting. Compared with XML there are two extra whitespace-sensitive regions: content inside preformatted elements is preserved, and whitespace between inline elements renders as a single visible space — so breaking a line between two inline elements adds a space, the usual cause of "the spacing changed after formatting".

Input<span>a</span><span>b</span>
OutputKept on one line → renders "ab" | Broken across lines → renders "a b" (one extra space)

Measured on This Page

Fixed: whitespace inside pre<pre> keep </pre> came out as <pre>keep</pre> — pre exists precisely to keep whitespace, and trimming it deforms code blocks and ASCII art wholesale. pre and textarea now pass through verbatim, tags included
Structure and attributes<div class="a"><p>hi</p></div> keeps its nesting and quoted attributes with 2-space indent
Void elements<img src="x.png"><br> stays as <img> and <br>, without being self-closed into <img />
Entities not double-escaped<p>a &amp; b</p> still emits &amp;, never &amp;amp; — the most common double-escape bug

Try the smallest possible case: <span>a</span><span>b</span> renders as ab, and putting each tag on its own line renders as a b — a visible extra gap, caused by the whitespace becoming a text node. That is the whole hazard of pretty-printing markup, and it is why indenting inline content needs care while block containers take indentation harmlessly. Inside pre or textarea no collapsing happens at all, so those 2 tags pass through untouched here rather than being tidied.

How this was measured: Four HTML inputs (nesting with attributes, void elements, an entity, and a pre block holding runs of spaces) were formatted, the output parsed with Python html.parser to confirm structure, and attribute and entity forms checked. Measured before and after the fix. · Measured 2026-10-31

How to Use

  1. Open "HTML Formatter"
  2. Paste the raw HTML source
  3. Click "Beautify" or "Minify"
  4. Pick 2, 3 or 4 spaces, or type any width from 1 to 8
  5. Click the copy icon to take the result

Use Cases

  • Tag nesting inspection — Expand minified or generated HTML into a hierarchy; pairs repaired by the parser can then be checked against the original.
  • Template cleanup — Format HTML templates to make tag structure clear and improve maintainability.
  • Code review — Format HTML before code review to make diffs clearer and more focused.
  • Debug layouts — Expand one-line HTML from templates or server rendering to inspect nesting and closure issues.
  • Minify for release — Compress HTML to a single line, removing whitespace to shrink delivery size.

FAQ

Does formatting affect HTML functionality?

No. Formatting only touches indentation and line breaks, and inline nodes stay on one line, so no spaces appear from nowhere. Two things do change: runs of spaces inside text collapse to one (outside pre and textarea), and quotes settle on double quotes, both matching how a browser renders the markup anyway.

Does it support HTML5?

Yes. DOMParser supports HTML5 tags, custom elements, SVG, and more.

Is my pasted HTML leaked or stored?

No. Formatting is done entirely in your local browser via DOMParser, with no network requests made during processing. Your HTML never leaves your device and is neither saved nor uploaded.

Will script or style content be reformatted?

No. These are raw-text elements; their inner content stays untouched to avoid breaking JS or CSS semantics.

Can it fix broken tag nesting?

It repairs them, but gives no warning. DOMParser fixes pairing by browser rules and re-emits the hierarchy, so unclosed tags vanish from the output; compare input with output when you need to find the original mistake.

Does it support minification?

Yes. Minify removes indentation and the whitespace between tags to produce single-line HTML. Spaces between words stay, and comments are kept rather than stripped.

The page renders differently after formatting — why?

Formatting only re-indents and collapses whitespace between tags, leaving tag structure and attribute values alone. Two known traps: reflowing content inside <pre> (whitespace is meaningful there) and inserting newlines between inline elements, which adds visible spaces (e.g. between <a> and text). Exclude both from formatting.

Template syntax (Vue/blade {{ }}, @if) gets mangled?

An HTML formatter treats directives as plain text and may split a directive from its arguments across lines. Use a framework-aware formatter (Prettier with Vue/Blade plugins) for templates; this tool is for pure HTML and static pages.