HTML Beautifier / Minifier

Beautify and minify HTML code

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

  1. Paste your HTML into the input area. A full document with <!DOCTYPE html> or a fragment both work.
  2. Choose the indentation in the settings: 2 spaces, 4 spaces or a tab.
  3. Click Beautify (or press Ctrl+Enter) to format, or Minify to compress.
  4. Copy the result with the Copy button or Ctrl+Shift+C. Ctrl+L clears 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, link and 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 &nbsp; and &lt; 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 // comments and 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: pre or pre-wrap on an ordinary element, or makes a div inline, 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.

Frequently Asked Questions

How do I beautify HTML online?

Paste your HTML into the input field and click Beautify (or press Ctrl+Enter). Block elements such as div, ul, li, table and section are put on their own lines and indented by nesting depth. Paragraphs, headings and links that only contain text stay on one line. Pick 2 spaces, 4 spaces or tabs in the settings, then copy the result with the Copy button.

What is the difference between beautifying and minifying HTML?

Beautifying adds line breaks and indentation so people can read the markup. Minifying removes comments and whitespace that the browser ignores, so the file is smaller. Both are the same document to the browser. Use beautify while you read or edit, and minify for the copy you ship.

Does beautifying HTML change how the page looks?

It should not, and this tool is built so it does not. Whitespace between inline elements such as span, a, b and img is rendered as a space, so the beautifier never adds a line break where the source had no whitespace and never removes one where it had some. Content of pre and textarea is copied exactly, and script bodies are left as written. The one thing it cannot know is your CSS: if a stylesheet sets white-space: pre on an ordinary div, re-indenting that div's children will show up on screen.

Why does my minified HTML show words stuck together?

The minifier you used deleted all whitespace between tags. In HTML, the space in <b>Hello</b> <i>world</i> is real text, so removing it renders Helloworld. A safe minifier collapses that whitespace to a single space instead of deleting it, and only drops whitespace next to block-level tags like div, p or li, where browsers ignore it anyway. This tool does exactly that.

Why isn't the JavaScript inside my script tag reformatted?

Script content is copied as-is in both modes, apart from aligning the closing tag. Re-indenting JavaScript inside HTML can change template literals and multi-line strings, and collapsing its line breaks can turn the next statement into part of a // comment. Paste the script body into the JavaScript Beautifier to format or minify it with a real tokenizer.

Why are some of my li and p tags left without a closing tag?

HTML allows end tags to be left out for li, p, td, tr, option, dt and dd, and browsers close them automatically. The beautifier understands these rules, so indentation is correct, but it never adds or removes tags. Your markup comes out exactly as you wrote it, just laid out differently.

Is my HTML data safe?

Yes. Beautifying and minifying run entirely in your browser using JavaScript. Your HTML is never uploaded to a server, which matters for email templates, internal admin pages and anything containing tokens or customer data.