Skip to content
Everyday Utilities Free • No signup • Instant results

HTML Minifier

Compress HTML by removing whitespace, comments and line breaks.

About the HTML Minifier

The HTML Minifier compresses HTML by removing comments, collapsing whitespace, and stripping unnecessary characters — producing output that renders identically but transfers faster over the network. For a typical 50KB HTML page, minification saves 10-30% of the file size, which directly improves page load time, especially on mobile networks.

HTML is verbose. Every <div> tag is surrounded by whitespace for readability: newlines, indentation, blank lines between sections. Browsers do not need any of this whitespace (except inside <pre> and <code> elements where whitespace is significant). Comments are useful during development but serve no purpose in production. Removing both can shrink a hand-written HTML file by 20-40%.

This tool is conservative: it removes only what is safe to remove. It preserves the structure of <pre>, <code>, and <textarea> elements (where whitespace is significant), and it preserves conditional comments like <!--[if IE]> (which older browsers use for compatibility). For more aggressive minification that also collapses inline CSS and JavaScript, use a dedicated tool like HTMLMinifier or terser.

How It Works

If the “preserve whitespace in <pre>/<code>” option is enabled, the tool first extracts those elements using /<(pre|code|textarea)[^>]*>[\s\S]*?<\/\1>/gi and replaces them with placeholder tokens. The placeholders survive the minification and are restored at the end.

Comment removal uses /<!--(?!\[if)[\s\S]*?-->/g — the negative lookahead (?!\[if) preserves Internet Explorer conditional comments (which are still meaningful for some legacy content). All other comments are stripped.

Whitespace collapse runs several passes: \s+ becomes a single space (collapsing runs of any whitespace), >\s+< becomes >< (stripping whitespace between tags), \s+> becomes > (stripping whitespace before closing angle brackets), and <\s+ becomes < (stripping whitespace after opening angle brackets). The final trim() removes leading and trailing whitespace.

Worked Examples

The default input is a 13-line HTML document with a comment and significant whitespace. Minifying with all options enabled produces roughly 100 characters of output on a single line: <!DOCTYPE html><html><head><title>My Page</title></head><body><div><p>Hello, World!</p><p>Another paragraph</p></div></body></html>. The comment is gone, the whitespace is collapsed, and the document renders identically.

If you have a <pre> block containing a code sample, the “preserve whitespace” option ensures the indentation inside the <pre> is not collapsed — otherwise your code sample would lose its formatting. The placeholder mechanism extracts the <pre> contents before minification and restores them afterwards.

For a real-world page like a blog post template with sidebar and footer, minification typically saves 15-25% of the file size. Combined with gzip compression (which the web server applies automatically), the transferred size can be 70-80% smaller than the unminified source.

When to Use This Tool

  • Compressing HTML for production deployment to improve page load time.
  • Reducing bandwidth costs on high-traffic websites.
  • Preparing HTML email templates (where every byte counts against size limits).
  • Stripping comments before publishing (some clients dislike visible comments in the source).
  • Compressing HTML before embedding it in a JavaScript string or JSON payload.
  • Optimizing static site output (Jekyll, Hugo, Eleventy) before deploying.
  • Pre-minifying HTML snippets before pasting into CMS rich-text editors.

Limitations & Disclaimer

This tool performs pattern-based minification and may mangle edge cases like inline JavaScript containing < or > in string literals, server-side template tags (PHP, JSP, ERB) that look like HTML comments, or CDATA sections. It does not minify inline CSS or JavaScript — extract those and minify separately. The whitespace collapse is aggressive and may affect content inside elements that use white-space: pre or pre-wrap CSS (not just the <pre> HTML element). For production-grade minification, use a tool like HTMLMinifier (Node.js) that uses a proper HTML parser. See our disclaimer for full terms.

Frequently Asked Questions

Will minification break my HTML?

No, as long as you preserve whitespace in <code>&lt;pre&gt;</code>, <code>&lt;code&gt;</code>, and <code>&lt;textarea&gt;</code> elements (the default option). Outside of those elements, HTML treats any run of whitespace as a single space, so collapsing whitespace does not change the rendered output. The tool preserves IE conditional comments for legacy compatibility.

Should I minify HTML in development?

No &mdash; keep your source HTML readable during development. Minify only when building for production, ideally as part of your build pipeline (Webpack, Vite, Gulp). Minified HTML is harder to debug because all the whitespace and comments are gone.

How much size can I expect to save?

Typically 15-30% on hand-written HTML, less on already-compressed output. The savings depend on how much whitespace and how many comments your source contains. Inline CSS and JavaScript are not minified by this tool &mdash; use a dedicated CSS/JS minifier for those.

Does this tool minify inline CSS and JavaScript?

No. Inline <code>&lt;style&gt;</code> and <code>&lt;script&gt;</code> blocks are preserved as-is. To minify them, extract the CSS or JavaScript, run it through a dedicated minifier, and paste it back. Tools like HTMLMinifier (Node.js) handle inline CSS/JS automatically.

What about &lt;script type='application/ld+json'&gt; blocks?

These are JSON-LD structured data blocks. The tool does not specially handle them &mdash; whitespace inside them may be collapsed, which is safe for JSON (whitespace is not significant). To preserve their formatting, wrap them in <code>&lt;pre&gt;</code> for the purpose of minification, or extract and re-insert them manually.

Is my HTML uploaded anywhere?

No. Minification happens entirely in your browser using JavaScript string operations. Your HTML &mdash; including any proprietary templates or sensitive markup &mdash; never leaves your device.

Last updated: July 21, 2026  ·  Author: HT99 Tools Editorial Team  ·  Reviewed by: HT99 Tools Editorial Team