XML & HTML Formatter

Developer

Indent or minify markup, without touching what must not move.

Formatted
<article class="post"> <h1> Title </h1> <p> Some <em> markup </em> with a <br> break. </p> <pre> indented code stays</pre> <ul> <li> one </li> <li> two </li> </ul> <!-- a comment --> </article>
16 tags · 0 problems
Tags16
Problems0
Characters276
This formats; it does not validate. Well-formedness against the full specification — entities, namespaces, doctypes — is a larger claim than indenting a document supports. The structural faults below are the ones it genuinely detects.
The contents of pre, textarea, script and style were left exactly as they were. Whitespace is part of the content there — re-indenting a pre block destroys a code sample, and a textarea gains leading spaces in its value.
Void elements such as br, img and input have no closing tag and are not indented into. Treating one as an opening tag pushes everything after it one level deeper for the rest of the document, which is the commonest way a hand-written formatter goes wrong.

How to use

What it's for

Make HTML or XML that is crammed onto one line readable again, with each tag on its own line and indented — or squeeze it small — and find a tag that was never closed.

Before you start

Have the markup to hand: the tagged code of a web page, an email template or a data file. It is formatted in your browser and nothing is sent anywhere.

  1. Paste the markup

    Replace the sample in Markup with your own HTML or XML. Markup is text built from tags in angle brackets, such as <p> and </p>.

  2. Choose the indent

    Indent sets how far each level is pushed in: Two spaces, Four spaces or Tab. Pick whatever the rest of your files use; two spaces is a common choice.

  3. Or make it small instead

    Tick Minify instead to remove the spacing between tags rather than add it, for a file that only a program will read. Indent is greyed out while it is ticked.

  4. Format and copy

    Press Format. The result is in the band at the top of the card — it also changes as you type — and Copy puts it on your clipboard.

  5. Check for broken tags

    If Structural problems appears below, each line names a tag and what is wrong with it. The output is still made, so you can look at its shape to find the spot.

Reading the result

  • Formatted (or Minified): your markup, laid out again. Only the spacing between tags changes; comments and everything else stay.
  • Tags, Problems and Characters: how many tags it found, how many structural faults, and the length of the result.
  • Never closed: a tag that opens and is never shut. Closes nothing: a closing tag with no matching opening tag.

If it goes wrong

  • Zero problems does not mean the document is valid. It tidies the layout and catches unclosed and stray tags, nothing more — a note on the page says so.
  • A code sample kept its odd spacing: the inside of pre, textarea, script and style is left exactly as it was, because the spacing there is part of the content.
  • Lines after a mistake are pushed in too far: look at the first Never closed line. A tag left open pushes what follows it in by one level, until the tag around it closes.

What this is

It formats; it does not validate, and the page says so. Reporting "valid" because a document could be indented is telling you something the tool does not know.

Some elements have whitespace as content: pre, textarea, script and style. Re-indenting inside a pre destroys the code sample; a textarea gains leading spaces in its value. Those four are left untouched.

Void elements have no closing tag. Treat <br> as an opening one and everything after it indents one level deeper for ever — the most visible way a hand-written formatter fails.

And it does not parse to a tree. A tree round trip drops comments, CDATA and unusual doctypes. This is a token stream: everything that goes in comes out.

Questions

Does this validate my XML or HTML?

No, and it says so on the page. Validating means checking against the whole specification — entities, DTDs, namespaces, encoding declarations — and a tool that reports “valid” because it managed to indent a document has told you something it does not know. What it does report is the one class of fault it genuinely detects: a tag that never closes, and a closing tag that matches nothing.

Why is my <pre> block left alone?

Because whitespace inside it is content, not formatting. Re-indenting a pre block destroys the code sample it contains, and a textarea treated the same way gains leading spaces in its value. Script and style are left alone for the same reason. Those four elements are passed through exactly as they arrived.

Why do some formatters indent everything after a <br> one level deeper?

Because they treat it as an opening tag. Void elements — br, img, input, hr, meta, link and the rest — have no closing tag in HTML, so nothing ever brings the indent back out, and the rest of the document drifts right for ever. It is the single most visible way a hand-written formatter fails, and this one knows the list.

Will it lose my comments or doctype?

No. It does not parse to a tree and re-emit, which is what drops anything the parser did not model — comments, processing instructions, CDATA, an unusual doctype. This works on a token stream: every token that goes in comes out, and only the whitespace between them changes. A formatter that silently deletes part of a document is far worse than one that leaves it oddly indented.

Why does it still produce output for a broken document?

Because refusing is no use to somebody trying to see where it broke. The problems are listed and the formatted output is produced anyway, so you can look at the structure and find the missing tag. An unmatched closing tag is also deliberately not allowed to change the indent level — otherwise one mistake pulls every following line left and the whole document looks wrong.

Is my markup sent anywhere?

No. Everything happens in your browser and nothing is transmitted or stored.

The maths

What is left untouched
pre, textareawhitespace is content
script, styleanother language inside
<!-- comment -->kept whole
<!DOCTYPE>, CDATAkept whole
The void elements

area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr have no closing tag. Nothing brings the indent back out once one is entered, so the whole rest of the document drifts right.

Keep going

Important

Runs entirely in your browser. This formats rather than validates — well-formedness against the full specification is a larger claim than indenting supports. It reports the structural faults it genuinely detects and still produces output for a broken document. Nothing you type here is sent to our servers — the calculation runs entirely in your browser.