How to format HTML
- Paste your HTML into the box, such as a full page from View Source or a fragment from a template.
- Choose 2 spaces or 4 spaces next to Indent. The default is 2.
- Select Format HTML. The result appears below.
- Select Copy result to copy it, or Clear to empty both boxes.
The tool only runs when you press the button. If you change the indent after formatting, press Format HTML again. For example, <div class="card"><h2>Title</h2><p>Text</p></div> with 4 spaces becomes six lines. The <div> sits at the left edge. <h2>Title, </h2>, <p>Text and </p> are each indented 4 spaces, and </div> closes at the left edge.
How the formatter decides indentation
The tool first removes the whitespace between tags. Then it starts a new line before every < and indents each line based on the tags it has seen so far.
| Item | What happens |
|---|---|
| Opening tag, such as div or p | Own line. Everything after it moves in one level. |
| Closing tag, such as /div | Own line, one level back. It does not stay next to the text before it. |
| Void tags: area, base, br, col, embed, hr, img, input, link, meta, param, source, track, wbr | Own line, no extra level. Upper case such as BR works too. |
| Tags ending in />, such as SVG path | Own line, no extra level. |
| DOCTYPE and comments | Own line, no extra level. |
| Text | Stays on the same line as the tag before it. Leading and trailing spaces on each line are trimmed. |
So <title>Hi</title> comes out as <title>Hi on one line and </title> on the next, at the same indent as the opening tag.
Whitespace changes that can affect the page
Formatting is not always invisible. Browsers treat a line break inside text as a space, and the tool adds and removes line breaks freely.
- Spaces between inline tags are removed.
<span>a</span> <span>b</span>loses the space between the spans, so the page shows ab instead of a b. - Closing tags move to a new line. In
<p>Hello <strong>world</strong>, how are you?</p>the line break before</strong>shows up as a space, so the page reads world , how are you? - pre and textarea are not protected. Each line inside loses its own leading spaces and gets the formatter's indent instead, trailing spaces are cut, and a line break is added before the closing tag. Code samples and ASCII art inside
<pre>will shift.
Script, style and broken markup
- Inline script. The JavaScript is not formatted. Its existing lines are trimmed and all placed at one level, so nested code loses its indentation. A
<inside the script, as inif (a < b), is read as a new tag and shifts the indent of everything after it. - Inline style. CSS is left as it is on its line. Paste it into the CSS Formatter to lay it out.
- Missing closing tags. HTML lets you skip some closing tags, such as for li and p. The formatter does not know that, so
<ul><li>One<li>Two</ul><p>after</p>ends with the p indented two levels too deep.
For scripts, copy the code into the JavaScript Formatter. To strip whitespace for production instead of adding it, use the HTML Minifier.
Privacy
The formatter runs in your browser and makes no network requests, so the markup you paste stays on your device.
Common Mistakes to Avoid
Pasting the formatted HTML back into a live page
Solution:
Leaving out optional closing tags
Solution:
Expecting embedded CSS and JavaScript to be formatted
Solution:

