How to Format HTML: Indentation and Pitfalls
Last updated: · 3 min read
HTML written on one line, or generated by a template system, makes it hard to see which tag sits inside which block. The HTML Formatter indents every element according to its depth in the tree, so the structure becomes visible at a glance. The content of pre, textarea, script and style is left untouched, and comments and attributes are preserved. This helps during both development and debugging. Formatting runs in your browser, so your markup is never sent to a server.
Formula
1. Split the HTML into tags, text, comments and the doctype; an opening tag increases the depth by one and a closing tag decreases it by one. 2. Write each element on its own line, indented by two spaces for every level of depth. 3. Trim redundant spaces at the start and end of text fragments and write each fragment on its own line. 4. Write void elements (such as br, img and input) without a closing tag; they do not change the depth. 5. Copy the contents of pre, textarea, script and style without any change.
This is not a mathematical formula but a parsing and layout algorithm. The tag rules follow the WHATWG HTML Living Standard. The formatter changes only the appearance of the markup, not the meaning of the document.
What the inputs mean
- HTML
- The HTML code to format. You can paste a page source or the output of a component. It does not matter whether it is on one line or irregularly indented.
Worked example: Indenting a nested list
- Input: <ul><li><a href="/a">A</a></li><li>B</li></ul>, a one-line list.
- The ul contains two li elements one level deeper; the first li holds a link.
- The link inside the first li is written on its own line, two levels deeper: <a href="/a">A</a>.
- The second li stays on a single line because it only contains text.
Result: The output is a six-line HTML fragment whose structure is clear: the link and the list items show their levels.
How to read the result
Because formatting changes line breaks and spaces, the appearance of inline elements can be affected. When an inline element such as b or a is preceded by text on a separate line, the browser shows a single space there. Test the formatted markup in a browser before you use it in production. This information is general.
Common mistakes
- Ignoring that a line break before an inline element such as b, a or span can show up as a space in the browser.
- Trying to close void elements such as br, img or input; in HTML these elements have no closing tag.
- Expecting missing closing tags to be repaired; the formatter does not complete the structure of broken markup.
- Thinking that the indentation inside pre or textarea will change; that content is preserved.
- Copying formatted HTML into production without checking its appearance in a browser first.
Frequently asked questions
Does formatting change how the page looks?
Usually not, but small differences can appear around inline elements because of line breaks. Check the page in a browser after formatting.
Are comments removed?
No. Comments and attributes are kept in the output.
Is the code inside script and style formatted?
No. Their content is left unchanged; use a dedicated JavaScript or CSS formatter for that.
Is my HTML sent to a server?
No. Formatting runs in your browser and the markup is not transmitted anywhere.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.