X Xerobit

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.

Mian Ali Khalid · · 5 min read
Use the tool
HTML / CSS / JS Minifier
Minify HTML, CSS, or JavaScript. Strips whitespace, comments, and unnecessary characters. Shows size reduction percentage.
Open HTML / CSS / JS Minifier →

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.

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

OptionWhat it doesSafe?
collapseWhitespaceRemove whitespace between tagsYes
removeCommentsRemove HTML commentsYes (except IE conditionals)
removeOptionalTagsRemove </li>, </p>, etc.Yes
removeRedundantAttributesRemove type="text" from inputsYes
useShortDoctypeReplace DOCTYPE with <!DOCTYPE html>Yes
minifyCSSMinify inline stylesYes
minifyJSMinify inline scriptsYes
collapseBooleanAttributesdisabled="disabled"disabledYes
removeEmptyAttributesRemove class=""Risky (JS may check)
removeAttributeQuotesclass="foo"class=fooRisky

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 posts

Related tool

HTML / CSS / JS Minifier

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.