Every byte counts when you are serving HTML to millions of requests. HTML minification is one of the cheapest performance wins in front-end development — but developers are often unsure what it actually does to their code and whether it is safe to apply in production.
This article explains what a minifier removes, what it keeps, and the few edge cases where you need to be careful.
What Minification Removes
A minifier strips content that browsers ignore during parsing. Here is what typically gets removed:
Whitespace between tags. Browsers collapse consecutive whitespace into a single space in most contexts. The newlines and indentation between your tags serve human readers, not the parser.
<!-- Before --> <div> <p> Hello </p> </div>
<!-- After --> <div><p> Hello </p></div>
Enter fullscreen mode
Exit fullscreen mode
HTML comments. <!-- This is a comment --> is parsed and discarded by the browser. There is no runtime value. Some build tools strip them; a minifier will too.
Optional closing tags.…