
What this HTML formatter does
HTML copied from a page source, a CMS export or a build tool often arrives as one long line or with drifting indentation, which hides where each tag closes. This HTML formatter rewrites the markup with one block element per line and consistent indentation, so the structure is visible at a glance. It can also go the other way and minify HTML, stripping comments and spare whitespace for a smaller file. Everything runs in your browser, so the code you paste is not uploaded anywhere.
How to use it
-
Paste your HTML into the Your HTML box. Click Sample if you just want to see what the tool does.
-
Pick an indent: 2 spaces, 4 spaces or Tab.
-
Click Beautify to get readable, indented markup, or Minify to get the most compact version.
-
Check the status line, which shows the line count after beautifying or the character savings after minifying.
-
Click Copy and paste the result back into your editor, template or site builder.
Worked example
Here is a small card squeezed onto one line, with three spaces inside the heading text:
<div class="card"><h2>Opening hours</h2><p>Mon to Fri<br>9am to 5pm</p><img src="map.png" alt="Map"><ul><li>Free parking</li><li>Step-free <em>access</em></li></ul></div>
With 2-space indentation, Beautify returns this:
<div class="card">
<h2>Opening hours</h2>
<p>
Mon to Fri<br>
9am to 5pm
</p>
<img src="map.png" alt="Map">
<ul>
<li>Free parking</li>
<li>Step-free <em>access</em></li>
</ul>
</div>
The div, h2, p, ul and li tags each get their own line, one level deeper than their parent. The h2 and the two list items are short, so each stays on a single line, and the three spaces in the heading collapse to one. The paragraph contains a br, so its text is split after the break and placed inside the p. The img has no closing tag and does not push the ul any deeper, and the em stays inline next to the word it styles. Minify turns the formatted version back into a single line identical to the minified original.
How it works
The tool scans your HTML and splits it into tags, text, comments and declarations such as the doctype. It keeps a stack of open elements: a block element like div, section, table, ul or p starts a new line and adds one indent level until its closing tag; inline elements like a, span, strong, em and code stay inside the current line of text. Void elements (br, img, input, meta, link, hr, source, wbr and others) never open a level, and a self-closing form such as br with a slash is accepted too. Optional end tags are understood, so a new li closes the previous li and a block tag closes an open p. Text inside pre, textarea, script and style is copied exactly, because whitespace there can change what the page shows or how the code runs. Minify removes ordinary comments, collapses whitespace runs in text to one space and drops whitespace that sits between block tags, while keeping the single space between inline elements that would otherwise run words together.
Tips
-
Beautify before you debug a layout. Misplaced closing tags are much easier to spot when every level is indented.
-
The formatter does not validate. If the indentation suddenly looks wrong partway down, that is often where a tag is missing or mistyped.
-
Minify does not compress JavaScript or CSS inside script and style tags. Use a dedicated minifier for those.
-
For data rather than markup, use the JSON Formatter or XML Formatter on this site.
Quick FAQ
Will formatting change how my page looks? Usually not, because browsers collapse most whitespace in normal text. Content inside pre and textarea is never touched. Rarely, a line break added between two inline-block elements can change the gap between them slightly.
Are comments kept? Beautify keeps every comment on its own line. Minify removes them, except conditional comments for old Internet Explorer.
Who it is for
It suits web developers reading minified templates, email developers untangling table-heavy markup, students learning how HTML nests, and site owners who pasted code into a builder and want to see what it actually contains. Because nothing leaves your browser, it is also fine for client code and internal pages.