HF
HTML Formatter & Minifier
Pretty-print messy HTML with proper indentation, or minify it by stripping comments and extra whitespace.
More Developer Tools Tools
How to Use HTML Formatter & Minifier
Paste HTML markup into the box. Click Format to re-indent it with one tag per line and consistent nesting, or Minify to strip HTML comments and collapse whitespace down to a compact single-line (or near single-line) output. Everything runs locally using a small built-in tokenizer — nothing is uploaded.
About HTML Formatter & Minifier
HTML is the markup language every web page is built from, and unlike XML it is famously forgiving: browsers will render HTML with unclosed tags, unquoted attributes, and elements in technically invalid places, silently patching things up as they go. That leniency is convenient for browsers but means HTML in the wild is often inconsistently formatted — auto-generated markup from a CMS, a template engine, or a minifier is frequently delivered as one giant unbroken line, and hand-written HTML often mixes indentation styles when multiple people or tools have touched it.
Because HTML's parsing rules are so permissive and full of special cases (voidelements like <br> and <img> that never have a closing tag, elements like <li> that browsers will auto-close for you, content models that vary per element), there is no built-in browser API equivalent to DOMParser/XMLSerializer that guarantees a clean re-indent while preserving exact semantics — feeding real-world HTML through an XML-style parser will break on things that are perfectly normal in HTML (an unclosed <img> tag, an unencoded standalone & in text, a <p> that browsers auto-close when a new block starts). So this tool takes a different approach: a small hand-rolled tokenizer that walks the markup character by character, recognising opening tags, closing tags, self-closing/void elements, text, comments and doctype declarations, and reconstructs indentation based on nesting depth.
Format mode walks the token stream and emits one tag (or text run) per line, increasing indentation by two spaces on each opening tag and decreasing it on each matching closing tag, while treating known void elements (br, img, input, hr, meta, link, and the rest of the HTML5 void element list) as never opening a new indentation level. Minify mode strips HTML comments (<!-- ... -->) entirely, then collapses runs of whitespace between tags down to nothing, producing compact output while leaving the text content of the page (words a user would actually read) intact.
A hand-rolled tokenizer like this cannot fully replicate everything a browser's HTML5 parsing algorithm does — the real specification (the WHATWG HTML parsing algorithm) is thousands of lines of rules covering implied tag closing, table-specific parsing quirks, foreign content (SVG/MathML embedded in HTML), and error recovery for almost any malformed input imaginable. This tool handles the markup patterns that make up the overwhelming majority of real HTML — nested elements, attributes with single/double/no quotes, void elements, comments, and text content — but it does not reformat the *contents* of <pre>, <script>, <style> or <textarea> elements, because whitespace inside those is significant (a <pre> block's formatting is meant to be preserved verbatim, and JavaScript/CSS inside <script>/<style> is not HTML and re-indenting it as if it were tags would corrupt it). Those elements' inner content is passed through untouched, with only the outer tags treated as normal HTML tokens.
For everyday tasks — cleaning up markup copied from a browser's "view source", tidying a component template, or shrinking a static HTML snippet before pasting it somewhere size-constrained — this lightweight, dependency-free approach is fast, entirely private (nothing leaves your browser), and good enough. For anything going into a production build pipeline, a proper tool such as Prettier (formatting) or html-minifier-terser (minification) will handle far more edge cases correctly.
Details & Tips
What Format does: tokenizes the input into tags, text, comments and doctype declarations, then re-emits them with one logical unit per line and two-space indentation per nesting level. Void/self-closing elements (area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr) never increase indentation, matching how browsers treat them. Attributes on a tag are preserved exactly as written, including their quoting style.
What Minify does: HTML comments are removed entirely (<!-- ... --> and everything between the markers), then whitespace-only text between tags is collapsed to nothing and runs of whitespace within text content are collapsed to a single space, producing compact but still textually-correct output.
Known limitations (read before using on anything you can't visually double-check afterward):
• The contents of <pre>, <textarea>, <script> and <style> elements are never reformatted or reflowed — only the surrounding tags are touched — because whitespace there is semantically significant (preformatted text) or because the content is a different language entirely (JS/CSS) that a HTML-only tokenizer should not attempt to re-indent.
• This is not a full HTML5 parser: it does not implement the browser's implied-tag-closing rules (e.g. automatically closing an open <p> when a new <div> starts, or closing <li> when the next <li> begins) the way a real browser engine does. Markup that relies on browsers auto-fixing missing closing tags may format with unexpected nesting.
• Malformed markup (e.g. a stray unmatched closing tag) is handled defensively rather than rejected outright, but the output in that case should be treated as best-effort, not authoritative.
• Inline SVG or MathML embedded in HTML is tokenized using the same generic tag rules as HTML and is not treated as "foreign content" the way the HTML5 spec requires — it will usually format fine in practice but is not guaranteed to be byte-identical to spec-correct handling.
Worked example — minifying:
Input: multi-line HTML with indentation and an HTML comment documenting a section.
Minified output: the comment is removed and all inter-tag whitespace is collapsed, producing a single compact line with the same visible content and no comment.
For production build pipelines, use a maintained tool such as Prettier for formatting or html-minifier-terser for minification — both handle the full HTML5 parsing model and many more edge cases than a lightweight in-browser tokenizer reasonably can.
Frequently Asked Questions
Does the HTML formatter reformat the JavaScript inside <script> tags?
No. Content inside <script>, <style>, <pre> and <textarea> elements is deliberately left untouched — only the surrounding HTML tags are re-indented — because reformatting those contents as if they were HTML would corrupt code or destroy meaningful whitespace.
Is this a full HTML5 parser?
No, it is a lightweight hand-rolled tokenizer covering common markup patterns. It does not implement the browser's full implied-tag-closing and error-recovery rules, so unusual or deliberately malformed markup may not format exactly as a browser would interpret it.
What does Minify actually remove?
HTML comments are stripped entirely, and whitespace between tags is collapsed. Whitespace within visible text content is collapsed to single spaces but not removed, so the page still reads correctly.
Will minifying break my page?
It should not for typical markup, but because <pre>/<script>/<style> contents are passed through untouched, whitespace-sensitive content there is preserved. Always spot-check the output before deploying.
Does it handle self-closing tags like <br> and <img> correctly?
Yes, the standard HTML5 void elements (br, img, input, hr, meta, link, area, base, col, embed, param, source, track, wbr) are recognised and never treated as opening a nesting level.
Can I use this for production minification?
It is fine for quick, one-off shrinking of a snippet. For a production build pipeline, a maintained tool like html-minifier-terser will handle more edge cases and is the better choice.
Does formatting preserve my attribute quoting style?
Yes, attributes are preserved exactly as written, whether single-quoted, double-quoted, or unquoted.
Is my HTML sent to a server?
No, formatting and minifying both run entirely in your browser using JavaScript. Nothing is uploaded.
Why does deeply nested markup sometimes look odd after formatting?
Because this tool does not implement browsers' implied tag-closing behaviour (for example, automatically closing an open <p> before a new block element), markup relying on that behaviour may nest differently than a browser would render it. Explicitly closing your tags avoids this.
Does the formatter reformat inline SVG?
It tokenizes SVG using the same generic HTML tag rules, which works correctly for the vast majority of inline SVG, though it is not guaranteed to match the HTML5 spec's special "foreign content" handling in every edge case.
Part of These Collections
Also Available As
html formatter, html minifier, html beautifier, pretty print html, format html online, html indent, html whitespace remover
Found a Problem?
Let us know if something with HTML Formatter & Minifier isn't working as expected.
Something went wrong. Please try again.
Thanks — we'll take a look.