How to use the HTML comment remover
- Paste your HTML into the HTML box.
- Tick Keep IE conditional comments if you need them (off by default).
- Keep Remove lines left empty ticked to delete blank lines where comments used to be.
- Copy HTML without comments and check Comments removed and Bytes saved.
What it does and when to use it
Comments are useful while you build a page: notes for colleagues, disabled blocks of markup, build stamps, reminders. Before a page goes live they are just weight, and sometimes a leak, such as a TODO that mentions a client or an old endpoint. This tool removes them in one pass so you can ship clean markup without editing each one by hand.
A plain search-and-replace for <!-- to --> is risky. It can match text inside a script or a CSS string, and an unclosed comment can make it swallow half the page. Here the HTML is read token by token, as a browser reads it, so only real comments go.
If you want a smaller page overall, the HTML Minifier removes comments and collapses whitespace in one step. To strip comments together with inline styles, classes and Word markup, use the HTML Cleaner. To count comments before you remove them, use the HTML Tag Counter.
How it works
The input is split into tokens: tags, text, doctype and comments. Every token is copied to the output unchanged except comments, which are skipped, unless the option to keep conditional comments is on and the comment starts with [if or ends with <![endif]. Content of script, style and textarea is one raw text token, so nothing inside it is touched. With Remove lines left empty on, lines that contain only spaces after removal are deleted, and runs of three or more line breaks are reduced to one blank line. Everything runs in your browser.
Worked examples
Inline and multi-line comments. <p>a<!-- x --></p><!-- multi + new line + line -->b becomes <p>a</p>b, with 2 comments removed.
The sample page. It contains a build stamp, an IE conditional comment, a multi-line TODO, and a script with a string that looks like a comment. The result reports Comments removed: 3 and Bytes saved: 130, and the script survives exactly:
<script>const a = "<!-- not a comment -->";</script>
Keeping the conditional comment. With Keep IE conditional comments ticked, the head still contains <!--[if lt IE 9]><script src="html5shiv.js"></script><![endif]-->, and only 2 comments are removed.
Limits and tips
- Comments in CSS (
/* */) and JavaScript (//) are not HTML comments and are not removed. Use a CSS or JavaScript minifier for those. - Keep a copy of the original if the comments document anything you may need later.
Frequently asked questions
Why remove comments at all?
Why is the comment inside my script not removed?
What are IE conditional comments?
Will removing comments break my page?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.