HTML minifier
Compress HTML and the CSS and JavaScript inside it, in one pass. Content of pre and textarea is left exactly as it is.
Why HTML is the trickiest of the three
In CSS and JavaScript, whitespace outside strings almost never matters. In HTML it sometimes does: a space between two inline elements is a real, visible space. Remove it and <a>link</a> <b>bold</b> renders as "linkbold". So an HTML minifier has to know which gaps it is allowed to close.
This one takes the conservative route. Runs of whitespace collapse to a single space everywhere, and whitespace is removed completely only next to block-level tags — div, p, li, section, table rows and the like — where a browser would not render it anyway. Between inline elements a single space survives, so your text keeps its spacing.
What happens to each part
- Comments are removed, except conditional comments (
<!--[if …]>), which are instructions rather than notes. - Inline
<style>is run through the CSS compressor at standard level. - Inline
<script>is run through the JavaScript minifier, but only when it actually holds JavaScript. Atype="application/ld+json"ortype="text/template"block is left alone, because it is data, not code. <pre>and<textarea>are pulled out before anything else happens and put back untouched. Their whitespace is content.- At maximum level quotes come off attribute values that do not need them, and the slash in self-closing tags goes.
Where the real win is
Honestly: HTML minification is the smallest of the three wins. A typical page saves 10 to 20 percent, and gzip would have found most of that anyway. It is worth doing as the last step of a build, and it is genuinely useful for email templates and embedded widgets where every byte is inlined and there is no gzip at all. It is not worth hand-running on every page edit — let your build or your template engine do it.
If your goal is a faster page, the bigger levers are elsewhere: fewer requests, images in the right format and size, and getting CSS off the critical path. That last one is covered on CSS optimization for speed.
Will minifying break my layout?
It can, if your layout depends on whitespace between inline-block elements — an old grid trick. Collapsing those spaces to one is safe; removing them is not, which is why this tool only removes whitespace around block-level tags. Check the result before shipping it.
Does it touch my JSON-LD structured data?
No. Script blocks with a non-JavaScript type are left as they are, apart from trimming the whitespace at the very start and end. Your schema markup keeps working.
Can I minify a PHP or template file?
Not safely. PHP tags, Twig braces and Blade directives are not HTML and can be split by whitespace changes. Minify the rendered output instead, at the end of your build or through output buffering.
Should I serve minified HTML or use gzip?
Both, and gzip matters more. Enable compression on your server first — that is one configuration line for every page you will ever serve — and treat HTML minification as the extra few percent on top.
Other tools on this site
Read more
- What is CSS minification? – What a minifier removes and what it saves
- CSS optimization for speed – Beyond minification: critical CSS and Core Web Vitals
- What breaks a CSS minifier? – Live test of the constructs minifiers get wrong
- Minifying CSS in WordPress – Which plugin does what, and when it breaks
- Frequently asked questions – Answers to the questions we get most
- Useful links – Reference material on CSS and speed
- About this site – Who makes this and how to reach us