Skip to main content

HTML Formatter / Beautifier

Format, beautify and minify HTML code.

Reviewed by · Last reviewed

How to Use the HTML Formatter

  1. Paste HTML into the input box - a full page, a minified template fragment, or the raw output of a server-rendered framework all work. The DOCTYPE, script, and style blocks are fine.
  2. Choose indentation: 2 spaces (common in React and Vue projects), 4 spaces (classic HTML5 style), or a tab character. The setting applies the next time you click Format.
  3. Click Format to indent every block element and put each tag on its own line, or Minify to collapse the document back into a compact single-line string.
  4. Copy the output with the clipboard button. The textarea keeps a scrollable copy so you can compare the before and after without leaving the page.

What the Tool Does Under the Hood

The formatter tokenises the input into three kinds of nodes: opening tags, closing tags, and text runs. When it sees an opening tag it increments an indent counter and emits a newline; a closing tag decrements the counter before it prints. Void elements listed in the HTML Living Standard - <br>, <img>, <input>, <meta>, <link>, <hr>, <source>, and friends - never push the stack, because they cannot have children. Self-closing XML-style tags ending in /> are treated the same way.

Content inside <pre>, <textarea>, <script>, and <style> is preserved verbatim because reformatting those elements would change whitespace that is observable to users or to the JavaScript engine. Comments (<!-- -->) and the DOCTYPE declaration are emitted on their own line. The minify pass collapses runs of whitespace between tags to nothing, removes comments, and joins the document into one line.

Real Reasons to Use This

  • Making sense of a server-rendered framework's output (Rails, Django, Laravel) when debugging an unexpected DOM structure.
  • Preparing an email template for a CMS that ships HTML inline and benefits from minified markup.
  • Normalising the output of a WYSIWYG editor before committing it to version control.
  • Inspecting a single Web Component's rendered shadow DOM after copying it from Chrome DevTools.
  • Generating a minimal reproduction for a bug report where indentation makes the structure obvious.
  • Converting a one-line HTML snippet from a CDN into something you can comfortably read on a laptop screen.

Pitfalls and Edge Cases

  • Tag soup. Legacy HTML lets you omit some closing tags (<li>, <p>, <td>). The formatter treats them as if they were nested, which can produce deeper indentation than the document's conceptual structure. The output still renders identically in the browser.
  • Inline-sensitive whitespace. Spaces between <span> or <a> tags affect the rendered layout. Aggressive minification can join "Hello link" into "Hellolink" visually. Use a non-breaking space or a leading space inside the anchor when this matters.
  • Conditional comments. Older Internet Explorer comments like <!--[if IE]> are preserved as plain comments; recent browsers ignore them regardless, so this is usually fine.
  • Embedded SVG and MathML. The tool indents them like any other markup, including the self-closing nodes that use XML syntax.
  • CDATA blocks. <![CDATA[ ... ]]> inside inline SVG survives the round-trip because the tokenizer treats it as opaque text.

HTML Specification Context

HTML is no longer a versioned spec - the current reference is the WHATWG HTML Living Standard, which defines the parser in algorithmic terms rather than as a grammar. That means the same input can be tokenised into different DOM trees depending on implicit-close rules for <table>, <ul>, or <dl>. Whitespace between block elements is collapsed by the browser during layout, so pretty-printing is purely a developer convenience: your rendered page looks identical whether it is indented with two spaces or crammed onto one line. The only places where whitespace is significant are inside <pre>, <textarea>, elements styled with white-space: pre, and the boundaries between inline formatting contexts.

When Other Tools Are Better

In an editor, Prettier's HTML plugin builds a real parse tree and handles edge cases like <a> nested inside block-level content, preserving tag omission rules, and word-wrapping long attribute lists. That is the right default for any project with a build step. Command-line tools like tidy or html-minifier-terser can additionally deduplicate attributes, collapse boolean attributes, or remove redundant quotes. This web tool is intentionally simpler - reach for it when you need to read a single-file minified page, beautify an email template, or minify a snippet for a transactional message without touching npm. For full-site optimisation, bake minification into your deploy pipeline.

Frequently Asked Questions

Does the formatter validate that my HTML is well formed?

No. Formatting and validation are orthogonal operations. This tool indents whatever you give it and does not verify that every opening tag has a matching close, that attribute values are quoted, or that you are using valid element names. If you need strict validation, paste the result into the W3C Markup Validator or enable the HTMLHint extension in your editor.

Why does the output leave <code>&lt;pre&gt;</code> and <code>&lt;textarea&gt;</code> content unchanged?

Both elements treat whitespace as part of their content. Indenting the inner text would introduce visible leading spaces when the browser rendered the page and would corrupt textarea default values. The HTML Living Standard specifically calls out these elements, plus <code>&lt;script&gt;</code> and <code>&lt;style&gt;</code>, as raw text content whose inner whitespace is significant.

Is the minifier safe to use on server-rendered templates?

Mostly yes, but be careful with templates that rely on significant whitespace between inline elements. Server templates often produce markup where a space between <code>&lt;span&gt;</code> tags matters for the rendered layout; the minifier collapses those spaces. If your template renders fine with or without whitespace, the minified output is a safe drop-in replacement.

What happens to attribute ordering and quote style?

Attributes are emitted in the same order they appeared in the source, and quote characters are preserved verbatim. Unlike Prettier, this formatter does not normalise single quotes to double quotes or sort attributes for deterministic diffs. If you need canonical output across a team, pre-process with Prettier first and use this tool only for ad hoc inspection.

Will my markup be sent to a server to process?

No. The formatter is implemented as a Preact component that runs inside your tab; the Format and Minify buttons call local JavaScript functions that never issue a fetch request. You can block outbound network traffic after the initial page load and every button still works. The input you paste is not logged anywhere.

How does the tool handle embedded SVG?

SVG elements are indented the same way as HTML elements. Self-closing SVG tags like <code>&lt;path d="..." /&gt;</code> are recognised and do not push the indent stack. XML namespaces (<code>xmlns:xlink</code>) and self-closing syntax survive the round-trip, so a formatted and then minified SVG produces the same rendered image.

Can I format a full HTML page with a DOCTYPE?

Yes. The DOCTYPE declaration (<code>&lt;!doctype html&gt;</code>) is emitted on its own line at the top of the output. The formatter does not add a DOCTYPE if your input lacks one, because doing so could change how the browser chooses between quirks mode and standards mode. Paste a full document and you get a full document back.

Why are some closing slashes in output like <code>&lt;br /&gt;</code> instead of <code>&lt;br&gt;</code>?

Whatever style your input used is preserved. HTML5 accepts both <code>&lt;br&gt;</code> and <code>&lt;br /&gt;</code> for void elements. If you paste XHTML-style self-closing tags you get the same back; if you paste HTML5 bare tags you also get those back. Consistency across a codebase is a linter concern, not a formatter one.

Does minifying remove <code>&lt;script&gt;</code> content?

No. The contents of a <code>&lt;script&gt;</code> block are preserved byte for byte because they are raw text - removing whitespace inside JavaScript would corrupt template literals, regexes, and ASI behaviour. Use a dedicated JavaScript minifier like Terser or esbuild for the script contents, then paste the result back into your HTML before minifying the surrounding page.

Can I use this for email HTML?

Yes, with one caveat: many email clients strip or modify certain tags, ignore external stylesheets, and require inline styles. The formatter will happily beautify or minify MJML-compiled output, but the client-specific hacks that make email render consistently (Outlook conditional comments, VML fallbacks) will still be preserved in whatever form you paste them in. Run a tool like Litmus or Email on Acid afterwards to confirm rendering.

More Developer Tools