11 June 2025 · 5 min read
What minification can and cannot safely remove
Whitespace in HTML is significant more often than people expect, and aggressive minifiers know it.
Minification removes bytes that do not affect meaning. The difficulty is that which bytes those are depends on the format, and HTML is much less forgiving than it looks.
JSON: entirely safe
Whitespace between tokens is meaningless in JSON. Removing all of it produces a byte-different, semantically identical document. This is the one case where minification carries no risk at all — parse, re-serialise, done.
CSS: mostly safe
Comments and whitespace around punctuation can go. Beyond that it gets interesting: whitespace is a descendant combinator, so removing the space in .a .b changes which elements match. Inside strings and url() values it is significant. And the space in calc(100% - 20px) is required by the grammar — remove it and the declaration is invalid.
HTML: genuinely risky
Whitespace between inline elements is rendered. Two spans separated by a newline display with a space between them; joined together they display without one.
<span>one</span> <span>two</span> renders: one two <span>one</span><span>two</span> renders: onetwo
This is why a careful minifier collapses runs of whitespace to a single space rather than removing them, and leaves pre, textarea, script and style untouched. It is also why an aggressive minifier occasionally produces a page where words have run together, and nobody notices until a customer does.
A minifier that reduces your HTML by 40 per cent is doing something a careful one will not. Check the rendered output, not just the byte count.
The other HTML traps
- Conditional comments are comments to the parser and instructions to old IE. Some build pipelines still rely on them.
- Removing optional closing tags is legal and makes later edits by hand error-prone.
- Unquoting attribute values is legal until the value contains a space, and minifiers occasionally get the boundary wrong.
- Inline event handlers and JSON-LD blocks contain JavaScript that a naive whitespace pass can break.
It matters less than it used to
Every response worth minifying is also being compressed. Gzip and Brotli are extremely good at repeated whitespace — it costs almost nothing in the compressed stream. Minifying before compressing still helps, but the marginal gain over compression alone is often a few per cent, not the number the byte counter suggests.
Measure the compressed size, because that is what crosses the network. If minification saves 30 per cent uncompressed and 3 per cent after Brotli, the risk calculation looks different.
Tools mentioned