HTML Comment Syntax: Comment Out Markup, CSS and JS

November 11, 2026 · 9 min read

HTML Comments: How to Comment Out Markup, CSS and JavaScript

An HTML comment starts with <!-- and ends with -->, and the browser renders nothing between the two markers. You use comments to leave notes in your markup or to switch elements off while you test. Below you'll find multi-line blocks, the rules that break comments, the different syntax inside style and script, and why comments stay public.

How Do You Write an HTML Comment?

Here's how to comment in HTML: type <!--, then your note, then -->. The browser parses the comment but displays none of it. A typical note inside a page header looks like this:

<header class="site-header">
  <!-- Logo links back to the home page -->
  <a href="/" class="logo">Acme</a>
</header>

On the page, you see only the Acme logo link. The note sits in the source for whoever edits the file next. To show the comment markers as visible text instead, escape them inside an HTML code block.

If you searched for an HTML line comment, the honest answer is that HTML doesn't have one. There's a single syntax, and a one-line comment is simply a short one. People also call it the HTML comment tag, but MDN's guide to HTML comments is clear that a comment isn't an element. It has no attributes and no closing tag. The parser turns it into a Comment node in the DOM, which is why it still shows up in DevTools. Our explainer on what a tag in HTML is untangles tags, elements and attributes.

Writing notes in a Markdown file instead? That case has its own quirks, covered in our guide to hidden comments in Markdown.

Writing Multi-Line Comments and Comment Blocks in HTML

One opener and one closer can wrap as many lines as you like. Line breaks, indentation and blank lines inside the markers are all fine. That's the whole trick behind an HTML multiline comment. Teams use it for documentation blocks at the top of templates:

<!--
  Template: product card
  Used on: /shop and /search pages
  Needs: a .price element for the sale badge
-->
<div class="product-card">
  <h3 class="title">Desk lamp</h3>
  <p class="price">49.00</p>
</div><!-- end .product-card -->

The browser shows a card with a heading and a price. The four-line block and the end marker stay out of sight.

End markers like <!-- end .product-card --> earn their keep in long templates. When a closing </div> sits 200 lines below its opener, a short label tells you which block just ended. Keep them brief, because every HTML block comment ships to every visitor (more on that below).

How Do You Comment Out an Element or a Whole Section?

To comment out one element, wrap just that element. Here the Blog link disappears from a navigation bar while the others stay:

<nav class="main-nav">
  <a href="/">Home</a>
  <!-- <a href="/blog">Blog</a> -->
  <a href="/about">About</a>
</nav>

The page now shows Home and About. Remove the two markers and Blog comes back, untouched.

For a whole section, put the opener on its own line above the block and the closer on its own line below it.

<!--
<section class="promo-banner">
  <h2>Winter sale</h2>
  <p>Twenty percent off until Friday.</p>
</section>
-->

Nothing from the banner renders, and its heading drops out of the page outline. The markup still downloads with the page, though. Before you wrap a large block, check that it doesn't already contain a comment, because that's the bug the next section explains. On a messy page, tidy the indentation first with our HTML formatter so you can see exactly where each block opens and closes.

Why HTML Comments Can't Be Nested, and Other Sequences That Break Them

The WHATWG HTML standard defines the HTML comment syntax. It also limits the text inside a comment. That text must not do any of the following.

  • start with > or ->
  • contain <!--, --> or --!>
  • end with <!-

Break one of these and the parser recovers in a way you probably didn't intend. The classic case is commenting out a block that already holds a comment:

<!-- Hide the promo for now:
<section class="promo-banner">
  <!-- TODO: swap the image -->
  <h2>Winter sale</h2>
</section>
-->

The comment ends at the first -->, right after the TODO note. The spec calls this a nested-comment parse error: everything after that point is treated as markup again. So the Winter sale heading renders, and the final --> appears on the page as plain text. A stray arrow on the rendered page is the telltale sign.

Two smaller traps work the same way. --!> closes a comment just like --> does. And <!--> is an empty comment that closes on the spot, so the text you meant to hide renders. A double hyphen in the middle of a comment is allowed in HTML today, but MDN notes it's forbidden in XML-based markup such as SVG and MathML.

