DEVELOPER TOOL

HTML Minifier — strip comments, collapse whitespace, shrink page weight

Paste any HTML and get back a smaller, cleaner version in your browser — no upload, no server round trip.

Includes a comment, loose whitespace, a <pre> block and a <script> block to demonstrate what gets preserved.
Size reduction
0%
 
0
Before (bytes)
0
After (bytes)
0
Bytes saved
0%
% saved
Tip: run your minified output through a diff tool once before deploying, just to confirm nothing inside a <pre> or <script> block shifted.

An HTML Minifier is a small piece of logic that takes a normal, human-formatted HTML document — full of indentation, comments, and blank lines — and rewrites it as a smaller file that renders the same in most layouts. This page runs that process entirely in your browser tab using vanilla JavaScript, so nothing you paste is ever sent anywhere.

Shipping lean HTML is one of the lower-risk performance changes available, but this particular tool makes a few trade-offs you should know about before you rely on it: it collapses whitespace between two inline tags to a single space rather than deleting it, drops whitespace next to block-level tags, and leaves quoted attribute values exactly as written. The sections below say exactly what it does so you can decide whether it suits your markup, or whether a build-tool minifier with an inline-aware setting is the better choice.

What This HTML Minifier Does

The tool performs three operations on the HTML you paste. First, it removes every HTML comment, anything wrapped in <!-- -->. Second, it deletes whitespace that sits directly between two tags (> < becomes ><), and collapses every remaining run of spaces, tabs and line breaks to a single space. Third, it trims whitespace from the start and end of the document.

Third, and most importantly, it never touches the inside of <pre>, <textarea>, <script>, or <style> tags. Whitespace inside those elements is either visually significant or syntactically significant, so the minifier detects those blocks first, protects their exact contents, runs the cleanup on everything around them, and then puts the protected content back untouched.

Alongside the minified output, the tool reports the before size in bytes, the after size in bytes, and the percentage saved, so you can see the real impact on your specific file rather than a generic estimate.

How to Use It

  1. Paste your HTML. Drop a full page, a component, or an email template into the input box on the left. A sample with a comment, loose whitespace, a <pre> block and a <script> block is preloaded so you can see the behavior immediately.
  2. Click Minify HTML. The button runs the whole process instantly in your browser — there is no upload step and no waiting on a server.
  3. Read the size panel. Before bytes, after bytes, bytes saved, and percent saved all update at once so you can judge whether minifying this particular file is worth the effort.
  4. Check the output textarea. Scan it quickly to confirm the script and pre blocks look untouched, since that's the part worth double-checking on unusual markup.
  5. Copy and use it. Click Copy Minified HTML to place the result on your clipboard, then paste it into your template, static file, or CMS field.

How HTML Minification Works

Under the hood, the tokenizer-style approach used here follows a simple order of operations. It scans the document for the four "protected" tag types and pulls their full contents — tags included — out into placeholder tokens, storing the real content in memory. This happens first so comment stripping and whitespace collapsing can never accidentally reach inside a script or preformatted block. Next, it strips every HTML comment from what remains, including conditional-comment-style syntax such as <!--[if IE]--> — this tool treats those the same as ordinary comments and removes them, a deliberate simplification worth knowing about if you still support conditional-comment hacks for legacy Internet Explorer.

After that, any whitespace sitting directly between two tags is removed, and every remaining run of spaces, tabs and line breaks collapses to one space. Finally, the placeholder tokens swap back in for the original protected content, byte for byte. The WHATWG HTML Living Standard and the MDN HTML documentation are the reference points for how browsers actually parse whitespace and comments.

HTML Minification vs. Gzip Compression

A common question is why minify HTML at all if the server already compresses responses with gzip or Brotli. The two techniques are separate and can be used together. Compression squeezes out repeated byte patterns, while minification removes bytes that carry no meaning, so a minified file is usually a little smaller after compression as well.

The extra saving on top of compression is usually modest, and it depends on how much indentation and how many comments your source has. Minification also matters where compression isn't applied at all, such as HTML embedded in emails, inline snippets injected into other documents, or responses a server chooses not to compress.

HTML Minification and Core Web Vitals

Page weight is one of many inputs into Core Web Vitals, and HTML is usually a small part of it. Trimming whitespace and comments reduces the bytes the browser downloads and parses before it finds your stylesheets and scripts, but on most pages that saving is small next to images, JavaScript and server response time. The sample on this page shrinks by 16.7%, mostly indentation, and your own file may differ either way.

