HTML Minifier

Minify or beautify HTML by removing comments, collapsing whitespace, and simplifying boolean attributes. See exact bytes saved instantly.

HTML minification strips unnecessary characters from your markup - comments, extra whitespace, and redundant attribute syntax - to reduce file size without changing how the page renders. Paste your HTML, click Minify, and see the exact byte count before and after. According to the HTTP Archive's July 2025 crawl, the median HTML document on the web is around 22 KB. Even modest percentage savings on that figure add up across millions of page loads.

Ad
Ad

About HTML Minifier

What Does the Minifier Remove?

Every transformation the minifier applies is safe and reversible (you can always re-format later with a prettifier). Here is what changes and what stays the same:

TransformationBeforeAfterSafe?
HTML comments<!-- navigation -->(removed)Yes - comments are for developers, not browsers
Excess whitespaceMultiple spaces, tabs, newlines between tagsSingle space or nothingYes - browsers collapse whitespace in normal flow anyway
Boolean attributesdisabled="disabled"disabledYes - both forms are valid HTML5 per the WHATWG spec
Whitespace between tags</div> <div></div><div>Yes for block-level elements (no visible difference)

The minifier does not touch attribute values, text content inside inline elements, or any structure of the DOM. The output is byte-for-byte equivalent in how the browser renders it.

What Is Preserved?

Content inside certain elements is whitespace-sensitive, so the minifier leaves it untouched:

ElementWhy It Is Preserved
<pre>Preformatted text depends on exact whitespace for layout
<script>JavaScript syntax depends on precise formatting - use a dedicated JS minifier separately
<style>CSS rules should be minified separately with a CSS minifier
<textarea>Content is user-visible and whitespace is significant

String content inside attributes, text nodes with significant whitespace, and inline elements where spacing affects rendering (like consecutive <span> elements separated by a space) are handled carefully to avoid visual changes.

How Much File Size Can You Save?

The percentage saved depends on how the original HTML was written. Well-commented, deeply indented source code shrinks the most. Already-compact markup shrinks less, but there is almost always something to trim.

Source TypeTypical ReductionWhy
Hand-written HTML with comments20-30%Developer comments and consistent indentation add significant bytes
Template engine output (Jinja, EJS, Handlebars)15-25%Template engines often inject extra whitespace between blocks
CMS-generated HTML (WordPress, Drupal)10-20%Plugin comments, theme boilerplate, and extra line breaks
Already-optimised markup5-10%Minimal whitespace to remove, but boolean attributes often still have verbose form

Worked example: Take a 22 KB HTML document (the median page size according to HTTP Archive 2025 data). Hand-written with comments and 4-space indentation, minification at 25% savings removes about 5.5 KB. Served to 100,000 visitors per month, that saves roughly 550 MB of transfer. It is a small number per request, but it compounds over time and improves time-to-first-byte for every visitor.

HTML Minification in the Build Pipeline

This browser-based tool is useful for quick one-off minification - paste, minify, copy. For production sites, you will want minification integrated into your build process so it runs automatically on every deploy.

ApproachTool / PackageNotes
Quick one-offThis toolPaste, minify, copy the result
Node.js build stephtml-minifier-terserThe most popular HTML minifier for Node, with over 12 million weekly npm downloads as of early 2026
Bundler pluginVite (vite-plugin-minify), webpack (html-webpack-plugin), esbuildRuns automatically during the build
Server-side middlewareExpress middleware, Nginx sub_filter, Apache mod_deflateMinifies responses on the fly (adds CPU per request)
Static site generatorsAstro, Next.js, Hugo, EleventyMost SSGs minify HTML output by default or via a flag

Note: Cloudflare deprecated its Auto Minify feature in August 2024. Their internal testing found that dynamically minifying HTML at the edge added latency that outweighed the file size savings for most sites. The recommendation now is to minify at build time instead.

Boolean Attributes in HTML5

The WHATWG HTML Living Standard defines certain attributes as boolean. Their presence on an element means "true" and their absence means "false". Writing the value is valid but redundant, and the minifier simplifies them to the short form:

AttributeVerbose form (valid)Short form (valid, preferred)Common on
disableddisabled="disabled"disabledbuttons, inputs, selects, fieldsets
checkedchecked="checked"checkedcheckboxes, radio buttons
requiredrequired="required"requiredinputs, selects, textareas
readonlyreadonly="readonly"readonlyinputs, textareas
autofocusautofocus="autofocus"autofocusany focusable element (one per page)
hiddenhidden="hidden"hiddenany element
asyncasync="async"asyncscript tags
deferdefer="defer"deferscript tags
multiplemultiple="multiple"multipleselect, file inputs
novalidatenovalidate="novalidate"novalidateform elements

The full HTML spec defines over 25 boolean attributes in total. The minifier handles all common ones. Note that "true" and "false" are not valid values for boolean attributes per the spec - the attribute should either be present (with an empty value or its own name) or absent entirely.

Minification vs Compression - How They Stack

Minification and compression are different techniques that complement each other. Minification reduces the source text itself. Compression (gzip or Brotli) encodes the byte stream for smaller transfer over the network. Using both together gives the best result.

