HTML Formatter and Minifier

Paste HTML to indent nested tags into readable markup, or minify it by removing comments and collapsing whitespace. It is free and instant, and the formatter runs in your browser, so your pages are never uploaded.

HTML Formatter and MinifierPrivate · runs on your device
Input
Words 0Characters 0Lines 0
Formatted HTML

Example: <div><p>Hi</p></div> → <div> <p>Hi</p> </div>

How to use the HTML formatter

Paste anything from a snippet to a whole page. The output updates as you type.

  1. Paste your HTML into the Input box, or press Paste. Fragments without <html> or <body> work fine, and Try it loads a short example.
  2. Set Mode to Beautify for indented markup, or to Minify to shrink it.
  3. Choose the Indent: 2 spaces, 4 spaces or Tab. It only affects Beautify.
  4. Read the status line. It says "Beautified", or for Minify it shows the before and after length and the percentage saved.
  5. Press Copy or Download .txt. Use as input moves the result back into the input box.

Beautify uses the js-beautify library inside your browser, the same code that sits behind many editor plugins. Nothing is uploaded, so pages with embedded keys, customer data or unreleased content stay private.

What Beautify does to a page

Beautify rebuilds the indentation from the tag structure. The example <div><p>Hi</p></div> becomes three lines, with the paragraph indented one level inside the div. On a full page the children of html, head and body are all indented, blank lines between elements are removed and long lines are not wrapped.

  • Inline elements such as a, span, strong and code stay on the same line as the surrounding text, because a line break there would add a visible space.
  • The contents of pre are left exactly as written.
  • The code inside script and style blocks is tidied as well, with the same indent, so you do not need a separate pass for embedded JavaScript or CSS.
  • Attributes keep their order and quoting, and the doctype is kept.

The beautifier does not repair markup. If a tag is never closed, everything after it is pulled one level deeper, which is a quick way to spot the missing tag: look for the place where the indentation stops coming back.

What Minify removes, and what it can change

Minify strips comments, removes whitespace between tags, collapses runs of whitespace to one space and trims the ends of the document. A four-line list, <ul>, two <li> items with indentation and </ul>, drops from 40 characters to 33, and the status line reports the saving as 18 percent. It does not remove optional closing tags, rename classes or shorten attribute values.

Four details decide whether the output is safe for your page:

  • Conditional comments for old Internet Explorer, which begin with <!--[if, are kept. Ordinary comments are removed.
  • The contents of pre, textarea, script and style are copied through untouched, so embedded code is not minified.
  • Whitespace between two tags is removed even when both are inline. Two links separated by a space, such as <b>one</b> <i>two</i>, become joined, and the visible space disappears.
  • Runs of spaces inside attribute values collapse too, so class="a b" becomes class="a b".

Compression already handles repeated whitespace well, so the gain after gzip or Brotli is small. Minify matters most for email templates, embed codes and pages served without compression.

Typical jobs for an HTML formatter

HTML usually arrives in one of two states: a single line from a CMS, a bundler or view-source, or a file whose indentation drifted over the years. Beautify fixes both and makes nesting mistakes visible.

  • Clean up a template exported from an email or page builder before you edit it.
  • Format a component copied from browser dev tools so it fits your project.
  • Prepare markup for a code review, with the diff checker comparing two formatted versions.
  • Minify a snippet for an embed code or a srcdoc attribute.

Template syntax from PHP, Twig, Blade or Jinja is passed through but not understood, so check places where tags are generated conditionally. For strict XML-style markup such as XHTML or SVG, the XML formatter also checks that tags are well-formed. To shrink embedded styles or scripts, use the CSS formatter and the JavaScript formatter, then paste the result back. Markdown writers can export HTML from the Markdown editor and tidy it here.

Quick answers

Frequently asked questions

The questions people ask most, answered in plain words.

Does formatting HTML change how the page renders?

Beautify normally does not, because inline elements stay on the same line and pre blocks are untouched. Minify can, in small ways: it removes the space between two inline elements and collapses repeated spaces inside attribute values. Compare the page before and after if your layout relies on spacing between inline items.

Does Minify also shrink my script and style blocks?

No. Script, style, pre and textarea contents are copied through exactly as written, so any JavaScript or CSS inside them stays full size. To shrink embedded code, minify it separately with the JavaScript or CSS tool, or use a build tool, and paste the result back into the page.

Are conditional comments removed when I minify?

No. Comments that begin with the conditional form for old Internet Explorer are kept, because removing them would change behaviour in those browsers. Every other HTML comment is deleted, so keep the readable source in version control and minify only the copy that you publish.

Is minifying HTML worth it when gzip or Brotli is already on?

Usually only a little. Compression already squeezes repeated whitespace, so the extra saving after gzip or Brotli is small. Minifying helps most for email templates, embed codes and pages served without compression. The status line shows the exact saving before compression, which you can use to judge it.

Why does my HTML look wrong after I beautify it?

Almost always because the input has a nesting mistake, such as an unclosed div or a paragraph wrapped around a block element. The formatter follows the tags as written, so an unclosed element pulls everything after it one level deeper. Find where the indentation stops returning and fix the tag there.