It won't fix a slow server, a bloated JavaScript bundle, or unoptimized images. Lighthouse has dedicated audits for minifying CSS and JavaScript; HTML minification is a smaller, optional extra, and its saving depends on your markup, so judge it on your own markup rather than as a free win.

Safe vs. Unsafe Minification Techniques

Not every whitespace-removal trick is safe, which is where homegrown minification scripts get into trouble. Removing whitespace between block-level tags like </div> and <div> is almost always safe since browsers don't render it anyway. Removing whitespace between inline elements — say, the space between <span>Hello</span> and <span>World</span> — is riskier, since that space is often meaningful text content, and an aggressive minifier can accidentally glue two words together.

This tool is not conservative in that respect. It removes whitespace next to block-level tags (div, p, li, table rows and similar), where browsers do not render it, but between two inline tags it keeps a single space: <span>Hello</span> <span>World</span> stays as it is, so the words do not run together. A space between a tag and plain text is kept, so Hello <b>World</b> is unaffected. If your pages rely on spaces between inline elements, check the result, or use a build-tool minifier with an option that always keeps one space: html-minifier-terser, for example, documents a conservativeCollapse setting that never removes whitespace entirely. Removing optional closing tags or attribute quotes needs a full HTML parser to do safely, so this tool doesn't attempt either.

Build Tool Integration

For a one-off cleanup, pasting HTML into a browser tool like this one is fine. For production sites, minification is usually wired into whatever build step already exists — static site generators and bundlers commonly offer it as a plugin, server-side template engines support it as middleware, and CI pipelines can run a minification pass right before deployment so source stays readable while only the deployed artifact is minified. The basic steps are similar in most tools: protect script, style, pre, and textarea contents, strip comments, then collapse whitespace. Understanding the manual version here makes it easier to reason about what a build-tool plugin is actually doing, and gives you a quick way to sanity-check its output.

Common Encoding and Whitespace Edge Cases

A few edge cases are worth knowing about before relying on any HTML minifier, including this one. Non-breaking spaces written as the entity &nbsp; are not whitespace in the source, so they are left alone; a literal non-breaking space character between two tags is treated as whitespace and removed. Collapsing to one space is invisible in most layouts, which is why this tool keeps a single space between inline elements instead of removing it.

Multi-byte characters, such as emoji or non-Latin scripts, affect the byte-count math in the results panel — a single visible character can be several bytes once encoded, which is why this tool measures actual UTF-8 byte length rather than character count. Attribute values are not protected: a run of spaces inside alt="a b" collapses to one space, and a comment-style <!-- --> typed inside an attribute value would be stripped.

Accessibility Considerations

Minifying HTML should not change what assistive technology announces. Comments carry no accessibility information, so removing them is safe, and collapsing a run of spaces to one does not change the accessibility tree. This tool keeps one space between inline elements, so words in neighbouring links or spans are not merged. If you minify a production page, re-run the accessibility checks you already use on the output.

Testing Minified Output and Diffing in Version Control

Before trusting minified HTML in production, load both versions in a browser side by side and compare the rendered result, not just the source. Pay particular attention to anything inside a <pre> block, any inline JavaScript that depends on exact string formatting, and any whitespace-sensitive component.

On the version-control side, most teams keep the readable, commented source in Git and generate the minified version only as a build artifact, which keeps pull-request diffs readable while still shipping a lean file to production. If you ever need to compare two minified files directly, a plain text diff still works fine since minification is deterministic — the same input always produces the same output, so a diff reflects real source changes rather than noise from the minification step itself.

How to Read the Result

  • The big percentage is bytes saved divided by original bytes, to one decimal place. It is the same number as the "% saved" tile.
  • Before and after are UTF-8 byte counts, not character counts, so an emoji counts as 4 bytes and an accented letter as 2.
  • A low figure means the file was already compact, not that the tool failed. Savings come from indentation, blank lines and comments, so a hand-indented template saves more than generated HTML.
  • The percentage is the saving before server compression. After gzip or Brotli the real difference on the wire is usually smaller.

Results from the tool for typical inputs:

