HTML minification: what it removes and what it keeps
How collapsing whitespace and stripping comments shrinks HTML, why script, style and pre are preserved, and when to reach for gzip instead.
What minification actually removes
Source HTML is written for humans, so it is full of indentation, blank lines and comments that the browser ignores when it builds the page. Minification strips exactly that slack. It deletes comments, turns every run of spaces, tabs and newlines into a single space, and removes whitespace that sits purely between two tags because that gap never affects the rendered result. What is left is the same tree of elements and attributes with none of the formatting a developer needs but a browser does not. Nothing about the document's meaning changes; only the bytes spent making it readable are recovered.
Why some content must be preserved
Not all whitespace is decorative. Inside a pre or textarea element, spaces and line breaks are content the user sees, so collapsing them would visibly corrupt the page. Inside script and style, whitespace can be syntactically meaningful and comments may carry directives, so touching them risks breaking behavior. For that reason the minifier detects these four elements, captures each one whole from its opening tag to its closing tag, and passes it through untouched while it compresses everything around it. Conditional comments get the same protection because some browsers still act on them.
Minification versus gzip and brotli
Minification and transfer compression solve overlapping but different problems. Gzip and brotli find repeated byte sequences and encode them compactly on the fly, so a lot of the whitespace minification removes would also be squeezed by the compressor. The value minification adds is the bytes a compressor cannot recover, such as whole comments and redundant structure, plus a smaller payload for the compressor to work on and less to parse once it arrives. In practice the raw byte savings from minifying look larger before gzip than after, but the two stack, and shipping both is the standard for production pages.
Where minified HTML fits in a build
For a static site or template, run minification as a build step so the source you edit stays readable and only the deployed output is compressed. Frameworks and bundlers often include an HTML minify pass, but a standalone tool is handy for one-off files, email templates, snippets you paste into a CMS, or checking how much a page could shrink before wiring it into a pipeline. Because this tool reports the before and after byte sizes, it also doubles as a quick way to judge whether a given document has enough slack to be worth minifying at all.