HTML Minifier Tools — Best Tools to Minify HTML Online and in Build Pipelines
HTML minifier tools compress HTML by removing whitespace, comments, and optional tags. Here's a comparison of the best tools for manual, CLI, and automated HTML minification.
HTML minifier tools reduce HTML file size by removing whitespace, comments, and redundant markup. The right tool depends on your workflow: a one-off online tool, a CLI command, or an automated build pipeline integration.
Use the HTML Minifier for instant online HTML minification.
Online HTML minifiers
Online tools are best for quick one-off minification, testing minifier settings, or verifying what a minifier does before adding it to your build:
xerobit.dev HTML Minifier — fast, no signup, configurable options:
- Remove whitespace between tags
- Strip comments
- Remove optional closing tags
- Minify inline CSS and JavaScript
- Remove redundant attributes
Best for: quick testing, comparing before/after, learning what gets removed.
html-minifier-terser (Node.js — most popular)
The definitive Node.js HTML minifier:
npm install html-minifier-terser
CLI usage
# Minify a single file:
html-minifier-terser --input-dir src --output-dir dist \
--file-ext html \
--collapse-whitespace \
--remove-comments \
--remove-redundant-attributes \
--minify-css true \
--minify-js true
# With all safe options:
html-minifier-terser index.html \
--collapse-whitespace \
--remove-comments \
--remove-optional-tags \
--remove-redundant-attributes \
--use-short-doctype \
--minify-css true \
--minify-js '{"compress":{"drop_console":true}}'
Programmatic API
import { minify } from 'html-minifier-terser';
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta tags -->
<meta charset="UTF-8">
<title>My Page</title>
<style>
.container {
background: white;
padding: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello World</h1>
<p>Content here.</p>
</div>
<script>
// Initialize
document.addEventListener('DOMContentLoaded', function() {
console.log('loaded');
});
</script>
</body>
</html>`;
const minified = await minify(html, {
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
useShortDoctype: true,
minifyCSS: true,
minifyJS: { compress: { drop_console: true } },
// Unsafe but effective:
removeEmptyAttributes: true,
collapseBooleanAttributes: true,
});
console.log(minified);
console.log(`Size: ${html.length} → ${minified.length} bytes`);
Build tool integrations
Vite (built-in)
Vite minifies HTML automatically in production builds:
// vite.config.js
export default {
build: {
// HTML minification is on by default in production
// No extra config needed
}
}
webpack (html-webpack-plugin)
// webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
minify: process.env.NODE_ENV === 'production' ? {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeOptionalTags: true,
minifyCSS: true,
minifyJS: true,
} : false,
}),
],
};
Gulp
// gulpfile.js
import gulp from 'gulp';
import htmlmin from 'gulp-htmlmin';
function minifyHtml() {
return gulp.src('src/**/*.html')
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true,
}))
.pipe(gulp.dest('dist'));
}
export default gulp.series(minifyHtml);
Next.js
Next.js automatically minifies HTML in production. No configuration needed.
Python: htmlmin
pip install htmlmin
import htmlmin
with open('index.html', 'r') as f:
html = f.read()
minified = htmlmin.minify(
html,
remove_comments=True,
remove_empty_space=True,
remove_all_empty_space=False,
reduce_empty_attributes=True,
reduce_boolean_attributes=True,
remove_optional_attribute_quotes=False,
keep_pre=True, # Don't minify <pre> content
)
with open('index.min.html', 'w') as f:
f.write(minified)
print(f'{len(html)} → {len(minified)} bytes ({100 * (1 - len(minified)/len(html)):.1f}% reduction)')
Minification options reference
| Option | What it does | Safe? |
|---|---|---|
collapseWhitespace | Remove whitespace between tags | Yes |
removeComments | Remove HTML comments | Yes (except IE conditionals) |
removeOptionalTags | Remove </li>, </p>, etc. | Yes |
removeRedundantAttributes | Remove type="text" from inputs | Yes |
useShortDoctype | Replace DOCTYPE with <!DOCTYPE html> | Yes |
minifyCSS | Minify inline styles | Yes |
minifyJS | Minify inline scripts | Yes |
collapseBooleanAttributes | disabled="disabled" → disabled | Yes |
removeEmptyAttributes | Remove class="" | Risky (JS may check) |
removeAttributeQuotes | class="foo" → class=foo | Risky |
Start with safe options; add risky ones only after testing.
Measuring the impact
# Check size before and after:
ls -lh index.html
html-minifier-terser --collapse-whitespace --remove-comments index.html > index.min.html
ls -lh index.min.html
# With gzip comparison:
gzip -c index.html | wc -c
gzip -c index.min.html | wc -c
Typical results:
- Raw minification: 15–30% reduction
- Gzipped original: 60–75% reduction from original
- Gzipped minified: 65–80% reduction from original
Gzip provides much larger savings than minification alone. But minification + gzip together beats either alone.
Related tools
- HTML Minifier — minify HTML online instantly
- HTML Minification Guide — comprehensive guide
- Minify HTML, CSS, JS — minification for all assets
Related posts
- Critical CSS Inlining — Eliminate Render-Blocking CSS for Faster Load — Critical CSS inlining embeds above-the-fold styles directly in HTML to eliminate…
- GZIP Compression for HTML — How It Works and How to Enable It — GZIP compression reduces HTML file size by 60-80% before sending to browsers. Le…
- HTML Comments Guide — Syntax, Uses, and When Minifiers Remove Them — HTML comments use <!-- --> syntax and are removed by minifiers unless they're co…
- HTML Minification Explained — What Gets Removed and Why — HTML minification removes whitespace, comments, and redundant attributes to redu…
- HTML Minification — What It Does and How Much It Saves — HTML minification removes whitespace, comments, and redundant attributes to redu…
Related tool
Minify HTML, CSS, or JavaScript. Strips whitespace, comments, and unnecessary characters. Shows size reduction percentage.
Written by Mian Ali Khalid. Part of the Dev Productivity pillar.