HTML Formatter

Beautify messy HTML with clean indentation, or minify it to one compact line. Runs entirely in your browser.

Indent:
479 B
Input
593 B
Output
+23.8%
Size change
24
Lines

What the formatter does

HTML copied from a CMS, an email builder or a minified production page usually arrives as one long line or with indentation that no longer matches the structure. This tool rewrites it so each block element sits on its own line, indented by its depth, while text and inline tags stay together on readable lines.

It works the other way too. Minify removes comments and the whitespace between block elements, which is useful for email templates, embedded snippets and anywhere a few hundred bytes matter. Both modes leave your content and attributes untouched: only whitespace and comments change.

Key takeaways

  • Beautify puts block elements on their own lines and keeps inline elements with their text.
  • Minify removes comments and the whitespace that browsers do not render, but keeps a single space where it matters.
  • <pre>, <textarea>, <script> and <style> contents are never changed.
  • The formatter never adds closing tags you did not write; it warns about stray and unclosed tags instead.
  • Your HTML is treated as text only. It is not rendered, run or uploaded.

A worked example

Here is a compressed fragment of the kind you might copy from a page source. It has a list with optional closing tags left out, a line break and a preformatted block:

<div class="card"><h2>Plans</h2><p>Pick a plan. <a href="/pricing">See <b>all</b> prices</a></p><ul><li>Basic<li>Pro</ul><br><pre> keep
    spacing</pre></div>

Beautified with a 2-space indent, the tool gives:

<div class="card">
  <h2>Plans</h2>
  <p>Pick a plan. <a href="/pricing">See <b>all</b> prices</a></p>
  <ul>
    <li>Basic
    <li>Pro
  </ul>
  <br>
  <pre> keep
    spacing</pre>
</div>

Three details are worth noticing. The run of spaces after "Pick a plan." collapsed to one, because browsers render any run of whitespace as a single space anyway. The link and the bold text stayed on the paragraph's line, because splitting inline elements across lines can add visible spaces. And the <pre> block kept its own spacing, because inside <pre> whitespace is the content.

The <li> elements had no closing tags in the input, which HTML allows. The formatter recognizes that a new <li> ends the previous one and indents them as siblings, but it does not invent </li> tags that were not there.

How whitespace and inline elements are handled

The hard part of formatting HTML is that whitespace is sometimes meaningful. Between two block elements, such as a </p> and the next <p>, line breaks and indentation make no visible difference. Between two inline elements they do: a space between two links shows on the page, and no space means the words run together.

Minifying shows the rule clearly. These two paragraphs render differently, and the minifier keeps the difference:

InputMinified output
<p> (newline) <b>one</b> (newline) <i>two</i> (newline) </p><p><b>one</b> <i>two</i></p>
<p><b>one</b><i>two</i></p><p><b>one</b><i>two</i></p>

The first paragraph shows "one two"; the second shows "onetwo". The line breaks in the first are reduced to one space rather than removed.

The formatter treats these elements as inline: a, abbr, b, bdi, bdo, br, button, cite, code, data, del, dfn, em, i, img, input, ins, kbd, label, mark, meter, output, progress, q, s, samp, select, small, span, strong, sub, sup, textarea, time, u, var and wbr, plus a few obsolete ones such as font and tt. Everything else, including div, p, the headings, lists, tables, forms and the HTML5 sectioning elements, is laid out as a block. An inline element that contains a block, such as a link wrapped around a whole card, is laid out as a block too.

What is kept exactly as written

  • <pre> and <textarea>: whitespace inside them is displayed, so re-indenting would change the page.
  • <script> and <style>: their contents are JavaScript and CSS, not HTML. A <div> inside a script string is just text, and the formatter looks only for the matching </script> or </style>. To tidy the code itself, use a dedicated CSS or JavaScript tool.
  • Attributes: values are untouched, including a > inside quotes such as title="a > b". Extra spaces and line breaks between attributes are reduced to one space.
  • Comments, the doctype and CDATA sections: kept verbatim when beautifying. Minify removes ordinary comments but keeps old Internet Explorer conditional comments, which still change behavior in some email clients.
  • Tag and attribute case: an uppercase <DIV> stays uppercase. Only whitespace changes.

How much minifying saves

On hand-written, indented HTML the saving comes mostly from indentation and newlines. A small 14-line page with a comment, indented two spaces per level, goes from 264 bytes to 188 bytes when minified, a 28.8% reduction. Pages with deep nesting, generous indentation or many comments save more; pages that are mostly text save less.

If your server already sends pages with gzip or Brotli compression, minifying HTML usually saves far less on the wire than these raw figures suggest, because repeated whitespace compresses very well. It still helps for HTML embedded in JavaScript, email templates and anywhere compression is not applied.

Stray and unclosed tags

Browsers are forgiving: they repair broken markup silently, which is why mistakes survive for years. The formatter is deliberately not a repair tool. If it meets a closing tag with no matching opening tag, it leaves it where it is and lists it. If an element is never closed, it reports that too, instead of guessing where the closing tag belongs. For <div><span>Note</div></p> it reports the stray </p> and the unclosed <span>.

Elements whose closing tags HTML makes optional, such as li, p, td, tr, dt, dd and option, are not reported when they are left open, because that is valid HTML.

Everything runs in your browser. The formatter reads your HTML as plain text. It never renders it, runs its scripts or loads its images, and nothing is uploaded.

Frequently Asked Questions

Does formatting HTML change how the page looks?

Almost never. The formatter only changes whitespace between tags and collapses runs of spaces in text, which browsers already display as one space. It keeps inline elements on the same line so no visible spaces appear or disappear, and it leaves pre, textarea, script and style contents untouched. The one case to check is CSS that uses white-space: pre on an ordinary element, since the browser would then show the new line breaks.

Why are some tags kept on one line?

Inline elements such as a, b, span and code sit on the same line as their surrounding text. Putting them on separate lines can add a visible space between words, so they stay together. Block elements such as div, p and ul get their own lines.

Will it fix broken HTML?

No. It reports closing tags that have no matching opening tag and elements that are never closed, but it does not add or remove tags. Guessing where a missing tag belongs can make things worse, so the fix is left to you.

Does it format the JavaScript and CSS inside the page?

No. Script and style contents are kept exactly as written, so code that relies on its own formatting is never broken. Use a dedicated JavaScript or CSS tool for those.

Should I use 2 spaces, 4 spaces or tabs?

Use whatever your project already uses. Two spaces is common for HTML because markup nests deeply and lines get long quickly. Tabs let each person set their own display width. Consistency matters more than the choice.

What does minifying remove?

Comments (except old conditional comments), whitespace between block elements, whitespace at the start and end of block elements, and extra spaces in text and inside tags. A single space between inline elements is kept because it is visible.

Is minified HTML bad for SEO?

No. Search engines read the same content and links either way. Minifying makes the file smaller, which can help load time slightly, but it has no direct effect on rankings.

Is my HTML uploaded anywhere?

No. Formatting happens in your browser. The HTML is treated as text: it is not rendered, its scripts are not run and its images are not loaded.

Why does a link with a div inside it get its own lines?

An inline element that contains block elements, such as a link wrapped around a whole card, behaves like a block in the layout, so the formatter indents it and its children as a block.

Related Calculators