An HTML minifier strips whitespace, comments, and optional syntax to shrink HTML output. The tool above runs the minifier in your browser; nothing leaves your machine. For the inverse direction, see HTML formatter.
What gets stripped
In rough order of safety:
| Transformation | Example | Risk |
|---|---|---|
| Strip comments | <!-- ... --> removed | Low (except IE conditional comments) |
| Collapse multiple spaces | <p>a b</p> → <p>a b</p> | Low |
| Strip whitespace between tags | </p>\n<p> → </p><p> | Medium (inline elements) |
| Strip leading/trailing whitespace | Outside the document body | Low |
| Remove optional closing tags | </li>, </td>, </p> | Low (HTML allows omission) |
| Remove optional doctype attributes | <!DOCTYPE html> already minimal | None |
| Shorten boolean attributes | required="required" → required | Low |
| Remove optional quotes around attribute values | id="foo" → id=foo (only when value has no spaces or special chars) | Medium |
| Strip empty attributes | class="" removed | Low |
Inline <script> and <style> minification | Run subordinate minifier | Medium |
A reasonable minifier achieves 5–20% size reduction on hand-written HTML. Auto-generated HTML (React SSR) is already pretty dense and minifies less.
What’s unsafe to strip
- Whitespace inside
<pre>,<textarea>,<code>. These elements preserve whitespace by spec; a minifier that strips inside them changes rendered output. - IE conditional comments.
<!--[if IE]>...<![endif]-->looks like a regular comment but had meaning to IE. Modern minifiers preserve them by default; ancient ones strip them and break IE rendering. - Whitespace around inline elements.
<a>X</a> <a>Y</a>(with space) renders differently from<a>X</a><a>Y</a>(no space). A correct minifier knows the inline-vs-block distinction. - Comments with
<!-- /\* */ -->style markers. Some templating systems use HTML comments as their own delimiters. - Server-side template tokens.
{{}}(Mustache, Vue),${...}(template literals),<?=(PHP). A pure-HTML minifier may not recognize these and may produce broken templates.
Minifier vs gzip / brotli
A minified HTML file then gzipped is smaller than a formatted HTML file gzipped, but not by 1:1 of the minification savings. Gzip and brotli already remove most of the redundancy that minification removes (repeated whitespace, repeated tag names). The marginal benefit of minification on the wire is real but smaller than the disk-savings number suggests — typically 2–5% of the gzipped size.
When minification matters
- Inline HTML in JSON or response bodies that aren’t separately compressed. Bytes count more here.
- Very large hand-written pages where minification savings stack up.
- Email HTML. Email size matters for delivery, and email pipelines often don’t compress payloads.
- Pages served without compression. Older or misconfigured CDNs.
When it doesn’t matter:
- Modern HTTP-served pages with brotli. The compression is doing the heavy lifting.
- Pages already auto-generated. SSR frameworks output reasonably tight HTML.
- Development. Always use formatted HTML in dev. Minify for production.
Aggressive minification options
Beyond the safe defaults, minifiers offer aggressive options:
- Remove optional
<html>,<head>,<body>tags. HTML5 allows these to be omitted entirely; the spec inserts them implicitly. Some minifiers strip them. Saves a few bytes; can confuse some downstream parsers. - Remove
type="text/javascript"from<script>. It’s the default; the attribute is redundant. Universally safe. - Remove
type="text/css"from<style>. Same. - Remove protocol from URLs (
http://example.com/foo→//example.com/foo). Saves bytes; legitimate; but breaks if you ever need to view the file locally (no protocol means it triesfile://). - Convert
<!DOCTYPE html>to lowercase. Tiny saving; mostly cosmetic.
Per-element whitespace rules
The minifier needs an internal model of element behavior:
Element type Rule
-------------------------------
Block (default) Strip whitespace at start/end of children
Inline Preserve significant whitespace
Pre-formatted Preserve all whitespace
Whitespace-aware (input, textarea) Preserve all whitespace
A minifier that gets this wrong produces visible rendering changes. The popular html-minifier-terser library has all of this baked in; rolling your own from regex is asking for trouble.
Common pitfalls
- Stripping all comments. IE conditional comments break IE rendering. Use a minifier with the “ignore conditional comments” option turned on.
- Re-minifying. Most minifiers are idempotent; some have bugs and produce subtly different output. Don’t double-minify.
- Minifying templates. Server-side template syntax (Jinja2, ERB, Twig) inside HTML may be mangled. Use template-aware minifier or minify after rendering.
- Quoting around attribute values. Removing quotes is safe only when the value has no spaces, no
<,>,",',,/,=`. Easy to mis-implement. - Treating
<as a tag start in<script>content. It’s literal text inside scripts. Naive parsers get confused.
For the inverse direction, see HTML formatter. For other minification needs, see CSS minifier, JSON minifier.