HTML Minifier
Minify HTML by removing whitespace, comments, and unnecessary characters to reduce file size.
Input HTML
Options
Minified HTML
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Sample Page</title></head><body><header><nav><ul><li><a href="/">Home</a></li><li><a href="/about">About</a></li><li><a href="/contact">Contact</a></li></ul></nav></header><main><article><h1>Welcome to Our Website</h1><p>This is a sample paragraph with some content.</p><div class="container"><button onclick="handleClick()">Click Me</button></div></article></main><footer><p>© 2024 Sample Company</p></footer></body></html>Compression Results
Original Size
758 bytes
Minified Size
565 bytes
Bytes Saved
193 bytes
Reduction
25.5%
Tip: Minified HTML loads faster and uses less bandwidth. Combined with gzip compression, you can achieve significant size reductions.
What Is HTML Minification?
HTML minification is the process of removing every character from an HTML document that the browser does not strictly need to parse and render the page correctly. This includes whitespace characters such as spaces, tabs, and line breaks between tags; HTML comments; and empty attribute values. The resulting minified HTML is functionally identical to the original source — browsers produce exactly the same DOM tree and visual output — but the file is smaller, which means it transfers to the user's browser faster.
Every byte of HTML your server sends must travel across the network before the browser can begin constructing the DOM. Because browsers cannot render a page until they have received and parsed the initial HTML document, reducing its size directly shortens the time to first byte and the time to first contentful paint. HTML minification is therefore one of the most direct optimizations you can apply to improve real-world page load speed.
Modern HTML files can accumulate significant dead weight over time. Developers add generous indentation to keep markup readable, insert comments to document component boundaries and template logic, and framework-generated markup often includes empty attributes that serve no purpose in production. A typical HTML page from a content management system or front-end framework can contain 20–40% more characters than the minimum required to represent the same document.
This free online HTML minifier applies three configurable transformations: comment removal, whitespace collapsing, and empty-attribute stripping. Each transformation is independently toggleable so you can choose the right balance between size reduction and compatibility for your specific use case. The tool reports your original file size, the minified size, the exact number of bytes saved, and the percentage reduction — all calculated live in your browser without sending your code to any server.
HTML minification complements other delivery optimizations. When combined with server-side gzip or Brotli compression, minified HTML achieves smaller transfer sizes than formatted HTML compressed alone, because compression algorithms perform better on already-compact input. Together, minification and compression can reduce HTML payload by 60–80% compared to the original formatted source.
How This HTML Minifier Works
This tool applies up to four regex-based transformations to your HTML input, executed in a fixed sequence. Each transformation targets a distinct category of removable content. Understanding the pipeline lets you predict exactly what the output will look like and verify that the minifier is safe to use on your specific markup.
- Remove HTML comments — Strips every occurrence of
<!-- … -->including multi-line comments. The regex/<!--[\s\S]*?-->/gmatches lazily so it never spans across unrelated comment blocks. Conditional comments used by legacy IE (<!--[if IE]>) are also removed when this option is on, so disable comment removal if you support very old browsers that rely on them. - Remove inter-tag whitespace — Replaces any sequence of whitespace between a closing
>and an opening<with nothing (/>\s+</g→><). This is safe for block-level elements but can theoretically collapse intentional spacing between inline elements like<span>tags separated only by a newline. In practice, most inline spacing is handled by CSS rather than whitespace nodes. - Trim line whitespace and remove newlines — Strips leading and trailing whitespace from every line (
/^\s+|\s+$/gm), then removes all remaining newline characters, then collapses any run of multiple spaces down to a single space. The result is a single-line document with no leading or trailing padding. - Remove empty attributes — Removes attribute assignments of the form
attr=""(where the value is an empty string) using the regex/\s+\w+=""/g. This is useful for framework-generated markup that outputs placeholder attributes. Do not enable this option if your JavaScript reads the presence of empty-string attributes as a meaningful signal.
After all selected transformations are applied, the tool trims any remaining leading or trailing whitespace from the final string and computes the size statistics.
Size Reduction Formula
Where:
- originalLength= Character count of the original HTML string (input.length)
- minifiedLength= Character count of the minified HTML string after all transformations (minified.length)
- Reduction (%)= Percentage of characters removed; displayed to one decimal place using toFixed(1)
- Bytes Saved= originalLength − minifiedLength; number of characters (bytes for ASCII) eliminated
When to Use HTML Minification
HTML minification is most beneficial in production environments where page load performance directly affects user experience and search engine rankings. If you are serving a high-traffic website, an e-commerce store, or any page where load speed influences conversion rates, minifying your HTML output should be part of your standard build pipeline.
Static site generators such as Hugo, Eleventy, and Jekyll can integrate HTML minification as a post-build step, so every HTML file written to the output directory is automatically minified before deployment. For server-rendered applications built with frameworks like Next.js, Nuxt, or SvelteKit, minification is often handled by the framework's production build process or by middleware that compresses responses on the fly.
This online tool is particularly useful in several scenarios. First, when you want to audit how much size reduction is achievable on a specific page before committing to a build-time integration. Second, when you are working with HTML email templates, which are often sent as raw HTML and benefit from reduced byte counts. Third, when you need to minify a one-off HTML snippet or template fragment without setting up a local build tool.
HTML minification is generally safe for modern pages that follow standard HTML5 practices. The main scenarios where caution is needed are: pages that use conditional comments for IE compatibility (those will be stripped by comment removal), pages where inline scripts contain string literals that include HTML comment syntax, and pages where whitespace between inline elements is visually significant and not controlled by CSS. For typical modern websites, all three options can be enabled without any rendering difference.
Development environments benefit less from minification because minified HTML is harder to inspect in browser developer tools. Keep your source files formatted and readable during development, and apply minification only to production builds or deployment artifacts.
Typical HTML Size Reductions
The size reduction achieved by HTML minification varies depending on how verbose the source HTML is. Pages generated by CMSes or template engines tend to have more indentation and comments than hand-coded pages, and therefore benefit more from minification. The table below shows typical ranges observed across different page types.
| Page Type | Typical Original Size | Reduction (Comments + Whitespace) | Reduction (Whitespace Only) |
|---|---|---|---|
| Simple landing page | 8–20 KB | 15–30% | 10–20% |
| WordPress blog post | 40–80 KB | 20–40% | 15–30% |
| E-commerce product page | 60–150 KB | 25–45% | 18–35% |
| Framework SPA shell | 5–15 KB | 10–20% | 8–15% |
| HTML email template | 20–60 KB | 25–50% | 20–40% |
These figures are estimates based on common formatting patterns. Your actual reduction will depend on the specific content, indentation depth, and comment density of your HTML file. Use the calculator above to measure the exact reduction for your own document.
HTML Minification vs. Server Compression
HTML minification and server-side compression (gzip or Brotli) are complementary techniques that operate at different layers of the delivery stack. Understanding the difference helps you apply both correctly and avoid the misconception that one replaces the other.
Minification is a static transformation applied once to the source file (or to each dynamically rendered response). It removes characters that are semantically redundant — whitespace, comments, empty attributes. The output is still valid, human-readable HTML (though dense), and no encoding or decoding step is needed at runtime. The minified file can be stored on disk, cached in a CDN, and served directly to clients.
Gzip and Brotli compression are encoding algorithms applied dynamically (or statically for pre-compressed files) at the HTTP transport layer. They find repeated byte patterns across the entire file and replace them with shorter references, achieving significant reductions that are completely transparent to the browser because it decompresses the response automatically. Brotli typically achieves 15–25% better compression ratios than gzip for text files.
Critically, these two techniques stack: a minified file that is then gzip-compressed achieves a smaller final transfer size than a formatted file that is gzip-compressed alone. This is because minification eliminates redundant characters before compression runs, giving the compression algorithm a head start. Benchmarks consistently show that minification adds 5–15% additional savings on top of compression for typical HTML files.
The recommendation is to use both. Minify your HTML as part of your build process or delivery pipeline, and configure your web server (Apache, Nginx) or CDN (Cloudflare, Fastly, AWS CloudFront) to apply gzip or Brotli compression to all text responses. Together, these two optimizations can reduce your HTML payload to 10–20% of its original uncompressed, unformatted size.
Worked Examples
Basic Page — All Options Enabled
Problem:
A small HTML document is 520 characters including 2 comments, indentation whitespace, and an empty class attribute. After removing comments, collapsing whitespace, and removing empty attributes, the minified output is 340 characters. Calculate the percentage reduction.
Solution Steps:
- 1originalLength = 520 characters
- 2minifiedLength = 340 characters (after comment removal, whitespace collapse, and empty-attribute stripping)
- 3Bytes Saved = 520 − 340 = 180 characters
- 4Reduction (%) = (1 − 340 / 520) × 100 = (1 − 0.6538) × 100 = 34.6%
- 5Result: 34.6% reduction, 180 bytes saved
Result:
34.6% size reduction — the minified file is 65.4% of the original size
Comment-Heavy Template — Comments Only
Problem:
A CMS-generated HTML file is 4,800 characters. It contains extensive developer comments occupying 1,440 characters. Only the 'Remove comments' option is enabled. Calculate the reduction.
Solution Steps:
- 1originalLength = 4,800 characters
- 2Characters removed by comment stripping = 1,440 characters
- 3minifiedLength = 4,800 − 1,440 = 3,360 characters
- 4Reduction (%) = (1 − 3,360 / 4,800) × 100 = (1 − 0.70) × 100 = 30.0%
- 5Bytes Saved = 4,800 − 3,360 = 1,440 bytes
Result:
30.0% reduction purely from comment removal — 1,440 bytes saved
Minified HTML Email Template
Problem:
An HTML email template is 12,500 characters with heavy indentation. With whitespace collapsing enabled (but comments left intact to preserve any conditional logic), the minified output is 8,750 characters. Find the reduction percentage and bytes saved.
Solution Steps:
- 1originalLength = 12,500 characters
- 2minifiedLength = 8,750 characters (whitespace collapse only)
- 3Bytes Saved = 12,500 − 8,750 = 3,750 bytes
- 4Reduction (%) = (1 − 8,750 / 12,500) × 100 = (1 − 0.70) × 100 = 30.0%
- 5At 3,750 bytes saved on a 12,500-byte template, this is a meaningful reduction for email delivery where some providers impose size limits
Result:
30.0% reduction (3,750 bytes saved) using whitespace collapse alone
WordPress Page — All Options Enabled
Problem:
A WordPress page renders to 68,000 characters. After enabling all three minification options (remove comments, collapse whitespace, remove empty attributes), the output is 44,200 characters. What is the percentage saved?
Solution Steps:
- 1originalLength = 68,000 characters
- 2minifiedLength = 44,200 characters
- 3Bytes Saved = 68,000 − 44,200 = 23,800 bytes ≈ 23.2 KB
- 4Reduction (%) = (1 − 44,200 / 68,000) × 100 = (1 − 0.65) × 100 = 35.0%
- 5Combined with gzip compression, the 44,200-character minified file could compress to roughly 7–9 KB over the wire
Result:
35.0% reduction — 23,800 bytes saved before server compression is applied
Tips & Best Practices
- ✓Enable all three options (remove comments, collapse whitespace, remove empty attributes) for maximum reduction on standard production HTML.
- ✓Keep comment removal disabled if your page uses IE conditional comments (<code><!--[if IE]></code>) for legacy browser compatibility.
- ✓After minifying, always test your page in a browser to confirm that whitespace collapse has not affected any inline element spacing that was controlled by whitespace nodes rather than CSS.
- ✓Combine HTML minification with server-side gzip or Brotli compression for the best transfer size; minification adds 5–15% on top of compression alone.
- ✓For build pipelines, prefer tools like html-minifier-terser (Node.js) which offer more options including attribute quote removal and optional tag omission for standards-compliant HTML.
- ✓Minify HTML email templates to reduce their size and improve deliverability — many email providers limit message size, and smaller HTML can help avoid clipping in Gmail.
- ✓Use the percentage reduction metric to benchmark pages over time; a sudden drop in reduction efficiency can indicate a template change that introduced unnecessary verbosity.
- ✓Do not minify HTML files that contain server-side template syntax (e.g., Jinja, Twig, Blade) — run minification after the server has rendered the final HTML output.
Frequently Asked Questions
Sources & References
Last updated: 2026-06-05
Help us improve!
How would you rate the HTML Minifier?
Editorial Note
MyCalcBuddy Editorial Team
This page is maintained as an educational calculator reference.
Formula Source: Standard Mathematical References
by Various