What is an HTML Beautifier?
An HTML beautifier (also called an HTML formatter or pretty printer) takes markup that is hard to read, such as a single-line page from a CMS, a minified production build, an email template or HTML copied out of browser DevTools, and rewrites it with one block element per line and indentation that follows the nesting. An HTML minifier does the opposite and strips the whitespace and comments the browser doesn’t need.
The difficulty with HTML, compared with JSON or XML, is that some whitespace is content. The space between two links is rendered. The line breaks inside a <pre> block are the whole point of the element. A formatter that treats every gap between tags as indentation will quietly change the page. This tool parses the markup into elements first and decides, per gap, whether whitespace there can be rendered.
How to Use This HTML Beautifier / Minifier
- Paste your HTML into the input area. A full document with
<!DOCTYPE html>or a fragment both work. - Choose the indentation in the settings: 2 spaces, 4 spaces or a tab.
- Click Beautify (or press
Ctrl+Enter) to format, or Minify to compress. - Copy the result with the Copy button or
Ctrl+Shift+C.Ctrl+Lclears the input.
What Beautify Changes, and What It Leaves Alone
Beautify only ever moves whitespace. Every tag, attribute, comment, entity and word comes out in the same order it went in.
- Block elements (
div,section,ul,li,table,tr,td,p,h1–h6,head,meta,linkand the rest) start on a new line, and their children are indented one level. - Inline content stays on one line. A paragraph like
<p>Run <code>npm i</code> to install</p>is short and reads naturally, so it is not exploded into five lines. Runs of spaces and newlines inside text collapse to one space, which is how the browser renders them anyway. - No new rendered spaces. If two inline elements touch in the source, as in
<button><svg>…</svg></button>or<b>bold</b><i>italic</i>, they stay touching even when the rest of the element is spread over several lines. - Attributes keep their order, quotes and values. Only the whitespace between attributes is normalized to a single space, so a tag split across several lines becomes one line.
- Entities such as
and<are never decoded, and a literal non-breaking space is not treated as whitespace. - Comments, the doctype and conditional comments are kept where they were.
- Long lines are not wrapped. A long paragraph stays one line. Wrapping prose is a style choice that tools like Prettier make, and it is easy to get wrong next to inline elements.
Elements That Are Never Reformatted
Four elements hold content that is not ordinary markup, and the tool treats them specially in both modes.
<pre>is copied exactly, character for character, including nested<code>and blank lines. Re-indenting it would shift every line of the code sample on screen.<textarea>is copied exactly, because its content is the field’s initial value.<script>content is kept as written. Beautify only moves the whitespace in front of</script>so the closing tag lines up with the opening one. Minify trims leading and trailing whitespace but keeps the line breaks inside, so// commentsand statements that rely on automatic semicolon insertion still work. Use the JavaScript Beautifier for the code itself.<style>is re-indented to sit one level inside its tag, which is safe because CSS strings cannot span lines. Minify removes the indentation and blank lines. For real CSS minification, run the stylesheet through the CSS Beautifier / Minifier.
Why Whitespace in HTML Is Not Always Safe to Change
Browsers lay out HTML in two kinds of boxes. Block elements like div and p stack vertically, and whitespace at their edges is ignored. Inline elements like span, a, strong, code, img and button flow along a line like words, and any whitespace between them, whether a space, tab or newline, renders as one space.
That is why a naive minifier that replaces >\s+< with >< breaks pages. <b>Hello</b> <i>world</i> becomes Helloworld, a row of <a> links in a footer loses its gaps, and inline-block buttons end up touching. It is also why a naive beautifier that puts every tag on its own line can add a visible gap between an icon and its label.
This tool classifies each element by its default display. For whitespace next to a block element, beautify may add a line break and minify may delete it. For whitespace between inline elements, the presence of whitespace is preserved and only its amount changes. Elements that are never rendered, such as script, style, template and comments, are treated as transparent, so removing a comment never glues two words together.
What Minify Removes
- HTML comments, except conditional comments like
<!--[if IE]>that some email clients still rely on. - Indentation and line breaks between block elements.
- Runs of whitespace inside text and between inline elements, collapsed to one space.
- Whitespace before the
>of a tag and repeated spaces between attributes.
It deliberately does not drop optional end tags, remove attribute quotes or shorten boolean attributes. Those save a few more bytes but make the output harder to diff, and some of them change how the HTML parser builds the tree. On a typical hand-written page, minification saves 10–25% before compression. With gzip or Brotli on the server the real-world gain is smaller, but it is still worth it for large documents and inline HTML in JavaScript bundles.
Common HTML Formatting Problems
Running markup through a beautifier is often the fastest way to find structural bugs, because the indentation shows how the browser will actually nest things.
- A
<div>inside a<p>. A paragraph cannot contain block elements, so the browser closes the<p>as soon as it sees<div>. The later</p>then creates a second, empty paragraph. The beautified output shows the<div>as a sibling, not a child, and the stray</p>on its own line. - Missing closing tags. An unclosed
<div>pulls everything after it one level deeper. If the indentation keeps drifting to the right toward the end of the file, look for the first element that never comes back. - Stray end tags. A
</span>with no matching opener is kept exactly where it was rather than silently dropped, and it does not pull the indentation of the following lines out of place. - Optional end tags.
<li>,<p>,<td>,<tr>and<option>may legally omit their end tags. The beautifier knows that the next<li>closes the previous one and indents accordingly. - CSS that changes whitespace rules. If your stylesheet sets
white-space: preorpre-wrapon an ordinary element, or makes adivinline, whitespace inside it becomes visible. No formatter can see your CSS, so check such elements after formatting.
HTML Beautifier vs Prettier and js-beautify
Prettier (npx prettier --parser html page.html) is the standard choice inside a project. It wraps long lines at a print width, reformats embedded CSS and JavaScript, and has an --html-whitespace-sensitivity option (css, strict or ignore) that controls the same inline-whitespace problem described above. js-beautify ships an html-beautify command used by many editors. html-minifier-terser is the usual build-time minifier, with options like collapseWhitespace and conservativeCollapse.
Those tools belong in your build or editor. This page is for the moments in between: reading an email template someone sent you, inspecting the HTML a CMS produced, cleaning up a snippet before pasting it into documentation, or shrinking a fragment for an inline string, without installing anything. For neighbouring formats, see the XML Formatter, and use HTML to Markdown when you want the text without the markup.