How to Format HTML with 2-Space Indentation
Enter HTML text, keep beautify selected for readable indentation, and choose an indentation value such as 2 spaces. Use minify when you want the documented comment and whitespace reductions instead.
What HTML Formatter does
HTML Formatter turns HTML text into a more readable indented form and also offers a compact mode. It is useful when markup is difficult to scan, when you want consistent indentation for review, or when you need to reduce selected whitespace and comments before inspecting a compact version.
The formatter accepts HTML as a string. The default indentation is 2 spaces, and the default mode is beautify. An empty or whitespace-only input completes successfully with empty formatted text, so you can clear the field without receiving a formatting result.
How to format HTML
-
Enter the HTML text you want to format. Use a string value; a non-string value is rejected rather than formatted.
-
Choose an indentation value if you do not want the default. Values that can be converted to integers are used when the input contains non-whitespace characters. Values outside 0 through 8, and values that cannot be converted, fall back to 2 spaces.
-
Select a mode. Leave the mode at its default, or choose beautify to produce an indented representation. A nonempty mode that does not become
minifywhen lowercased follows the beautifying path, whileminifyuses the compact transformation. -
Run the formatter and review the returned text. Beautifying nonempty HTML can fail if the required HTML parsing dependency is unavailable, and an exception during HTML parsing or formatting is returned as an HTML parse error rather than a successful result.
-
For compact output, use minify mode. It removes comments unless their contents begin with
[if, collapses whitespace between tags, reduces repeated whitespace, and removes surrounding whitespace. This is a limited whitespace-and-comment transformation, not a general compression process. -
If the input is rejected, check its type and size. Non-string HTML is rejected, and non-whitespace input larger than 1,000,000 UTF-8 bytes is rejected.
How to read the result
A successful beautify result should be an indented HTML text representation. The indentation setting affects the formatting path when the input contains non-whitespace characters, while an empty or whitespace-only string produces empty formatted text. A successful minify result should be a more compact text form reflecting the documented comment and whitespace changes.
Treat a parse error as a formatting failure, not as proof that the source is valid or invalid HTML. This tool formats HTML text; it is not described as an HTML validator, sanitizer, security filter, or standards-compliance checker. Review the result when comments, unusual markup, or whitespace-sensitive content matters, because the documented minify behavior changes comments and whitespace.
Worked example
A developer is reviewing a compact section of page markup and wants a readable indented representation before making edits.
A developer enters <section><p>Hello</p></section>, leaves beautify mode selected, keeps the 2-space indentation default, and runs the formatter. The returned value has the shape of formatted HTML text.
The result is HTML text in an indented representation, with nesting made easier to scan; the example does not assume a particular serialized output.
Limitations
- Beautifying nonempty input can fail when the required HTML parsing dependency is unavailable, and parsing or formatting exceptions return as an HTML parse error.
- Minify applies the documented comment and whitespace changes; it is not described as full compression or as a validation process.
Common errors
- If the formatter rejects the input, confirm that the value is a string and that non-whitespace content does not exceed 1,000,000 UTF-8 bytes. Reduce the input or pass the HTML as text before trying again.
FAQ
What indentation does HTML Formatter use?
The default indentation is 2 spaces. For non-whitespace input, an indentation value outside 0 through 8 or one that cannot be converted falls back to 2.
What does HTML minify mode change?
Minify mode removes comments except comments whose contents begin with [if, collapses whitespace between tags, reduces repeated whitespace, and strips surrounding whitespace.
What happens when the HTML input is empty or too large?
An empty or whitespace-only string returns successfully with empty formatted text. Non-string input is rejected, and non-whitespace input larger than 1,000,000 UTF-8 bytes is rejected.