HTML Formatter
Indent and format HTML markup with consistent line breaks.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
Re-indented 1 line(s) of HTML at 2-space depth.
How it works
- Tokenised the markup into tags, comments, doctype, raw element bodies and text.
- Normalised whitespace inside tags without touching quoted attribute values.
- Emitted each structural token on its own line at a depth-based indentation.
- Copied script, style, pre and textarea bodies byte-for-byte.
- The formatter changes whitespace only; tags, attributes and text are preserved.
- Because HTML collapses whitespace, reflowing mixed inline content can introduce a space at an inline boundary.
- Output is 6 line(s); formatting it again changes nothing.
Methodology
- Tokenise the markup with a quote-aware tag scanner so `>` inside an attribute value cannot end a tag.
- Track nesting depth and emit each tag/comment on its own line at a depth-based indent.
- Preserve raw element bodies and re-emit tags with normalised whitespace.
Usage
- Paste or type an HTML document.
- Choose a 2- or 4-space indent.
- Run the formatter and copy the indented result.
Examples
These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.
Nested elements indented
{
"text": "<div><p>Hello</p><br></div>",
"indentSize": 2
}Raw script preserved
{
"text": "<script>if (a < b) { run(); }</script>",
"indentSize": 2
}Attributes with a quoted angle bracket
{
"text": "<a href=\"a>b\">link</a>",
"indentSize": 4
}Limitations
When to format HTML
Indentation makes markup reviewable and diffs readable. Format a document before reading or reviewing it; minify it when serving it.
The formatter is intentionally conservative: it never validates or repairs, so a broken input stays broken and visibly so.
Frequently asked questions
- Does formatting change my HTML content?
- No. Tags, attributes and text are preserved; only whitespace between and around them is normalised.
- Why is my script/style content untouched?
- Raw element bodies are emitted byte-for-byte so code that is whitespace-sensitive can never be altered.
- Is my HTML uploaded?
- No. All processing happens in your browser and nothing is sent anywhere.
Sources
- HTML Living Standard — WHATWG
- MDN Web Docs — HTML elements reference — MDN
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.