Remove HTML comments safely, without touching scripts

Paste HTML and every comment is removed, including multi-line notes, while comment-like text inside script and style blocks is left alone; you can keep old IE conditional comments and tidy up the blank lines left behind.

Live demo · real result from this tool

HTML without comments

<!DOCTYPE html><html><head></head><body><p>Hello</p><script>const a = "<!-- not a comment -->";</script></body></html>
Comments removed3
Bytes saved
130

Processed on your device — nothing is uploaded.

How to use the HTML comment remover

  1. Paste your HTML into the HTML box.
  2. Tick Keep IE conditional comments if you need them (off by default).
  3. Keep Remove lines left empty ticked to delete blank lines where comments used to be.
  4. 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?
Comments are sent to every visitor and anyone can read them with View Source. They often contain internal notes, TODOs, old code, build details or names that were never meant to be public. Removing them also makes the page a little smaller.
Why is the comment inside my script not removed?
Inside script and style, the browser treats everything as code, not HTML, so text that looks like a comment is really part of a JavaScript string or CSS. Removing it would change your code. The tool reads HTML the way a browser does and only removes real comments.
What are IE conditional comments?
Comments such as if lt IE 9 that old versions of Internet Explorer read as instructions, for example to load a script only in IE 8. Modern browsers ignore them, so most sites can delete them. Tick "Keep IE conditional comments" if you still support those browsers or the markup is used in Outlook email templates that rely on similar comments.
Will removing comments break my page?
Normally not, because browsers ignore comments. Check two cases first. Some templating systems and frameworks use comments as markers, such as server-side includes or hydration hints. And some email templates rely on conditional comments for Outlook.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close