Remove Comments from Code | HTML, CSS and JS Guide

All posts

Clean up HTML, CSS, and JavaScript by removing comments before production. Free online comment remover with examples for each language.

Guide to removing comments from HTML, CSS, and JavaScript before production minification and deployment

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.

3

Web Syntax Families

10–30%

Typical Comment Share

/*! */

License Preserve Marker

0

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:

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.

Diagram showing HTML, CSS, and JavaScript comment blocks flowing through a strip step toward minified output

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.

  1. Open the Remove Comments tool and choose HTML, CSS, or JavaScript (TypeScript uses the same comment rules as JavaScript).
  2. Paste your snippet or upload a small file with the file control on the input panel.
  3. Review the Result panel. Empty lines left by removed line comments are trimmed for readability.
  4. Copy the output into your repo, CMS, or email template. Keep the original commented file in version control.
  5. 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

Tips and common mistakes

Related free tools

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.

Browse all posts