Comments help humans read source code, but they add bytes and can leak internal notes when you ship to production. To remove comments from code in HTML, CSS, and JavaScript, you need to know each language's comment syntax, what must stay (license headers, IE conditionals), and why naive search-and-replace breaks strings and regexes. This guide covers comment types, safe workflows, build-tool options, and a free browser Remove Comments tool that strips comments locally without uploading your source.
Web Syntax Families
Typical Comment Share
License Preserve Marker
Server Upload (Our Tool)
Key facts: removing comments from code (quick reference)
Use this block when you need a concise answer for search or AI summaries:
- HTML: Comments use
<!-- ... -->. Browsers ignore them at parse time. Conditional comments for legacy IE use a different shape and may need to stay if you still support those browsers. - CSS: Only block comments
/* ... */exist. A leading!inside the block (/*! ... */) is a widespread convention for license text that minifiers often keep. - JavaScript: Line comments
//and block comments/* ... */. The lexer must not treat//inside strings, template literals, or regex literals as comments. - Comment removal vs minification: Stripping comments is one step. Full minification also collapses whitespace, may shorten names (JS), and can change CSS structure. Use a parser-based minifier for production bundles.
- Legal notices: MIT and similar licenses often require retaining copyright text. Terser, cssnano, and peers can preserve
/*!or@licensecomments when configured (Terser docs). - Privacy: Our Remove Comments tool runs in your browser. Paste or upload snippets locally; nothing is stored on a server.
Why remove comments before production?
Comments do not execute, but they still ship in plain source unless you strip them. That matters for page weight, caching, and security hygiene.
- Smaller payloads: Documented codebases and JSDoc-heavy files can carry more comment characters than logic. Removing comments is often the first minification win.
- Fewer leaked hints: TODOs, author names, internal URLs, and staging API notes sometimes live only in comments. Production HTML and JS are public.
- Cleaner embeds: Email templates, ad snippets, and single-file demos benefit from compact markup without editorial noise.
- Prep for further optimization: After comments, teams run HTML, CSS, or bundler minifiers for whitespace and structure.
- Not a substitute for secrets: Never put passwords or API keys in comments (or anywhere in client-side code). Removal does not fix exposure if the value is still in a string.
Figure 1: Comment syntax differs by language; stripping must respect strings, regexes, and license markers.
Comment syntax in HTML, CSS, and JavaScript
HTML comments
<!-- Navigation placeholder --> <nav class="main">...</nav> <!--[if IE 9]> <link rel="stylesheet" href="ie9.css"> <![endif]-->
Standard comments are safe to remove when you no longer need the note. IE conditional comments are a special case: they are comments to modern browsers but instructions to old IE. Drop them only when IE support is out of scope.
CSS comments
/* Layout tokens */
.header { margin: 0; }
/*!
* SPDX-License-Identifier: MIT
* (c) Example Corp
*/
There is no // line comment in standard CSS. Minifiers that follow the /*! convention leave license blocks unless you explicitly discard all comments.
JavaScript comments
// API base URL for staging (remove before release)
const url = "https://api.example.com"; // not a comment inside the string
const pattern = /https?:\/\//; // regex is not a line comment
/** @param {string} name */
function greet(name) {
console.log(name);
}
JavaScript is the riskiest language for regex-based comment removers because / can start a regex or a division operator, and // inside template literals must stay literal.
Methods compared: how to strip comments
| Approach | Best for | HTML / CSS / JS aware | License comment control |
|---|---|---|---|
| Online Remove Comments (browser) | Quick cleanup, snippets, QA samples | Per-language modes | Manual review (strips matched patterns) |
| IDE find/replace (regex) | Tiny files, expert review | Easy to break strings | None by default |
| Terser / esbuild (JS) | Production JS bundles | Parser-based | /*!, @license |
| cssnano / clean-css (CSS) | Stylesheets and design tokens | Parser-based | /*! except rules |
| html-minifier (HTML) | Static pages and templates | Token-aware | Optional IE conditional keep |
Method 1: Remove comments online (free, in your browser)
Use our Remove Comments tool when you want fast results without installing Node or opening a full build pipeline. Processing happens as you type or paste: pick a language, paste source on the left, and read stripped output on the right.
- Open the Remove Comments tool and choose HTML, CSS, or JavaScript (TypeScript uses the same comment rules as JavaScript).
- Paste your snippet or upload a small file with the file control on the input panel.
- Review the Result panel. Empty lines left by removed line comments are trimmed for readability.
- Copy the output into your repo, CMS, or email template. Keep the original commented file in version control.
- For other languages (Python, Go, PHP, SQL, and more), switch the language dropdown; each mode uses comment rules for that syntax.
Before/after (JavaScript):
Input:
// TODO: remove before launch
function init() {
const api = "https://cdn.example.com"; // endpoint
return api;
}
Output (comments removed, strings preserved):
function init() {
const api = "https://cdn.example.com";
return api;
}
Alternative methods
Command line: JavaScript with Terser
npm install -D terser
npx terser src/app.js --compress --mangle --comments /^!/
The --comments /^!/ flag keeps license-style blocks that start with !. Adjust the regexp if your legal team requires broader retention.
Command line: CSS with clean-css CLI
npx clean-css-cli -o dist/app.min.css src/app.css
Most CSS optimizers remove ordinary comments by default. Check your config if you rely on /*! preservation.
HTML in the terminal
npx html-minifier --remove-comments --collapse-whitespace index.html
Validate rendered output in a browser after aggressive HTML compression; whitespace rules can affect preformatted text nodes.
Editor workflows
VS Code and JetBrains IDEs can fold or highlight comments, but bulk deletion via regex is risky for JavaScript. Prefer language-aware formatters or the online tool for one-off strips, and reserve CI minification for release artifacts.
When you should not strip comments
- Source repositories: Git history is for humans. Strip only build outputs (
dist/, minified assets), not authoredsrc/unless your team standard says otherwise. - Generated license bundles: Open-source distributions may require visible copyright. Use minifier comment policies instead of blind removal.
- Server-side-only code: This guide focuses on client-visible HTML, CSS, and JS. Backend comments are still a style choice, not a bandwidth issue.
- Debugging builds: Keep comments (and source maps) in staging until you verify behavior in minified form.
Tips and common mistakes
- Regex vs parser: Pattern-based removers can mis-handle edge cases in JavaScript (regex literals, nested templates). Always diff output and run tests.
- HTML inside script tags: Treat embedded JS as JavaScript, not HTML, when choosing a language mode.
- Python docstrings: Triple-quoted strings at module level are often documentation, not comments. Removing them changes runtime strings if code reads
__doc__. - SQL hints: Some databases treat special comment forms as optimizer hints. Confirm with your DBA before stripping production migrations.
- Size expectations: Comment removal alone may not beat gzip/Brotli on the wire. It still helps raw source leaks and non-compressed embeds.
- Backup: Comment removal is one-way in a working copy. Commit before bulk edits or use a branch.
Related free tools
- Remove Comments - Strip comments for HTML, CSS, JavaScript, and 15+ other languages in the browser.
- HTML Minifier - Shrink markup after comments are gone.
- CSS Minifier - Compress stylesheets for production.
- Remove Whitespace - Further compact code when minifiers are overkill.
Frequently Asked Questions
How do I remove comments from HTML, CSS, and JavaScript online?
Paste your code into the Remove Comments tool, select HTML, CSS, or JavaScript, and copy the result panel. The tool runs locally in your browser and updates output as you edit the input.
Does removing comments change how my code runs?
Comments are ignored by parsers, so removal should not change behavior if strings, regexes, and docstrings are handled correctly. Always test after processing, especially for JavaScript edge cases.
Will license headers survive comment removal?
Our quick remover targets comment patterns and does not special-case license blocks. For production JS and CSS bundles, use Terser or cssnano with a preserve policy for /*! or @license comments.
Is it safe to paste proprietary code into an online comment remover?
Our Remove Comments tool does not upload source to a server; processing stays in your session. For highly sensitive code, use an offline parser-based minifier in your own environment.
What is the difference between removing comments and minifying?
Comment removal deletes annotation text only. Minification also removes whitespace, may shorten identifiers in JavaScript, and can merge CSS rules. Use both steps for smallest client bundles.
Whether you are polishing a landing page, shrinking an inline tracker, or preparing a homework snippet for submission, knowing how to remove comments from code saves bytes and avoids accidental leaks. Start with the free Remove Comments tool for HTML, CSS, and JavaScript, then chain minifiers when you are ready for production.