HTML Formatter — Beautify Messy or Minified HTML with Prettier

Paste minified or messy HTML to get clean, consistently indented markup, with any inline style and script blocks formatted too, so the structure of the page is easy to read.

Live demo · real result from this tool
Indent

Formatted HTML

<div class="card">  <h2>Order #1042</h2>  <p>Status: <strong>shipped</strong></p>  <ul>    <li>Desk lamp</li>    <li>Notebook ×2</li>  </ul>  <pre>  keep   this</pre></div> 

Processed on your device — nothing is uploaded.

How to use the HTML formatter

  1. Paste your HTML into the HTML box. A full page, a fragment or a single element all work.
  2. Choose the Indent: 2 spaces, 4 spaces or Tab.
  3. Pick a Line width of 80, 100 or 120 characters. Longer lines are wrapped where it is safe.
  4. Choose how to treat Inline whitespace: Respect CSS display (safe), Strict: never change inline spacing, or Ignore: tidiest, may change spacing.
  5. Copy the Formatted HTML or download it as formatted.html.

What it does and when to use it

HTML is often hard to read by the time you need to look at it. It may be minified onto one line, exported from a page builder, pasted from an email template, or edited by several people with different habits. Formatting puts every element on its own line with consistent indentation, so the structure of the page becomes visible at a glance.

Use it to:

  • Read and debug the HTML of an email template or a CMS export.
  • Find nesting mistakes, such as an unclosed div, by following the indentation.
  • Tidy a snippet before you paste it into documentation or a code review.
  • Make two versions of a file comparable before you diff them in the Diff Checker.

When you are ready to ship, go the other way with the HTML Minifier. To strip unwanted inline styles and attributes first, use the HTML Cleaner. To check the markup for errors, try the HTML Validator.

How it works

The formatter uses Prettier, a widely used open-source code formatter, loaded in your browser with its HTML, CSS and JavaScript plugins. Prettier parses your HTML into a tree, then prints it again with your chosen indent and line width. Because it works from a parsed tree rather than by search-and-replace, it handles attributes, comments and nested elements reliably.

The whitespace setting matters because HTML spacing is not always invisible. Between inline elements, such as a link next to some bold text, a line break shows up as a space. “Respect CSS display” follows each element’s default display type and only adds line breaks where they are safe. “Strict” never touches spacing around inline content. “Ignore” formats freely.

Worked examples

A minified list. <div><p>Hi</p><ul><li>a</li><li>b</li></ul></div> becomes a tree with each element on its own line: the paragraph and list indented two spaces inside the div, and each list item indented two more.

Four-space indent. A card with a heading and a paragraph, formatted with 4 spaces, places <h2>Order #1042</h2> four spaces in. The inline <strong> stays on the same line as its text, so no extra space is added.

Inline CSS. <style>body{margin:0;color:red}</style> is expanded into a readable rule, with margin: 0; and color: red; each on their own indented line.

Preformatted text. A <pre> block containing “ a” and “ b” keeps its exact spaces and line breaks.

Limits and tips

  • Badly broken HTML is formatted the way a parser reads it, which may not be what you meant. Fix errors the validator reports first.
  • Template syntax such as Handlebars or Jinja tags can confuse the HTML parser. Format the final HTML where possible.
  • Very large documents can take a moment, because formatting runs on your device.

Frequently asked questions

Will formatting change how my page looks?
With the default "Respect CSS display" setting, the formatter only adds line breaks where the browser would ignore them, so the page should look the same. The "Ignore" setting gives the tidiest code but can add or remove spaces between inline elements, which may shift text slightly. Always check the rendered page after a big change.
Does it format inline CSS and JavaScript?
Yes. Code inside style tags is formatted as CSS and code inside script tags as JavaScript, using the same Prettier engine. For whole CSS or JS files, use the CSS Formatter or JavaScript Formatter.
What happens to pre and textarea content?
It is left exactly as written, because spaces and line breaks inside those elements are shown on the page. Only the tags around it are moved.
How do I find an unclosed tag?
Format the document and follow the indentation. An unclosed element makes everything after it sit one level deeper than expected, which points you to the spot. For a precise list of errors, run the HTML Validator as well.
Is my HTML uploaded?
No. Prettier runs inside your browser, so templates, emails and pages with private content stay on your device.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close