HTML Minifier
WM-18Paste HTML to remove comments and collapse whitespace. Content inside pre, textarea and script is left alone, and Size saved shows the bytes you gained.
Runs in your browser. Nothing is uploaded.
HTML
Minified HTML
How to use it
- Paste your HTML into the input box, or open an .html file (up to 5 MB).
- Choose Minify and read the result in the output box.
- Copy it or download it as an .html file.
- Choose Size saved to compare the byte counts before and after.
What it removes and what it keeps
Comments are removed, except conditional comments such as <!--[if IE]>. Runs of spaces, tabs and line breaks become a single space, and whitespace between block-level tags such as div, p, ul and table is removed. Spacing inside tags is tidied, so two spaces between attributes become one. Text inside pre, textarea and script is copied as written, and the CSS inside style is minified with the same rules as the CSS Minifier.
Why some whitespace stays
Between two inline elements, for example a link followed by a bold word, one space can be visible to the reader. Removing it would join the words, so a single space is kept. The result is a little larger than what a more aggressive tool produces, but the page should render the same. The tool does not remove optional end tags, drop attribute quotes or minify the JavaScript inside script tags.
Checks that run first
The minifier reports an unclosed comment, an unclosed declaration, and a script, style, pre or textarea that never closes, each with its line number. It does not validate the HTML in general. A missing closing tag on a div is not an error here, because the tool never needs the tree to do its work. Use an HTML validator for that.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Comment starting at line 4 is never closed (missing -->). | An HTML comment was opened with <!-- but never ended. | Add --> where the comment should end. |
<script> opened at line 9 is never closed. | A script tag has no matching </script>, so the tool cannot tell where its code ends. | Add the closing </script> tag. The same message appears for style, pre and textarea. |
Questions people ask
How do I minify HTML?
Paste the markup, choose Minify and copy the result. Comments disappear and whitespace between tags collapses. Use Size saved to see how many bytes that is.
Will it break my page layout?
The rules are chosen to avoid that. Whitespace is removed only where it cannot be shown, and one space is kept between inline elements. Text in pre and textarea is untouched. Still, preview the page after a new build, especially if you use CSS white-space settings.
Does it minify inline JavaScript?
No. The contents of script tags are copied exactly. Inline style blocks are minified as CSS. For JavaScript, use a JavaScript minifier in your build tool.
Are IE conditional comments kept?
Yes. Comments that start with <!--[if are kept as written. All other comments are removed, so do not put text you need, such as a license note, in an ordinary comment.
How much smaller will my HTML get?
It depends on how much indentation and how many comments you have. Server compression such as gzip works on top of that and is not measured here.
Can I restore the original formatting?
Not exactly. The HTML Formatter on this site can re-indent minified markup, but removed comments and the original spacing are gone. Keep the readable source and minify a copy.
About this tool
Removes comments (conditional comments such as <!--[if IE]> are kept), collapses runs of whitespace, removes whitespace between block-level tags and tidies spacing inside tags. Text inside <pre>, <textarea> and <script> is left as written, and <style> is minified. Whitespace between inline elements is kept as one space, because removing it can change how the page looks, so the result is not as small as it could be. It does not remove optional tags or attribute quotes, shorten attribute values or minify JavaScript. An unclosed comment, or a <script>, <style>, <pre> or <textarea> that never closes, is reported with its line.