Free tool

HTML formatter and beautifier

Paste HTML and get it back indented and readable as you type. It runs Prettier, so inline <style> and <script> blocks are formatted too, and a broken tag is reported with its line and column. Switch to Minify for the smallest safe version of the same markup.

Free · no sign-up · runs in your browser, nothing is uploaded

HTML input
7 lines
Formatted HTML

Working…

Drop a .html, .htm, .xhtml file here, or choose one. It is read in your browser.

How the HTML formatter works

The formatter is Prettier 2, loaded into this page. It parses your HTML into a tree, then prints it again with one element per line where that is safe, child elements indented, and long tags wrapped at the line width you pick. Attribute values, text and the content of <pre> stay as you wrote them.

With Format inline CSS and JS on, the content of <style> and <script> is formatted by Prettier’s CSS and JavaScript printers, so one paste fixes the whole file. Turn it off to leave embedded code alone, for example when it holds template syntax.

Options: indent with 2 spaces, 4 spaces or tabs; line width 80, 100 or 120; one attribute per line (Prettier’s singleAttributePerLine); and the whitespace setting below. Turn on the rendered preview to see the result in a sandboxed frame with scripts disabled, so you can check that formatting did not change the page. Need JSON or SQL instead? Use the JSON formatter or the SQL formatter. Looking for a full editor setup? Browse IDE and editor tools on DevHunt.

Whitespace: the one setting that matters

In HTML, whitespace between inline elements is visible. <b>bold</b> <i>italic</i> shows a space between the two words, and <b>bold</b><i>italic</i> does not. Between block elements like <div> or <li>, browsers ignore it. A formatter that adds line breaks without knowing this can change how your page looks. Prettier’s htmlWhitespaceSensitivity option controls it:

SettingWhat it doesUse it for
Respect CSS display (default)Treats whitespace as significant only around elements whose default display is inline, like a, span, b, imgMost pages
StrictTreats whitespace around every tag as significant, so rendering never changesEmail templates and pages with display overrides
IgnoreTreats whitespace around every tag as unimportantTemplates and markup you own, when you want the tidiest output

Gotcha: the default decides by each element’s default display. If your CSS turns <li> into inline-block (a common horizontal menu), whitespace between items becomes visible gaps, and only Strict keeps it exactly as it was.

Format HTML from the command line or your editor

The same Prettier output you get here, in your project:

# format every HTML file in the project
npx prettier --write "**/*.html"

# check formatting in CI without changing files
npx prettier --check "**/*.html"

Put the options in a .prettierrc file so everyone gets the same result:

{
  "printWidth": 100,
  "tabWidth": 2,
  "htmlWhitespaceSensitivity": "css",
  "singleAttributePerLine": false
}

In Node, call the API directly. Prettier 3’s format returns a promise; Prettier 2’s returns a string.

import * as prettier from 'prettier';

const pretty = await prettier.format('<div><p>Hello</p></div>', { parser: 'html' });

In Python, BeautifulSoup’s soup.prettify() puts each tag on its own line. It does not know about inline whitespace, so check the result in a browser.

Minify HTML: what is removed and what is kept

Minify mode makes the markup smaller without changing what the page shows. It is careful by design: it only drops whitespace that the browser would ignore anyway.

ChangeExampleSafe?
Remove comments (keeps <!--[if IE]> conditionals)<!-- nav --> → (removed)Yes, unless a script reads comment nodes
Remove whitespace next to block tags</li>\n <li> → </li><li>Yes, unless your CSS makes those elements inline-block
Collapse runs of whitespace in text to one spaceHello␣␣␣<b>world</b> → Hello <b>world</b>Yes, the browser collapses them anyway
Tidy whitespace inside tags<div class="a" > → <div class="a">Yes, attribute values are kept as written
Minify inline <style> with CSSOa { color: #ff0000 } → a{color:red}Yes
Minify inline <script> with Terser; compact JSON-LDfunction add(first, second) → function add(d,n)Yes for classic and module scripts; other types are left alone
Leave <pre>, <textarea> and unknown script types as they are<pre> a\n b</pre>Kept byte for byte

The size cards show bytes before and after, and the gzip size of each. Most servers send HTML gzip or Brotli compressed, so the gzip number is closer to what users download. To shrink a stylesheet or a script on its own, use the CSS minifier or the JavaScript minifier.

FAQ

How do I format HTML in VS Code?+

Open the file and run Format Document: Shift+Alt+F on Windows, Shift+Option+F on macOS, Ctrl+Shift+I on Linux. VS Code has a built-in HTML formatter. If you use the Prettier extension, set it as the default formatter so the editor and this page give the same result.

What is the difference between formatting and minifying HTML?+

Formatting (beautifying) adds line breaks and indentation so people can read the markup. Minifying does the opposite: it removes comments and whitespace the browser does not need, so the file is smaller. Use formatted HTML in your source code and minified HTML in what you ship.

Will formatting change how my page looks?+

Not with the default "Respect CSS display" setting. Prettier only adds or removes whitespace where the element’s default CSS display makes it invisible, such as between block elements. "Strict" never touches whitespace around any tag. "Ignore" gives the tidiest output but can add or remove a visible space between inline elements.

Why does the formatter put the closing > on the next line?+

Between inline elements like <a>, <span> or <b>, a space is visible. To wrap a long line without adding a space that was not there, Prettier breaks inside the tag instead, which leaves a lone > on the next line. Switch Whitespace to "Ignore" if you know those spaces don’t matter.

Can this tool fix broken HTML?+

It tells you where it breaks. A closing tag that does not match, like </span> where </div> was expected, stops the formatter and shows the line, the column and the code around it. It does not guess a fix, because the right one depends on what you meant.

Is my HTML uploaded anywhere?+

No. Prettier runs in your browser. Short inputs are saved in the page address so you can share a result; long ones stay out of it, and nothing is sent to a server.

Sources: Prettier options · MDN: handling whitespace · VS Code: formatting · Terser · CSSO. Checked 2026-10-29.