HTML formatter

An HTML formatter turns minified or tangled markup into readable code. This page formats HTML with 2 spaces, 4 spaces or a tab, or minifies it for production, and checks the structure: unclosed tags, crossed nesting, duplicate IDs and images without alt text. Free, no signup, everything runs in your browser.

Last verified: November 12, 2026 · Primary source: WHATWG HTML Standard · Independent tool, not affiliated with the W3C or WHATWG

Format HTML

Up to 2,000,000 characters, and up to 256 levels of nesting. Below 300,000 characters the result updates a moment after you stop typing; above that it waits for the Format button so typing stays smooth. Files up to 2 MB can be opened from disk. The input is read as text and never rendered, so scripts in it cannot run, and nothing is uploaded.

What each layout produces

The same input, <div><h2>Title</h2><p>Some <b>bold</b> text</p></div>, with one option changed at a time. All four outputs were measured with the tool above.

LayoutResultEffect
2 spaces<div>
  <h2>Title</h2>
  <p>Some <b>bold</b> text</p>
</div>
53 bytes in, 60 out, 4 lines. The default: one nesting level per two spaces.
4 spaces<div>
    <h2>Title</h2>
    <p>Some <b>bold</b> text</p>
</div>
64 bytes out. One nesting level per four spaces.
Tab<div>
  <h2>Title</h2>
  <p>Some <b>bold</b> text</p>
</div>
58 bytes out. One tab per nesting level (shown here as two spaces).
Minify<div><h2>Title</h2><p>Some <b>bold</b> text</p></div>53 bytes, 1 line. This input was already minified, so it comes back unchanged.

What the structure check catches

Every problem comes with its line and column and a fix. Anything else is accepted, as a browser would. Positions are 1-based. Each row below was run through the tool above.

InputMessagePositionFix
<div><p>Hello <b>world</div>The end tag </div> closes <div> while <b>, <p> are still open (crossed nesting).line 1Close </b> and </p> before </div>.
<div><p>x</p>The <div> opened here is never closed.line 1Add </div> where it should end.
<p>x</p></div>The end tag </div> matches nothing open, so browsers ignore it.line 1Remove </div>, or open a <div> for it to close.
<img src="x.png">This <img> has no alt attribute, so screen readers have nothing to read for it.line 1Add alt="" for a decorative image, or a short alt text that says what the image shows.
<div id="a"><p id="a">x</p></div>The id "a" is already used on line 1; ids must be unique on a page.line 1Give this element a different id.
<div class="a>x</div>The double quote that starts this attribute value is never closed.line 1, column 1Close the attribute value with a matching quote.
<p>x</p><!-- noteThis comment is never closed: it needs --> at the end.line 1, column 9End the comment with -->.
<html><head></head>...The <html> tag has no lang attribute, so screen readers cannot pick the right pronunciation.line 1Add lang="en" (or the page language) to <html>.
<div/>x</div>The self-closing slash on <div/> is ignored by browsers: <div> still needs an end tag.line 1Write <div> ... </div> instead of <div/>.

Unclosed quotes, tags and comments are hard errors: the run stops there and the message gives the line and column where the unclosed construct starts. Crossed nesting and unclosed elements are reported as errors too, and the output shows the formatter's best reading of the structure, so fix those first if the output is not what you meant.

What stays and what changes

Formatting changes whitespace, line breaks and indentation. It never adds an element, renames a tag, moves content, or rewrites an attribute value. One exception: end tags HTML lets you leave out are written out, so the structure you see is the structure a browser builds.

CaseYou typeThe tool outputsWhy
Script<script>if (a < b) { go(); }</script>UnchangedThe content is never read as HTML, so its spacing, "<" and ">" stay as typed.
Style<style>a > b { color: red; }</style>UnchangedCopied exactly; use a CSS minifier to shrink it.
Preformatted text<div><pre> a
b</pre></div>
<div>
<pre> a
b</pre>
</div>
Spaces and line breaks inside <pre> are part of what the page shows.
Textarea<p>Say <textarea> hi
there</textarea></p>
UnchangedIts content is the field's default value.
Comment<div><!-- note --><p>x</p></div><div>
<!-- note -->
<p>x</p>
</div>
Kept, on its own line. Minify drops it unless you tick keep comments.
Text spacing<p>one two
three&nbsp; four</p>
<p>one two three&nbsp; four</p>Runs of whitespace become one space (as a browser shows them); &nbsp; stays.
Attributes<a href = "x?a=1&amp;b=2" title='say "hi"' hidden>t</a><a href="x?a=1&amp;b=2" title='say "hi"' hidden>t</a>Names, values and quote marks as typed; only the blanks between them change.
Void elements<div><br><img src=a.png><hr></div>Each on its own lineNo end tag is added and no extra level is opened.
End tag left out<ul><li>One<li>Two</ul><ul>
<li>One</li>
<li>Two</li>
</ul>
Browsers close the first <li> themselves, so the formatter writes the end tag out.
Inline elements<p>Some <b>bold</b> text</p>Unchanged, one lineA block holding only inline content stays on one line when it fits in 100 characters.
Long blockA paragraph past 100 charactersSplits across linesReadability over one-line fidelity; the words and tags do not change.

