You open the source of an email template, a CMS export or a page built by a site builder, and it is one enormous line of tags. Or you inherit a hand-written page where the indentation drifted years ago and nobody can tell which </div> closes which. Before you can safely change anything, you need to see the structure.
This guide explains what an HTML beautifier does, the one whitespace rule that can make formatting or minifying change how a page looks, how much minification really saves, and how to format HTML in a browser, in your editor and in a build process.
What beautifying HTML does
A beautifier parses the markup and reprints it with one element per line and indentation that reflects nesting depth. A simplified example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shop</title>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
</ul>
</nav>
<p>Up to <strong>40% off</strong> today.</p>
</body>
</html>
Two details show that a good beautifier understands HTML rather than blindly breaking lines. Short inline content, such as the link inside the list item and the <strong> inside the paragraph, stays on one line. And it does not "fix" anything: an unclosed tag stays unclosed. Beautifying makes structure visible; it does not validate.
The whitespace rule that matters
In HTML, a run of spaces, tabs and newlines between inline elements is rendered as a single space. Between block elements, it is ignored. That difference is why formatting and minifying are usually safe but occasionally visible:
<a>Home</a><a>Offers</a>renders the two links touching. If a formatter puts each link on its own line, a space appears between them.<span>Total:</span> <b>49</b>needs the space. A minifier that removed all whitespace between tags would glue the words together.- Elements styled
display: inline-block(buttons, badges, navigation items) show small gaps caused by whitespace in the source. Layouts that rely on those gaps being absent can shift after reformatting. - Inside
<pre>and<textarea>, every space and line break is significant and must never be changed.
Safe tools respect these rules: they collapse whitespace to one space rather than deleting it, and they leave <pre> and <textarea> content alone.
Format or minify HTML with our tool
The HTML Formatter uses the js-beautify library for formatting and a conservative minifier, both in your browser.
- Open the HTML Formatter. Paste into HTML input, or click Open file to load an
.html,.htm,.xhtml,.vueor.sveltefile. Load sample shows a one-line page. - Choose the Indent size: 2 spaces, 4 spaces or Tab.
- Read the Formatted HTML output. Content inside
<head>and<body>is indented under<html>, and long lines are not wrapped, so attributes stay on the same line as their tag. - To shrink the markup instead, turn on Minify. Comments are removed and runs of whitespace collapse to a single space, while
<pre>,<textarea>,<script>and<style>blocks are left untouched. - Copy the result or Download it. Downloads are named with
-formattedor.min.
Blank lines you had in the source are kept (up to two in a row), so deliberate grouping survives formatting. Embedded CSS and JavaScript can be tidied separately in the CSS Formatter and JavaScript Formatter.
How much does minifying HTML save?
Less than people expect, for two reasons. HTML is usually a small part of a page's total weight compared with images, fonts and scripts. And web servers normally compress HTML with gzip or Brotli, which already squeezes repeated whitespace very efficiently. The difference between minified and unminified HTML after compression is often small.
Minifying is still worthwhile for:
- HTML emails, where some email clients clip messages above a certain size, so every byte of markup counts;
- inline snippets such as embed codes or widgets pasted into other sites;
- large generated pages, such as long tables or product listings.
For regular websites, let the build process minify automatically so the source stays readable. For more control, such as removing spaces between tags or minifying inline CSS and JavaScript, the dedicated HTML Minifier has separate switches for each step.
Other ways to format HTML
Your code editor
VS Code formats HTML out of the box with Format Document (Shift+Alt+F on Windows, Shift+Option+F on macOS); it uses js-beautify's rules under the hood. Most other editors have an equivalent command.
Prettier
Prettier is the standard formatter in many JavaScript projects:
npx prettier --write "src/**/*.html"
It formats consistently across a whole team and runs automatically on save or in a pre-commit hook. It is stricter than js-beautify about wrapping long lines and attributes, which some people find harder to read in templates.
Browser developer tools
The Elements panel in Chrome, Edge or Firefox always shows the live DOM as an indented, collapsible tree, even when the source is minified. It is ideal for inspecting a live page, although it shows the DOM after scripts have run, not the original file.
Tips and pitfalls
Templates are not plain HTML
Files full of template syntax ({{ }}, {% %}, <?php ?>, JSX) can confuse an HTML beautifier. Use your framework's formatter for these. To turn HTML into JSX for React, use HTML to JSX, which also renames attributes like class to className.
Formatting will not fix broken markup
If the indentation looks wrong after formatting, a tag is probably unclosed or misnested. Scan upward from the point where the indentation goes astray.
Check the result visually
After minifying, preview the page. The HTML Viewer renders pasted markup so you can compare before and after.
Frequently asked questions
Does formatting HTML change how the page looks?
Usually not. It can add a visible space between inline elements that previously touched, such as adjacent links or inline-block buttons. Content inside <pre> and <textarea> is not changed.
Is it safe to minify HTML?
A conservative minifier that removes comments and collapses whitespace to single spaces is safe for almost all pages. Test pages with inline-block layouts and preview emails before sending.
Should I use 2 or 4 spaces for HTML?
Either works. Deeply nested HTML becomes wide quickly, so 2 spaces is the more common choice.
Does the HTML formatter validate my markup?
No. It reformats what you give it. Use the W3C Markup Validation Service or your editor's linting for validation.
Why are my conditional comments kept when minifying?
Comments that start with <!--[if are Internet Explorer and Outlook conditional comments, which still matter in HTML emails, so the minifier keeps them.