ToolBook
Support us on Ko-fi
Help us keep this free, forever

HTML Formatter / Minifier

How to format or minify HTML online

Beautify HTML markup for readability or compress it for production with this free online HTML formatter.

  1. Paste your HTML

    Paste any HTML document, snippet, or component into the input area on the left.

  2. Choose an indent or minify mode

    Select 2 spaces, 4 spaces, or Tab to beautify. Choose Minify to produce a compact single-line version for production.

  3. Review the output and byte-size stats

    The formatted or minified HTML appears on the right alongside original size, output size, and savings percentage.

  4. Copy or download the result

    Click Copy to clipboard for quick use, or Download to save the result as a .html file.

Frequently asked questions

What is HTML formatting used for?

HTML formatters help developers read and debug markup. Formatting with indentation is useful when inspecting or editing HTML from a CMS, framework output, or third-party tool that produces a single-line blob. Minifying removes all unnecessary whitespace to reduce file size for production delivery.

Does formatting happen in my browser?

Yes. All formatting and minification runs locally in your browser using JavaScript. Your HTML is never sent to a server. There is no upload step and nothing is stored. The tool works offline once the page has loaded.

How does minifying HTML improve page speed and SEO?

Smaller HTML files transfer faster over the network, which lowers time-to-first-byte and overall page load time. Search engines factor page speed into rankings, so minifying HTML is a lightweight SEO improvement. The effect is amplified when the server also applies gzip or Brotli compression.

Does the formatter also format CSS and JavaScript inside HTML?

No. The HTML Formatter handles HTML structure only. CSS inside style tags and JavaScript inside script tags are preserved exactly as they appear in the input. Use a dedicated CSS formatter or JavaScript formatter for those blocks.

Does the minifier preserve script and style blocks?

The minifier collapses whitespace and removes HTML comments, but leaves script and style block content intact. For production builds, use a dedicated JavaScript minifier (like Terser) or CSS minifier to compress those blocks further.

What is the difference between HTML and XHTML?

HTML5 has a forgiving parser: void elements do not need closing slashes, tags are case-insensitive, and some end tags are optional. XHTML is HTML written as valid XML: all tags must be closed, attributes must be quoted, and tag names must be lowercase. This tool formats both, but does not validate against an XHTML DTD.

Why does formatting change my whitespace inside pre and code tags?

The formatter preserves whitespace inside pre, code, textarea, script, and style tags, since whitespace is significant in those contexts. All other elements collapse their text content whitespace to a single space.

Can I format large HTML files with this tool?

Yes. The tool runs entirely in your browser and handles files up to a few megabytes without issues on modern devices. There is no server-side size limit. For very large files processing may take a moment, but most HTML documents are well within range.

Can I use this to minify HTML for production?

Yes. The minifier strips HTML comments and collapses whitespace between tags. For maximum compression on a production site, combine this with HTTP gzip or Brotli compression. The two techniques together can reduce transmission size by 60 to 80 percent compared to unformatted HTML.

HTML formatting and minification: a practical guide

Readable during development, compact in production. This guide covers how to get both from the same source.

Why format HTML?

Raw HTML from a CMS, a scraper, a template engine, or a minified production bundle can be one long line that's impossible to read or debug. Formatting it gives you the indented tree structure your eyes expect, making it easy to see nesting errors, find specific elements, and understand the document structure.

Minification does the opposite: it removes all formatting (whitespace, newlines, and comments) to produce the smallest possible file. For production serving, a minified HTML file combined with gzip compression can reduce transfer size by 60–80%.

Void elements: no closing tag

HTML has a set of void elements that never have content and therefore never need a closing tag: <br>, <hr>, <img>, <input>, <link>, <meta>, <area>, <base>, <col>, <embed>, <param>, <source>, <track>, and <wbr>. The formatter knows this list and does not increase the indent depth after these tags.

In XHTML (which follows XML rules), void elements must be self-closed: <br />. In HTML5, the slash is optional and ignored. The formatter preserves whatever form the input uses.

Preserve tags: pre, code, script, style, textarea

The formatter does not reformat content inside <pre>, <code>, <textarea>, <script>, or <style> elements. Whitespace inside these elements is significant:

  • <pre> and <code> display whitespace literally. Reformatting would visibly change the rendered output.
  • <script> contains JavaScript where indentation in string literals matters.
  • <style> contains CSS where the formatter shouldn't interfere.
  • <textarea> shows its content as user-editable text; reformatting would add unwanted leading/trailing whitespace.

What minification removes

HTML minification strips:

  • All standard comments (<!-- ... -->). IE conditional comments (<!--[if IE]...) are preserved.
  • Leading and trailing whitespace in text nodes.
  • Consecutive whitespace sequences collapsed to a single space.
  • Whitespace between tags (> <><).

What minification does not do (to stay safe):

  • Remove attribute quotes (some parsers require them).
  • Collapse boolean attributes (disabled="disabled" is left as-is).
  • Remove optional closing tags (</li>, </p>), which would require a full HTML5 parser.

HTML formatting vs. a real HTML parser

The formatter uses a regex tokenizer and is deliberately simple. It handles the vast majority of real-world HTML correctly, but it is not a full HTML5 parser. Edge cases include:

  • Deeply nested structures where indentation gets out of sync due to optional closing tags.
  • <svg> and <math> elements (which have their own namespace rules).
  • Template syntax like {{mustache}} or {% block %} inside attributes.

For production-grade formatting, tools like Prettier with the HTML plugin use a full parse tree and produce byte-for-byte identical-semantics output. For inspection, debugging, and quick cleanup, the formatter here handles the common case well.

HTTP compression amplifies minification

A minified 20 KB HTML file will compress to roughly 5–6 KB with gzip or Brotli. An equivalent formatted 30 KB file compresses to 6–7 KB. The absolute savings from minification are amplified by HTTP compression, but minification reduces baseline transfer size for clients without compression support (rare) and reduces memory usage during HTML parsing on the client.