Skip to main content

Online HTML Minifier & DOM Payload Optimizer

Rate this tool 0 of 0 ratings
Additional page content: Editable from the admin panel -> tools content.

Programmatic Code Snippets: Minify HTML

How to implement Minify HTML programmatically in Python, Node.js, PHP, and CLI
CLI & API Syntax
Minify HTML using htmlmin in Python
# pip install htmlmin
import htmlmin

html = """<div>
 <h1> Hello World </h1>
</div>"""
minified = htmlmin.minify(html, remove_empty_space=True)
print(minified)
Minify HTML using html-minifier-terser in Node.js
// npm install html-minifier-terser
const { minify } = require('html-minifier-terser');

const html = '<div>\n <h1> Hello World </h1>\n</div>';
minify(html, { collapseWhitespace: true, removeComments: true }).then(console.log);
Minify HTML using regular expressions in PHP
<?php
$html = "<div>\n <h1> Hello World </h1>\n</div>";
$search = ['/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'];
$replace = ['>', '<', '\\1'];
echo preg_replace($search, $replace, $html);
Minify HTML using html-minifier on CLI
npx html-minifier-terser --collapse-whitespace --remove-comments index.html -o index.min.html
Embed
Need this Html Minifier calculator on your website, lab portal, or research blog?
Free HTML Widget 100% Responsive Zero Cookie Tracking
Notice a calculation discrepancy, rounding error, or glitch?
Help us maintain 100% scientific precision and report any issues directly to engineering.