AspectMinificationGzip CompressionBrotli Compression
What it doesRemoves unnecessary characters from the sourceCompresses the byte stream using DEFLATE algorithmCompresses using a dictionary-based algorithm optimised for web content
When it runsBuild time (once)Per request (server compresses, browser decompresses)Pre-compressed at build time or per request
Typical reduction10-30%60-70%65-80% (15-25% smaller than gzip on text files)
CPU costNone at runtimeLow per requestHigher per request (pre-compression preferred)
Browser supportN/A (source change)UniversalAll modern browsers (97%+ global support)

Worked example: Start with a 22 KB HTML document. Minification at 25% reduces it to 16.5 KB. Brotli compression at 75% then reduces that 16.5 KB to about 4.1 KB for transfer. Without minification, Brotli on the original 22 KB gives roughly 5.5 KB. The minified-then-compressed version saves about 1.4 KB extra per page load. That gap comes from removing the noise (comments, whitespace patterns) that even Brotli cannot fully eliminate.

Why Minification Still Matters in 2025 and Beyond

With HTTP compression handling the heavy lifting, some developers question whether HTML minification is still worth the effort. There are a few reasons it remains relevant:

Parse time, not just transfer size. Browsers must parse every byte of HTML to build the DOM. Fewer bytes means marginally faster parsing, which contributes to metrics like First Contentful Paint (FCP) and Largest Contentful Paint (LCP). Google's Core Web Vitals guidelines recommend keeping LCP under 2.5 seconds, and every optimisation counts toward that target.

Edge caching and CDN efficiency. Smaller source documents mean smaller cache entries. For sites with thousands of pages cached at CDN edge nodes globally, the storage savings add up.

Mobile networks. The HTTP Archive's 2025 data shows the median mobile page weighs 2.56 MB. While HTML is a small fraction of that, mobile connections with high latency benefit from any reduction in document size, since HTML is the first resource the browser needs before it can start fetching CSS, JS, and images.

Build pipeline hygiene. Minification as a build step catches accidental debug comments, TODO notes, and developer annotations that should not ship to production. It serves as a safety net against leaking internal notes into the public source.

Common Mistakes When Minifying HTML

Minification is generally safe, but there are a few edge cases to watch for:

MistakeWhat Goes WrongHow to Avoid It
Removing conditional commentsIE conditional comments (<!--[if IE]>) get stripped, breaking IE-specific fallbacksNot an issue for modern sites - IE support was dropped by Microsoft in June 2022
Collapsing whitespace in inline elementsRemoving the space between two <span> or <a> tags can cause words to run togetherThis tool preserves spaces where inline elements depend on them
Minifying embedded SVGSVG attribute values and path data can be whitespace-sensitiveKeep SVG in separate files or minify with an SVG-specific tool
Double-minifyingRunning minification twice rarely saves extra bytes but adds build complexityRun once during the build, not at both build time and CDN level
Forgetting pre/code blocksCollapsing whitespace inside <pre> or <code> destroys formattingThis tool automatically preserves pre, script, style, and textarea content

Beyond HTML - Minifying the Full Stack

HTML is just one part of the frontend stack. For maximum performance, minify CSS and JavaScript as well. Each type of minification uses different techniques suited to the language:

LanguageWhat Minification DoesTypical SavingsTool
HTMLRemoves comments, whitespace, simplifies attributes10-30%This tool or html-minifier-terser
CSSRemoves comments, whitespace, shortens colour codes, merges rules15-40%CSS Minifier
JavaScriptRemoves comments, shortens variable names, dead code elimination30-70%JavaScript Minifier

JavaScript sees the largest savings because variable renaming (mangling) can dramatically shorten identifier names across the entire file. HTML and CSS cannot rename identifiers since classes, IDs, and tag names must remain intact.

For the reverse operation - making minified HTML readable again - use the HTML Prettifier. To encode special characters for safe embedding, try the HTML Entity Encoder/Decoder.

All processing happens entirely in your browser. Your HTML is never sent to any server, and nothing is stored after you close the page.

Sources

Frequently Asked Questions

What does the HTML minifier remove?

It removes HTML comments, collapses multiple whitespace characters into one, strips whitespace between tags, and simplifies boolean attributes like disabled="disabled" to just disabled. The resulting HTML is functionally identical to the original.

Does it preserve script and style content?

Yes. Content inside pre, script, style, and textarea tags is preserved exactly as written. Only whitespace and comments outside of these elements are affected.

How much file size can I save by minifying HTML?

Typical savings range from 10% to 30% depending on how much whitespace and how many comments are in the original. The tool shows the exact byte count before and after so you can see the precise reduction.

Is my HTML sent to a server for processing?

No. All minification happens entirely in your browser using JavaScript string operations. Your markup is never uploaded or transmitted anywhere.

What are boolean attributes?

Boolean attributes are HTML attributes that are either present or absent and do not need a value, like disabled, checked, required, readonly, and autofocus. Writing disabled="disabled" is valid but redundant. The minifier simplifies these to the short form.

Link to this tool

Copy this HTML to link to this tool from your website or blog.

<a href="https://toolboxkit.io/tools/html-minifier/" title="HTML Minifier - Free Online Tool">Try HTML Minifier on ToolboxKit.io</a>