The fix is simple: remove or rewrite any inner comments before you wrap a block. Try it here. The preview renders the raw HTML, so you can watch a working comment disappear and a nested one leak.

<nav> <a href='#'>Home</a> <!-- <a href='#'>Blog</a> --> <a href='#'>About</a> </nav>

<!-- TODO: add a pricing link -->

<p>The Blog link and the TODO note are hidden.</p>

<!-- outer <!-- inner --> this text leaks -->

<p>The line above shows what a nested comment does.</p>

44 words276 characters13 lines
Markdown

CSS and JavaScript Comments Inside an HTML File

Inside <style> and <script> elements, the HTML parser treats the content as raw text and doesn't recognize HTML comments there. So you switch to each language's own syntax.

CSS has exactly one form: /* ... */. It can span lines and it can't be nested. The MDN page on CSS comments lists it as the only comment syntax, so // isn't a CSS comment. JavaScript gives you two everyday forms: // comments out the rest of a line, and /* ... */ wraps a block. If the slash-star form looks familiar, it's the same one you'll find in C, C++ and Java.

Here's one small page that uses all three systems:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Comment demo</title>
  <style>
    /* Brand color for every link */
    a { color: #0b57d0; }
    /* .promo { display: none; } */
  </style>
</head>
<body>
  <!-- Main navigation -->
  <nav><a href="/">Home</a></nav>
  <script>
    // Log once the script runs
    console.log("ready");
    /* Disabled until the API is live:
    loadStats();
    */
  </script>
</body>
</html>

The links turn blue, the .promo rule never applies, and the console logs ready. The loadStats() call never runs.

One trap here: <!-- and --> inside a style block don't comment anything out. The CSS syntax spec discards those two markers and keeps every rule between them.

Keyboard Shortcuts to Toggle Comments in VS Code and Other Editors

You rarely need to type the markers by hand. VS Code's default keyboard shortcuts include two toggles.

CommandWindowsmacOSLinux
Toggle Line CommentCtrl+/Cmd+/Ctrl+/
Toggle Block CommentShift+Alt+AShift+Option+ACtrl+Shift+A

Select the lines you want to switch off and press the toggle. Press it again to restore them. In plain markup, expect <!-- and --> around your selection.

Inside a style or script block, glance at what the toggle inserts before you save. You want /* */ or // there, never <!--. Sublime Text and JetBrains IDEs have their own toggle-comment commands, so check the keymap in whichever editor you use.

Are HTML Comments Visible to Visitors?

Not on the rendered page, but they're far from private. Every comment ships inside the HTML, so anyone can read it with View Page Source or the Elements panel in DevTools. Scrapers and crawlers download the same text.

Treat every comment as public. Never leave passwords, API keys, internal hostnames or notes about unfixed security bugs in them. Commenting out a form or an admin link hides it from view, not from anyone who reads the source.

Comments also cost bytes on every page load. Many HTML minifiers and build tools can strip them before deploy, but often only when you switch that option on. Check your build settings rather than assuming it happens.

We prefer deleting dead markup and letting Git keep the history, rather than shipping large commented-out blocks to production. The trade-off is that the reasoning behind a removal then lives only in your commit messages, so those need to be good.

Conditional Comments and the Old Script-Hiding Trick

You'll still find two legacy patterns in old templates. Internet Explorer's conditional comments, such as <!--[if IE]>, loaded markup only in IE. Microsoft's own compatibility docs say IE10 dropped support in standards and quirks modes, so every current browser treats them as ordinary comments.

The second pattern wraps a script's entire content in <!-- and --> to hide it from browsers that predated JavaScript. MDN calls this a legacy feature you shouldn't rely on. You can safely delete both when you meet them.

Comments are easy to write and easy to break. Keep one rule in mind: an HTML comment ends at the first closing marker, so never nest one. Use slash-star inside CSS, double slash or slash-star inside scripts, and assume every note is public. If you write in Markdown and export to HTML, see how to convert Markdown to HTML, or test raw HTML in the editor.