How this page formats

The text is read once, front to back, by a small tokenizer written for this tool. It splits the text into start tags (with their attributes), end tags, comments, the doctype and other declarations, and text, and it jumps from the start tag of script, style, pre or textarea straight to the matching end tag so their content is never interpreted. Each step only moves forward and uses plain string searches, with no regular expressions, so the time grows in step with the length of the text however it is built. It does not use the browser's DOMParser and does not build a page, so nothing in the input can run, load or render.

Block-level elements start a line, indented one level per open block element. Inline elements and text stay together on a line, and a block element that holds only such content stays on one line when it fits in 100 characters. Runs of whitespace in text become one space; non-breaking spaces are content and stay. Void elements and self-closing tags open no level, and a few end tags HTML lets you leave out (li, dt, dd, tr, td, th, option, p and similar) are treated as ended by the next sibling, with the end tag written out. Depth is the most elements open at once; sizes are UTF-8 byte counts of the text before and after.

Formatter or validator?

Formatting is layout. Validation is correctness against the HTML specification: whether every element is allowed where it sits, whether every attribute is valid, whether the document follows the content models. This page does layout plus a small set of structural checks. It catches crossed nesting, unclosed tags, duplicate IDs and missing alt text, but it does not check whether a <div> belongs inside a <p> or whether an attribute value is in range. For that, use the W3C Markup Validation Service, which checks a page against the specification and lists every error with its line.

Format HTML in code

Use a maintained formatter in a build or an editor. The commands below show the two Prettier calls most developers reach for. Check the version you install, because defaults change.

Prettier, from the command line

npx prettier --write index.html
npx prettier --check "**/*.html"

VS Code formats the open file with Shift+Alt+F on Windows and Linux, or Shift+Option+F on a Mac, using Prettier when it is installed as the default formatter, or its built-in HTML formatter otherwise.

This page's formatter is a small tokenizer with no library behind it, so its output will differ from Prettier on edge cases. The layout rules it follows are listed above.

Frequently asked questions

How do I beautify HTML?

Paste it into the box and pick a layout. Each block-level element goes on its own line, indented one level deeper. Short elements that only hold text and inline tags stay on one line when they fit in 100 characters.

Will formatting change how my page looks?

It is built not to. Inline elements stay inside their line of text, so the spaces around them do not change, and runs of whitespace become one space, which is how a browser shows them anyway. The content of pre, textarea, script and style is copied exactly. The one thing that can differ is whitespace around inline-block elements or in CSS that treats whitespace specially, so compare the page if that applies to you.

What happens to script, style, pre and textarea?

Their content is copied character for character, including indentation and anything that looks like a tag. A script that contains the text "</p>" is therefore safe. If the end tag is missing, the content runs to the end of the text, as a browser would read it.

Does it add or remove closing tags?

It writes out the end tags HTML lets you leave out, such as </li> and </p>, so the structure you see is the structure a browser builds. It never adds an element, renames a tag, or moves content. An end tag that matches nothing open is kept on its own line and reported in the structure check.

What does Minify remove?

The whitespace between block-level tags, comments (unless you tick keep comments), and repeated spaces inside text. Conditional comments are kept. It is a whitespace-only minifier: it does not shorten attribute values, remove optional tags or compress scripts and styles.

Is my HTML uploaded or saved?

No. It is formatted inside your browser tab and is never sent, saved or kept in local storage, so it disappears when you reload.

Sources

WHATWG, HTML Standard: The syntax of HTML: void elements, optional end tags, raw text elements, and the attribute rules this page follows.

WHATWG, HTML Standard: Parsing HTML documents: how browsers close elements implicitly and read </br> as <br>.

MDN Web Docs, HTML elements reference: which elements are void, which are raw text, and the content model of each element.

Prettier, Options: HTML whitespace sensitivity: why whitespace around inline elements is kept and when formatting can change rendering.

W3C, Markup Validation Service: full validation against the HTML specification, for when the checks on this page are not enough.

Agents

Machine readable summary for AI assistants: