HTML Beautifier

Re-indent minified or messy HTML with void tags, inline text, pre blocks, scripts and comments kept intact. Runs in your browser, nothing is uploaded.

The interactive tool loads here when JavaScript runs.

About this tool

What is the HTML Beautifier?

The HTML Beautifier takes markup that arrives on one line, or with indentation that drifted over years of edits, and prints it back with one block element per line and two spaces per nesting level. Paste the HTML, click Format HTML, and copy or download the result as formatted.html.

It is careful about the parts of HTML where whitespace changes what the browser shows. Inline content such as text, links, <b>, <code> and <span> stays on one line, so no space appears between a word and the bold tag that follows it. The contents of <pre> and <textarea> are copied byte for byte, and <script> and <style> bodies are only re-indented as a block, never split at a < character.

It is a formatter, not a validator. Unclosed tags and stray end tags are tolerated and printed where they appear rather than reported, and attribute values are kept exactly as written. There is no line-length wrapping, so a long paragraph stays one long line.

  • Knows HTML void elements and implied end tags
  • Keeps inline text and tags on one line so rendering does not change
  • Copies <pre>, <textarea>, comments and DOCTYPE verbatim
  • Copy to clipboard or download as formatted.html
  • Runs in your browser; markup is not uploaded
Read the guide: Formatting HTML, XML and YAML Without Changing What They Mean

Updated

How to use the HTML Beautifier

  1. Paste your markup into the Input HTML box. A full document with <!DOCTYPE html> and a fragment such as a single <div> both work.
  2. Click Format HTML. The result appears in the Formatted HTML panel with two-space indentation.
  3. Check that <pre> blocks, inline scripts and comments came through as you expect.
  4. Use the copy button to put the result on your clipboard, or the download button to save it as formatted.html.
  5. On a desktop screen, click Fullscreen for side-by-side panels with more room.

Worked example

Nav with implied end tags and inline text

Input

<nav><ul><li><a href="/">Home</a><li><a href="/docs">Docs</a></ul></nav><p>Read the <code>README</code> first.<br>Then run it.</p>

Output

<nav>
  <ul>
    <li><a href="/">Home</a>
    <li><a href="/docs">Docs</a>
  </ul>
</nav>
<p>Read the <code>README</code> first.<br>Then run it.</p>

The second <li> closes the first, so both items sit at the same level and no </li> is added. The paragraph stays on one line because it only contains inline content, and the <br> does not push anything after it to the right.

Background and insight

HTML is not XML, and that is where most naive beautifiers go wrong. The HTML Living Standard defines void elements (area, base, br, col, embed, hr, img, input, link, meta, source, track and wbr) that never have an end tag. A formatter that treats every start tag as "indent one more level" drifts to the right after each <img> or <meta> and never comes back. This tool knows the list and does not indent after them, whether they are written as <br> or <br />.

The spec also lets you omit some end tags. A list written as <li>One<li>Two is valid, because a new <li> implicitly closes the previous one, and the same rule applies to <p>, <td>, <tr>, <dt>, <dd> and <option>. The parser here applies those rules so the following items line up as siblings, and it prints the markup without inventing end tags you did not write.

Whitespace is the other trap. Browsers collapse runs of spaces and newlines in normal text to one space, but they do not ignore whitespace between inline elements entirely. Breaking <b>big</b>world onto two lines would render as "big world". Keeping inline runs on a single line is the simplest rule that guarantees the formatted page renders exactly like the original.

Common use cases

  • Reading minified templates

    Expand server-rendered or minified HTML from view-source to see how the page is actually nested.

  • Cleaning up email HTML

    Make table-heavy email templates readable before editing, without risking extra gaps between inline elements.

  • Reviewing CMS output

    Format markup exported from a CMS or page builder so a diff against the previous version is readable.

  • Preparing code samples

    Tidy a snippet before pasting it into documentation, a ticket or a pull request description.

Tips and limitations

  • Format both versions of a page before diffing them. Comparing two formatted files in a diff tool shows structural changes instead of one giant changed line.
  • Attribute values are left exactly as written, including their quotes. The only change to a start tag is that whitespace between attributes collapses to a single space.
  • Content of <pre> and <textarea> is never touched, so if it looks misaligned in the output, it was misaligned in the source.
  • JavaScript inside <script> is re-indented as a block, not reformatted. A multi-line template literal inside a script can lose its leading spaces, so check those by eye.
  • Malformed markup is not reported. If the nesting in the output looks wrong, the cause is usually an unclosed <div> earlier in the source.

Sources and further reading

  1. HTML Living Standard: The HTML syntax, WHATWG. Void elements and the rules for optional end tags such as li, p, td and option
  2. Void element, MDN Web Docs. The full list of HTML elements that never take an end tag
  3. Handling whitespace, MDN Web Docs. How browsers collapse whitespace between inline elements

Frequently asked questions

Will beautifying my HTML change how the page looks?

It should not. Text and inline elements stay on one line, so no whitespace is added between them, and <pre> and <textarea> content is copied unchanged. The only whitespace that moves is between block-level elements, where browsers ignore it.

Why does a formatter indent everything after an img tag?

Formatters that treat HTML like XML expect every start tag to have an end tag. <img>, <br>, <meta> and the other void elements never do, so the indentation keeps growing. This tool recognises void elements and leaves the level unchanged after them.

Does the HTML Beautifier fix unclosed tags?

No. It prints the markup it was given and does not invent end tags, apart from honouring implied ones such as a new <li> closing the previous <li>. Use the nesting in the output to spot where a tag was left open.

Can I minify HTML with this tool?

Not here. This page only expands markup. For production, minification is usually done by your build step, and HTTP compression with gzip or Brotli already removes most of the cost of whitespace.

Does it format the CSS and JavaScript inside the page?

Only the indentation. Inline <style> and <script> blocks are moved to their nesting level as a whole, but their statements are not reformatted. Run them through a dedicated CSS or JavaScript formatter if you need that.

Is the HTML I paste sent anywhere?

No. Parsing and formatting run in JavaScript in your browser tab, so private templates and internal markup stay on your machine.

Is there an in-depth guide to the HTML Beautifier?

Yes. “Formatting HTML, XML and YAML Without Changing What They Mean” covers the background, how it works, common pitfalls and worked examples. Read the full guide.