HTML Formatter
Paste HTML and get consistently indented markup. The tool parses your input the way browsers do (WHATWG HTML), then re-serializes it with rehype-format.
How the formatter works
Your HTML is parsed into a hast syntax tree with rehype-parse, whitespace is rewritten by rehype-format, and the tree is serialized again with rehype-stringify. Because the input is really parsed, the output can differ from the input in more than whitespace:
- Missing tags are filled in. Unclosed
<li>or<p>elements are closed where a browser would close them. - Attributes are normalized. Values get double quotes; duplicate attributes keep the first value and produce a warning.
- Whitespace-sensitive content is respected.
<pre>,<textarea>,<script>and<style>contents are not re-indented. - Fragments versus documents. Input that starts with
<!doctype>or<html>is treated as a full document. Anything else is a fragment, so no<html>,<head>or<body>wrapper is added. You can override this with the Mode option.
The single newline that rehype-format puts before a fragment is trimmed so the output starts on line one. To do the same thing in a project, see Format and minify HTML with rehype or run rehype-format from the command line.
Packages used
rehype-parse, rehype-format, rehype-stringify, bundled locally from npm and run in a Web Worker. Your input stays on your device.
Other tools: HTML Minifier · HTML Sanitizer · HTML to Markdown · HTML Playground · Plugin Explorer