InputOutputBefore / after bytes, saved
The preloaded sample (comments, indentation, a <pre> and a <script> block)Comments gone, the <pre> and <script> contents unchanged, indentation removed604 / 503, 101 bytes, 16.7%
<p><span>Hello</span> <span>World</span></p> plus a line with two links separated by a spaceHello and World touch; the two links touch86 / 83, 3 bytes, 3.5%
<img alt="a b" title="x y"> <!-- c --><img alt="a b" title="x y">43 / 27, 16 bytes, 37.2%
<!--[if IE]><p>ie</p><![endif]--><p>x</p><p>x</p> (the conditional block is removed)41 / 8, 33 bytes, 80.5%
<p>hi</p> (already minimal)Unchanged9 / 9, 0 bytes, 0%
<p>😀 é</p><p>😀 é</p>16 / 14, 2 bytes, 12.5%

What to Do Next

  1. Render the result and compare it with the original.
  2. Keep the readable source in version control and treat the minified file as a build artifact.
  3. If your pages depend on spaces between inline elements, switch to a build-tool minifier with a conservative whitespace setting.
  4. Minify your stylesheets and scripts too, with the CSS minifier and JS minifier, where the audits in Lighthouse apply.
Need a full website, not just a smaller HTML file?

Arb Digital builds fast, well-structured websites where performance details like minification, caching, and clean markup are handled as part of the build — not an afterthought bolted on later.

See Our Services All Free Tools

Common Mistakes to Avoid

  • Minifying inside a <pre> or <script> block by hand. Manual find-and-replace whitespace cleanup often breaks code samples or inline scripts; always use a tool that explicitly protects those tags.
  • Assuming conditional comments are preserved. This tool removes all HTML comments, including old conditional-comment syntax for legacy Internet Explorer — check your output if you still depend on that pattern.
  • Not checking custom inline elements. This tool keeps one space between inline tags, but treats unknown or custom elements as inline; render the result once to confirm.
  • Skipping a visual check after minifying. Always render the minified output at least once before deploying, especially for pages with inline scripts or complex layout.
  • Minifying the file that lives in source control. Keep a readable, commented version in Git and generate the minified version as a build step, so future edits stay easy.
  • Expecting huge savings on already-clean HTML. Files with little whitespace or few comments will show a small percentage saved — that's expected, not a bug.
  • Forgetting that minification isn't compression. Pair HTML minification with gzip or Brotli on the server for the largest realistic reduction in transferred bytes.

Related Free Tools From Arb Digital

If you're cleaning up front-end code, try the JS Minifier and the CSS Minifier to shrink your scripts and stylesheets the same way this tool shrinks HTML. Convert between formats with Markdown to HTML and the HTML to Markdown converter, and use the HTML Encoder/Decoder to escape or decode entities. Browse the full set on our Free Tools hub.

Frequently Asked Questions

Does this HTML Minifier upload my code anywhere?

No. Everything runs client-side in your browser using vanilla JavaScript; your HTML is never sent to a server.

Will minifying break my JavaScript or CSS?

Not inside a <script> or <style> tag, because this tool preserves those contents exactly, including internal whitespace and line breaks. Inline style and onclick attribute values are protected too: whitespace and comment-like text inside quoted attribute values are left exactly as written.

What happens to comments during minification?

All HTML comments are removed, including conditional-comment-style syntax such as <!--[if IE]-->. This tool treats every comment the same way for simplicity.

Why is my percentage saved so small?

Files that already have minimal whitespace and few comments won't shrink much further. Minification helps most on hand-formatted, heavily indented, or comment-heavy HTML.

Is minified HTML still valid HTML?

Yes. This tool only removes comments and whitespace; it does not remove tags, attributes, or required syntax, so the output remains valid HTML. It keeps one space between inline elements, so the rendered text does not change in normal layouts.

Does it remove the spaces between inline elements?

No. Whitespace between two inline tags is collapsed to a single space, so two links or spans separated by a space stay separated. Whitespace next to a block-level tag such as div, p or li is removed, and a space between a tag and plain text is kept.

Should I minify HTML in addition to using gzip or Brotli?

Yes. Minification and server-side compression solve different problems and stack together, generally producing a smaller final transfer size than either technique used alone.

This tool runs entirely in your browser — nothing you paste here is ever uploaded or sent to a server.

Need help with the next step?

Want a fuller picture of your own site? Run the free AI Website Audit for a page-by-page report.