HTML Minifier
Minify HTML by removing comments and collapsing whitespace between tags.
Three rules, applied to the whole document
- Comments are removed, except old Internet Explorer conditional comments, whose text begins with
[if, which are kept. - Whitespace between two tags is deleted. Any run of spaces, tabs or line breaks between a
>and the next<disappears. - Any run of two or more whitespace characters becomes one space, wherever it is. A single line break on its own is left alone.
The sample on the page shrinks from 89 to 60 characters:
Nothing else changes. Attribute quotes, optional closing tags, inline CSS and inline JavaScript are kept exactly as written, apart from the whitespace rules above. The result updates as you type or paste.
Words that run together after minifying
Rule 2 removes the space between inline elements, and browsers render that space. <strong>Total:</strong> <em>42</em> becomes <strong>Total:</strong><em>42</em>, which displays as Total:42. The same happens to links, icons and buttons placed side by side.
After minifying, scan the rendered page for words glued together. Where you find one, move the space inside a tag, as in <strong>Total: </strong>, or use between the elements. Both survive minifying.
Preformatted text and inline scripts
Rule 3 doesn't skip any element, so it reaches inside blocks where whitespace matters:
<pre>and<textarea>: indentation collapses. A code sample with a line indented by four spaces loses the indent and joins the line before it.- Inline
<script>with//comments: an indented line break counts as two or more whitespace characters, so the next line is pulled onto the comment line.var a = 1; // set afollowed byvar b = 2;becomes one line, andvar b = 2;is now part of the comment. - Comment-based markup: comments used as data, such as block markers some CMS editors store in post content, are deleted along with ordinary comments.
If your page has any of these, move scripts into external files, or minify only the template parts that don't contain them.
Frequently asked questions
Does it also minify the CSS and JavaScript inside my HTML?+
No. Inline styles and scripts only go through the same whitespace collapsing as the rest of the page. Minify them separately for a full saving.
Can I use it on templates with PHP, Handlebars or Jinja tags?+
Usually, since template tags are treated as plain text and only extra spaces are touched. Check any template comments, though, because HTML-style comments are removed.
How much smaller will my file get?+
It depends on how deeply the source is indented and how many comments it has. Heavily indented, commented markup shrinks the most; already compact HTML barely changes.
Can I get the original formatting back?+
No, removed comments and indentation are gone. Keep your readable source file and treat the minified output as a build artifact.
Embed this tool on your website
Free to use on blogs, school sites and intranets. Paste this code into your page's HTML; please keep the credit line.
<iframe src="https://utilityapp.lat/embed/dev/html-minifier" title="HTML Minifier" width="100%" height="600" style="border:0;max-width:760px" loading="lazy"></iframe> <p style="font-size:13px"><a href="https://utilityapp.lat/dev/html-minifier">HTML Minifier</a> by UtilityApp</p>