HTML / XML Formatter & Validator
Paste HTML or XML and get it cleanly indented, with well-formedness errors (XML) or unclosed-tag warnings (HTML) flagged.
1,224 views
How It Works
The formatter walks your markup and tracks nesting depth as it goes: every opening tag pushes the depth counter up one level, every matching closing tag pulls it back down. Each output line gets a number of leading indentation units proportional to its current depth — a tag one level deep gets one unit of indentation, a tag four levels deep gets four, and so on. Text nodes, comments and attributes inherit the depth of whatever element contains them, so the final result visually mirrors the document's actual tree structure instead of a wall of unbroken text.
For example, a cramped one-liner like <ul><li>A</li><li>B</li></ul> comes back as <ul> at the top level, with <li>A</li> and <li>B</li> each stepped in one level, and the closing </ul> dropped back to the top — three lines instead of one, and each one's depth immediately readable from its indentation alone. Void elements like <br> or <img> never push the depth counter, since they have no children to nest anything inside.
Good to Know
Pick XML mode and the parser is strict: any well-formedness violation — an unclosed tag, mismatched nesting, an invalid character — gets reported with a specific error message instead of being silently patched over. Pick HTML mode and the tool instead runs a best-effort tag-balance check, because browsers themselves parse HTML leniently and there's no equivalent hard "invalid" state to detect. This split reflects a real difference between the two formats: XML's spec demands that a document be well-formed or be rejected outright, with no middle ground, while HTML5 was deliberately designed so small authoring mistakes — like a forgotten closing tag — don't break the page. That permissive tradition is also why <br/> is mandatory in XML but <br> and <br/> are both valid, interchangeable HTML.
This is useful well beyond tidying up hand-written markup. Minified HTML fetched from a live page, an XML API response returned as one dense line, or a config file exported without any formatting all become far easier to read — and far easier to spot a misplaced tag in — once depth-based indentation is applied. Because the indentation is recomputed from the parsed structure rather than copied from the input, the output is consistent no matter how inconsistent the original spacing was.
Frequently Asked Questions
Why does XML mode catch errors that HTML mode misses?
XML has a strict well-formedness spec — the browser's XML parser rejects anything that breaks it. HTML parsing is deliberately forgiving (a core design goal, so the web doesn't break on small mistakes), so there's no equivalent hard error — this tool instead does a best-effort tag-balance check for HTML.
How does the tool decide how much to indent each line?
It tracks nesting depth as it parses: opening a tag increases the depth by one, closing a tag decreases it by one, and each line is indented by an amount proportional to its depth at that point. An element three levels deep in the tree gets three indentation units, regardless of how long or short the tags around it are.
Why is <br/> required in XML but optional in HTML?
XML has no built-in concept of a "void" element — every tag must be explicitly closed, either with a separate closing tag or the self-closing slash, or the document is not well-formed. HTML5 instead defines a fixed list of void elements (br, img, input, hr, and others) that browsers already know never take children, so both <code><br></code> and <code><br/></code> parse identically.
Does formatting change my content?
No — only whitespace and indentation change. Tags, attributes, text and comments are preserved exactly (attribute values are re-escaped for safety, which can only change quote characters, never the value itself).
Will this work on a deeply nested or minified document?
Yes — the depth-tracking approach doesn't care how the input was originally formatted or how deeply it's nested; a minified single-line file and a messily hand-indented one produce the same clean output, since indentation is recalculated from the tag structure itself rather than preserved from the input.
Similar Tools
Report a Problem
HTML / XML Formatter & Validator
Comments
No comments yet — be the first to write one!