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>&copy; 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.

  1. Remove HTML comments — Strips every occurrence of <!-- … --> including multi-line comments. The regex /<!--[\s\S]*?-->/g matches 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.
  2. 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.
  3. 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.
  4. 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

Reduction (%) = (1 − minifiedLength / originalLength) × 100

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:

  1. 1originalLength = 520 characters
  2. 2minifiedLength = 340 characters (after comment removal, whitespace collapse, and empty-attribute stripping)
  3. 3Bytes Saved = 520 − 340 = 180 characters
  4. 4Reduction (%) = (1 − 340 / 520) × 100 = (1 − 0.6538) × 100 = 34.6%
  5. 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:

  1. 1originalLength = 4,800 characters
  2. 2Characters removed by comment stripping = 1,440 characters
  3. 3minifiedLength = 4,800 − 1,440 = 3,360 characters
  4. 4Reduction (%) = (1 − 3,360 / 4,800) × 100 = (1 − 0.70) × 100 = 30.0%
  5. 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:

  1. 1originalLength = 12,500 characters
  2. 2minifiedLength = 8,750 characters (whitespace collapse only)
  3. 3Bytes Saved = 12,500 − 8,750 = 3,750 bytes
  4. 4Reduction (%) = (1 − 8,750 / 12,500) × 100 = (1 − 0.70) × 100 = 30.0%
  5. 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:

  1. 1originalLength = 68,000 characters
  2. 2minifiedLength = 44,200 characters
  3. 3Bytes Saved = 68,000 − 44,200 = 23,800 bytes ≈ 23.2 KB
  4. 4Reduction (%) = (1 − 44,200 / 68,000) × 100 = (1 − 0.65) × 100 = 35.0%
  5. 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>&lt;!--[if IE]&gt;</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

For most modern websites, yes. Standard HTML comments (<code>&lt;!-- … --&gt;</code>) are never rendered to users and have no effect on the DOM or JavaScript. The exception is Internet Explorer conditional comments (<code>&lt;!--[if IE]&gt;…&lt;![endif]--&gt;</code>), which IE used to conditionally load CSS or scripts. If you still need to support very old IE versions, disable comment removal. In all other cases, stripping comments is completely safe.
Collapsing whitespace between tags is safe for block-level elements like <code>&lt;div&gt;</code>, <code>&lt;p&gt;</code>, and <code>&lt;section&gt;</code> because whitespace between blocks has no visual effect. It can theoretically affect inline elements — for example, two <code>&lt;span&gt;</code> tags separated only by a newline normally render with a small implicit space between them, which disappears when the whitespace is removed. In practice, most professional CSS-driven layouts use explicit spacing (margin, padding, or a space character in the text) rather than relying on inter-tag whitespace nodes.
HTML minification has a positive effect on SEO because page speed is a confirmed Google ranking signal. Smaller HTML documents load faster, which improves Core Web Vitals scores — particularly Largest Contentful Paint (LCP) and First Input Delay (FID). Minification does not affect the semantic content, headings, alt text, or structured data that search engine crawlers read, so there is no risk of damaging your on-page SEO while achieving performance improvements.
Minification is a permanent transformation of the source text that removes redundant characters, producing a smaller but still-valid HTML string. Compression (gzip or Brotli) is a reversible encoding applied at the HTTP transport layer that the browser automatically decodes. Minification happens once at build time and the result is stored; compression happens on every request (or is pre-computed for static files). Both reduce the bytes transferred, and they stack: applying both yields smaller payloads than either alone.
The reduction percentage depends entirely on how much 'dead weight' was present in the original HTML. A page generated by a CMS with verbose indentation, dozens of developer comments, and framework-injected empty attributes will see a much larger percentage reduction (25–45%) than a hand-coded minimal HTML shell with little indentation and no comments (5–15%). The absolute bytes saved also vary with file size: a 10% reduction on a 100 KB page saves more bytes than a 30% reduction on a 5 KB page.
Minify only in production. In development, formatted and indented HTML makes it much easier to inspect the page structure in browser developer tools, debug layout issues, and understand the rendered markup. Most modern build tools and frameworks (webpack, Vite, Next.js, Nuxt) automatically apply HTML minification only during production builds, keeping source HTML readable during development. This online tool is best used to audit specific pages or to manually minify HTML snippets and templates.
Yes, this tool processes the entire HTML string including any <code>&lt;style&gt;</code> blocks and <code>&lt;script&gt;</code> blocks that are part of the document. However, it applies only HTML-level transformations — it does not parse or minify the CSS or JavaScript inside those blocks separately. Whitespace inside inline scripts and styles will be collapsed in the same way as the rest of the document, which is generally safe but may affect scripts that rely on significant whitespace in template literals or regular expressions. For comprehensive minification of inline scripts and styles, consider a dedicated build tool.

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.

Source

Formula Source: Standard Mathematical References

by Various

UpdatedLast reviewed: May 2026
CheckedFormula checks are based on standard references and internal QA review.