Developer Tools

HTML Formatter

HTML formatter that puts every tag on its own indented line, or minifies markup by removing comments and whitespace, with a size comparison.

Input HTML
Output

    

This HTML formatter rewrites minified or single-line HTML so that every tag and every run of text sits on its own line, indented by nesting depth. The Minify button does the reverse and strips comments and the whitespace around tags. Both actions run as JavaScript in your browser, and each run reports the input and output size in characters and bytes.

How to use the HTML formatter

  1. Paste your markup into the Input HTML box, or press Try Example to load a sample page with a doctype, inline CSS, a script and a comment.
  2. Pick 2 spaces, 4 spaces or Tab from the Indent menu. 2 spaces is selected when the page loads.
  3. Click Beautify to format HTML, or press Ctrl+Enter (Cmd+Enter on a Mac) in the input box. Click Minify instead to shrink the markup.
  4. Read the size panel, which compares the input and output in bytes, then press Copy above the Output box.

Example: beautifying a one-line navigation list

With the indent set to 2 spaces, this input:

<ul class="nav"><li><a href="/">Home</a></li><li><a href="/pricing">Pricing</a></li></ul>

becomes:

<ul class="nav">
  <li>
    <a href="/">
      Home
    </a>
  </li>
  <li>
    <a href="/pricing">
      Pricing
    </a>
  </li>
</ul>

Home and Pricing sit one level below their links because text gets a line of its own as well. The size panel for this run shows 89 B before and 136 B after (+52.8%), since indentation adds bytes.

Beautify or minify

BeautifyMinify
ResultOne tag or text run per line, indentedWhitespace between and around tags removed
Indent menuUsedIgnored
CommentsKept at the current depthRemoved, except ones starting with <!--[if
script, style, pre, textareaContents not re-indentedKept exactly as written
SizeGrowsShrinks

The Minify button is a simple HTML minifier built on regular expressions. Given this card:

<!-- promo card -->
<div class="card">
    <h2>Spring sale</h2>
    <p>Save 20% this week.</p>
</div>

it returns <div class="card"><h2>Spring sale</h2><p>Save 20% this week.</p></div> and reports Saved 31 B (30.7% smaller).

Where it helps

Beautify HTML that arrives on one line, such as a template exported by a CMS or email builder, or a snippet copied from a browser's page source. One tag per line makes nesting levels easy to count. Because it is an HTML beautifier online, there is nothing to install, which helps on a borrowed machine or in an editor with no formatter set up. If you write in Markdown, the Markdown preview gives you the HTML to format here.

Minify suits a finished static page or HTML email fragment that only needs its comments and indentation removed. Read the output before you publish it, because of the word-joining problem described under the limits below.

Limits of this HTML formatter

  • Beautify matches tags with a regular expression and counts opening and closing tags. It does not validate or repair markup, so a missing closing tag, including optional ones such as </li> and </p>, pushes every following line one level deeper.
  • Inline elements are split onto separate lines the same way as blocks. A line break renders as a space, so <strong>Hi</strong>! shows a gap before the exclamation mark once beautified.
  • Beautify does not re-indent the contents of script, style, pre and textarea. Inside a pre block it still puts nested tags and text on separate lines and trims the first line's leading spaces, which can change what the block displays.
  • Minify deletes whitespace on both sides of every tag, which can join words: <p>Read the <a href="/docs">docs</a> first.</p> comes out as <p>Read the<a href="/docs">docs</a>first.</p>.
  • Minify leaves script and style blocks exactly as written. To shrink that code as well, run it through the CSS formatter or the JavaScript formatter.

The same paste-and-format approach for data is covered in how to format JSON in your browser, and the other formatters are listed under developer tools.

Frequently Asked Questions

Does the HTML formatter support tabs instead of spaces?
Yes. The Indent menu above the editor offers 2 spaces, 4 spaces or a tab, and 2 spaces is selected when the page loads. Pick the option before clicking Beautify, because changing the menu afterwards does not reformat output that is already showing. Minify ignores the setting, since its job is to strip indentation out.
How is the size saved by minifying calculated?
Both figures are UTF-8 byte counts, the size the text would have if saved as a UTF-8 file, and the character counts appear under the input box. Accented letters and emoji take two to four bytes each, so bytes and characters can differ. A smaller result is reported as Saved with a percentage, while a larger one, normal after beautifying, shows a plus sign.
Does the HTML formatter handle a full page with a doctype?
Yes. The DOCTYPE declaration stays on its own line exactly as written, and html, head and body are indented like any other element. Void elements such as img, br, meta, link and input do not open a new indent level, and neither does a tag closed with a trailing slash. Comments and CDATA sections are kept intact at the current depth.
How is this different from formatting HTML in VS Code?
VS Code's built-in Format Document command (Shift+Alt+F on Windows, Shift+Option+F on a Mac) keeps inline elements such as a and strong on the same line as the surrounding text and wraps long lines. This page puts every tag on its own line, which is easier for counting nesting but changes spacing around inline elements.
Is my HTML sent to a server?
No. The HTML formatter's Beautify and Minify buttons run JavaScript functions on the page, and the result is written straight into the Output box. Nothing you paste is uploaded, so you can format templates that contain internal links, tracking code or unpublished copy.