What is an HTML formatter?
HTML that comes out of a template engine, a CMS, an email builder or a minifier is written for machines: no indentation, elements run together on a handful of enormous lines, and the nesting — the one thing you actually need to see — is invisible. An HTML formatter (also called a beautifier) rewrites that markup with consistent line breaks and indentation: every nested element steps in by one level, siblings line up, and the skeleton of the page becomes something you can read at a glance and diff without tears.
This one does a second job before it formats, the same discipline
as this site's JSON tools: it checks the tag balance
first. Unbalanced markup — a missing </div>,
a </span> closed in the wrong order — is the
single most common cause of layouts that "mysteriously" break,
because browsers recover from it silently and unpredictably. If the
structure does not check out, you get the line and column of the
first problem instead of a confidently reformatted mess.
How to use this tool
- Paste your HTML into the box above (or press Load sample to see a worked example). You can also upload an .html file or drop one onto the box — the file is read in your browser and never sent anywhere.
- Choose an indent — 2 spaces is the common default for markup; 4 spaces and tabs are one click away.
- Press Format. If the markup is balanced you get the reformatted document and a line count. If it is not, you get the precise location of the first structural problem — fix that one and format again.
- Tick Auto-format as I type if you would rather not press the button: the result updates as you edit (very large inputs are still formatted on demand, to keep typing smooth). While the markup is mid-edit and unbalanced, the message line tells you so and your last good result stays on screen.
- Use Copy result or Download result to take the formatted document with you. Your input above is never modified — the result is a separate copy.
Checked first, then formatted
The balance check walks the document the way a browser's parser
does, which means it knows the difference between markup and text.
The contents of <script>,
<style>, <textarea> and
<title> are text, not tags: a
</div> appearing inside a script string closes
nothing, and a < in a comparison is not an error.
Comments, doctypes and quoted attribute values (which may
legitimately contain a >) are handled the same
way. Inline SVG and MathML follow the XML rules that apply to
them — self-closing elements, case-sensitive names — rather than
the HTML rules around them.
The check also knows HTML's legal shorthand. Roughly a dozen
elements may omit their end tag when the structure implies it:
a new <li> closes the previous one, a
block-level start tag closes an open <p>, a new
table row closes the previous row's cells, and
<html>, <head> and
<body> need no closing tags at all. Markup that
uses this shorthand passes the check, as it should. What fails is
the genuinely ambiguous: an element still open when its parent
closes, a closing tag with nothing to close, or a non-void
element left open at the end of the document.
What formatting never changes
Formatting here is a layout rewrite, never a data rewrite. Tag
names keep their spelling and case. Attributes are never
reordered, added, removed or split across lines. Text content is
carried across exactly; the contents of
<pre> and <textarea>, where
whitespace is part of the content, are preserved byte-for-byte.
Comments survive. What changes is the whitespace
between elements: indentation, the line breaks around
block-level elements, and blank lines, of which at most one
consecutive blank is kept, as authored. Inline runs — a sentence
with links and emphasis inside a paragraph — stay together on a
single line, so no new spaces appear inside your text.
Examples
This compressed fragment:
<section class="profile"><h2>Ada Lovelace</h2><p>Often called the first <strong>programmer</strong>.</p><ul><li>Notes</li><li>Letters</li></ul></section> formats into:
<section class="profile">
<h2>Ada Lovelace</h2>
<p>Often called the first <strong>programmer</strong>.</p>
<ul>
<li>Notes</li>
<li>Letters</li>
</ul>
</section>
And this broken fragment — a <span> never closed
inside its <div> — is reported with the span's
opening position and the place where the problem surfaces, instead
of being reformatted into a guess:
<div><span>Price: 42</div> Common HTML mistakes this catches
- The missing </div> — the classic: one unclosed container quietly swallows the rest of the page, and every section after it renders inside it.
- Crossed tags like
<b><i>text</b></i>— opened in one order, closed in another; browsers recover by inventing structure you did not write. - A self-closing slash on a normal element —
<div />does not close the div in HTML; the slash is ignored and the element stays open. - Closing a void element —
</br>or</img>: void elements have no content and no end tag. - An unclosed comment — everything after
<!--is comment until-->; forget it and half the page vanishes without an error anywhere.