About HTML Minifier
Indentation and comments help the people who write HTML and do nothing for the people who load it. Minifying removes them. It is useful when HTML goes somewhere size or line count matters, such as an email template, a CMS field, a JSON string, or a hand-built page with no build tool.
The danger with minifying HTML is that some whitespace matters. Collapsing every gap between tags will join <b>one</b> <i>two</i> into "onetwo", and it will wreck a code sample or a form's default text. So this tool is deliberately conservative. It removes only whitespace that cannot affect the page in normal rendering.
You see the original and minified sizes, and the percentage saved, as soon as you paste. That is worth a glance, since well-written HTML often saves less than you would expect.
How it works
A tokenizer reads the source into tags, text, comments and raw content, and understands quoted attributes, so title="a > b" is not cut short. Content inside <script>, <style> and <textarea> is treated as a single opaque value and copied exactly, and nothing in it is read as markup. <pre> and any element with an inline white-space: pre style are copied byte for byte, including their nested tags.
Everywhere else, runs of whitespace in text become one space. The space is then removed only where it sits against a block-level tag, such as the edge of a paragraph or the gap between two <div>s. Between inline elements it stays, and so a space between words can never disappear. Non-breaking spaces are left alone, whether written as a character or as .
Spaces and line breaks between attributes are reduced to one, and attribute values are never altered. Comments are removed by default, apart from Internet Explorer conditional comments, which some email templates still depend on. A comment between two words is taken out without gluing them together.
How to use HTML Minifier
- Paste your markup on the left, load it with Open file, or press Try an example to see what is protected.
- Check the minified source on the right, and the sizes below it.
- Open Options and turn off Remove comments if you want to keep them.
- Take the result with Copy result, or save it using Download .html.
Comment and indentation gone, pre and script untouched
<div>
<!-- nav -->
<p> Soup <b>and</b> <i>bread</i> </p>
<pre>
4.00
2.50</pre>
<script>
const s = "a b";
</script>
</div><div><p>Soup <b>and</b> <i>bread</i></p><pre>
4.00
2.50</pre><script>
const s = "a b";
</script></div>A single space survives between the <b> and <i> elements, because removing it would join the words. The size went from 144 to 114 bytes, a saving of 20.8 percent. The spaces inside the JavaScript string are untouched.
When it helps
- Fitting an HTML email template under the size where some mail apps start clipping messages.
- Pasting markup into a CMS or form builder field where line breaks cause stray gaps.
- Storing a snippet of HTML inside a JSON value or a configuration file on a single line.
- Trimming a hand-written landing page that has no build step of its own.
Tips for a better result
- Do not expect huge savings on a site that already serves compressed files. Gzip and Brotli squeeze repeated whitespace very well, so the real gain over the network is usually much smaller than the figure shown.
- Keep your readable original. Run HTML Beautifier if you lose it, but your comments will not come back.
- Minify the script and style blocks with a dedicated JavaScript or CSS tool before pasting here, if you want them smaller too.
Good to know
- JavaScript and CSS inside the page are not minified. They are kept exactly as written on purpose, since doing this safely needs a full parser for each language.
- Whitespace rules set by a stylesheet cannot be seen here. If a class applies
white-space: preto an element, its formatting will be collapsed. Use an inline style, or a<pre>element, to protect it. - A block element restyled with
display: inlinecould lose a visible space beside it, since it is treated as a block. - It does not remove optional tags or attribute quotes, or shorten attribute values. Those can save a little more but carry more risk.
- It works on one document or snippet of text at a time, and does not check whether your HTML is valid.
Questions people ask
Will minifying change how my page looks?
It should not in normal use, because only whitespace that browsers already ignore is removed, and text, form values and preformatted blocks come through unchanged. The exceptions are stylesheets that change how whitespace or display works, as listed in the limits above.
Why is there still a space between some tags?
Between inline elements such as <span>, <a> and <b>, a space is visible on the page as a gap between words. Removing it would change your text, so one is kept. Custom elements are treated the same way, since the tool cannot know how they display.
Does it remove my JavaScript comments?
No. The contents of a <script> block are not read at all, so the "//" comments, the line breaks and the spacing all stay. Removing them safely would mean understanding JavaScript strings and regular expressions, and a small mistake there breaks a page.
Are conditional comments kept?
Yes. Comments starting with "<!--[if" are kept even with Remove comments on, because they are instructions and not notes. Old versions of Outlook still read them, so removing them can break email layouts.
What to use next
- HTML Beautifier: to make minified HTML readable again when you need to edit it
- HTML to Text: if you want a plain text version of the same content
- Markdown to HTML: to generate compact markup from Markdown to minify
Last reviewed . Read our editorial policy.