Skip to content

HTML Formatter & Beautifier

HTML Formatter & Beautifier

Paste minified or messy HTML and get it back with one tag per line and nested indentation, so you can read its structure. The reverse direction, minifying to one line, is one toggle away. Works on full documents and fragments; template tags such as Handlebars {{…}} are kept as text lines.

Why HTML Formatting Matters

Browsers don't care about whitespace inside HTML, but humans do. A minified bundle pulled out of a CDN, a server-rendered fragment from a framework, or a chunk of inherited code copy-pasted from a CMS will routinely arrive as one long line with no breaks. Reading that requires either an editor with format-on-paste or a tool that fixes it in place. Formatting also makes diffs sane — see the text diff tool for what an unformatted vs formatted diff looks like.

Beyond readability, consistent formatting catches real bugs. Mismatched tags, accidentally-nested forms, and unclosed elements all surface visually once the indentation is correct. The formatter doesn't fix invalid HTML, but it surfaces it: indentation gets weird in exactly the spot where the parser had to guess.

How It Works

The HTML is split into tags, text, comments, doctype and CDATA. Every tag goes on its own line, inline tags such as <a> and <strong> included, and the content of each element that is not a void element is indented one level deeper. Text is trimmed. The content of <script>, <style>, <pre>, <code> and <textarea> is kept as a block but each of its lines is trimmed and re-indented.

Minify removes all comments, removes whitespace between tags and collapses every remaining run of whitespace to one space, including inside attribute values, <pre> and scripts. The worked example below shows where both modes change what the page displays.

Common Use Cases

Cleaning up HTML pulled from a view-source: or a network-tab response. Preparing a static fragment to paste into a documentation page or blog post. Generating a minified version of an email template before sending. Inspecting the output of a server-side renderer to verify it matches what you expected. Pair with the JSON formatter when working with API responses that contain embedded HTML strings.

Worked example: does the text survive formatting?

A formatter must not change what a page says. Beautify now uses js-beautify 1.15.1 (the library behind the Code Beautifier, self-hosted), and Minify works on the parsed document instead of on raw text. On October 13, 2026 we took 39 guide articles from this site, ran each through both modes in Chromium 141, rendered the results and compared their visible text with the original: identical in 39 of 39 (beautify) and 39 of 39 (minify), compared exactly, character for character. Before this change the beautifier altered the visible text of all 39.

CaseBefore (v214)Now
Beautify: <p><b>bold</b> <i>italic</i></p>every inline tag on its own line: the space between the words could disappearinline tags stay on their text line (js-beautify's inline list)
Minify: <pre> line 1 line 2</pre>whitespace inside pre collapseduntouched
Minify: script with // note then a new lineline break collapsed, so the comment swallowed the next statementscript contents untouched
Minify: title="a b"spaces inside the attribute value collapsedattribute values never rewritten
Minify: commentsalways removed, including IE conditional commentsremoved unless you tick Keep comments; conditional comments always kept
Minify: whitespace between <p> blocksremoved (correct)removed; whitespace between inline elements becomes one space instead of vanishing

Sources

Checked on .

Related tools

Frequently Asked Questions

Does this tool fix broken HTML?+
Beautify does not: it indents what you give it. Minify rebuilds the markup from the browser's parsed document, so unclosed tags come out closed the way browsers close them; check the result if your source was malformed, because the browser's repair may not be what you intended.
What is the difference between beautify and minify?+
Beautify indents nested elements so the structure is easy to read, keeping inline elements such as b, a and span on their text line and leaving pre and textarea content as written. Minify removes whitespace between block elements and collapses other runs of whitespace to one space, without touching pre, textarea, script, style or code content or attribute values; comments are removed unless you tick Keep comments.
Does this tool upload my code?+
No. All formatting happens in your browser. Your code never leaves your device.
What indent size should I use?+
2 spaces is the most common convention for HTML. 4 spaces is popular in some teams and languages. Tab characters are preferred by some developers for accessibility reasons. Choose what your team uses.
Can I format XML with this tool?+
Yes. Since XML follows similar tag-based syntax to HTML, this formatter works well for XML documents too.
Does formatting change how HTML renders?+
It should not, and we test it: on 39 guide articles from this site the visible text after beautify and after minify was identical to the original in 39 of 39 and 39 of 39 cases, compared exactly in Chromium. The exception you control is CSS: text styled with white-space: pre shows spaces and line breaks, so leave such markup unminified.
Should I minify HTML for production?+
It usually matters little. On the article bodies of 39 of this site's guides, minify saved 0.1% of the bytes because the markup was already compact; server compression (gzip or Brotli) saves far more. Hand-indented templates gain more. The minifier here leaves pre, textarea, script, style and code content and attribute values untouched.
Does this tool work offline?+
Yes. Once the page loads, formatting runs entirely in your browser using built-in parsing logic. There is no network call when you click Format, so the tool works offline after the initial page load and is safe for sensitive markup you would rather not paste into a hosted service.

📖 Learn More

Related Article HTML Formatting Best Practices for Clean, Maintainable Code →

Built by Derek Giordano · Part of Ultimate Design Tools

Privacy Policy · Terms of Service