Skip to content

HTML Comments: How to Comment in HTML

Short answer: start a comment with <!-- and end it with -->, like <!-- This is a comment -->. The browser skips everything in between, so it doesn’t show on the page. A comment can span many lines, and you can wrap it around code to switch that code off. In most editors, Ctrl+/ (Cmd+/ on a Mac) adds or removes one for you. Most examples below are live: edit the code and the preview changes.

The quick answer

An HTML comment is a note in your code that the browser doesn’t show. It starts with <!-- (less-than, exclamation mark, two dashes) and ends with --> (two dashes, greater-than). Put a space after the opening and before the closing marks, so it’s easy to read.

A comment and a paragraph
Comments that label sections

What comments are good for:

  • Labels for the parts of a long page, like “Header” or “Price list”, so you can find them later.
  • Marking where a block ends, like <!-- /Main content -->, when the opening tag is far up the file.
  • Notes for yourself or your teammates: why something is done a certain way, or what’s left to do.
  • Switching code off for a while without deleting it. That’s the next section.

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor.

Comment out code

“Commenting out” means wrapping working code in a comment so the browser ignores it. It’s handy when you want to hide a section for now, test the page without it, or keep an old version close by. Remove the two marks and the code is back.

Comment out a block
Comment out part of a line

A comment doesn’t have to fill a whole line. In the second example, the words “, and Saturday mornings” are commented out in the middle of a sentence, and the chat link is switched off. Try deleting <!-- and --> around the link and watch it come back.

Multi-line comments

HTML has only one kind of comment, and it can be as long as you like. There’s no separate syntax for a multi-line comment: open it with <!--, write as many lines as you need, and close it with -->. Putting the marks on their own lines makes long comments easier to spot.

A comment over several lines

The keyboard shortcut

You don’t have to type the marks by hand. Select one or more lines and press Ctrl+/ (on a Mac, Cmd+/). The editor wraps them in <!-- -->. Press it again and the comment is removed, so the same keys switch code off and on. With nothing selected, it toggles the line your cursor is on.

EditorToggle commentBlock comment
VS Code Ctrl+/ (Mac: Cmd+/) Shift+Alt+A (Mac: Shift+Option+A, Linux: Ctrl+Shift+A)
Sublime Text Ctrl+/ (Mac: Cmd+/) Ctrl+Shift+/ (Mac: Cmd+Option+/)
WebStorm and other JetBrains editors Ctrl+/ (Mac: Cmd+/) Ctrl+Shift+/ (Mac: Cmd+Option+/)

In an HTML file both shortcuts give you <!-- -->, because HTML has only one kind of comment. The block version is useful when you want to comment out part of a line. Editors also notice where your cursor is: inside <style> they use the CSS comment /* */, and inside <script> the JavaScript one. Shortcuts can differ if you changed your keyboard settings or use a keyboard layout where / needs Shift.

Mistakes that break comments

When a comment goes wrong, you usually see stray text like --> on the page, or a whole part of the page goes missing. These are the usual causes.

You can’t put a comment inside a comment

A comment ends at the first --> the browser finds. So if you comment out code that already has a comment in it, the inner --> closes the outer comment too early, and everything after it shows up again, including the leftover -->.

A comment inside a comment
A missing dash hides the rest

To comment out code that has comments in it, take the inner ones out first, or change their marks for a moment (for example <!~~ links go here ~~>, which is plain text inside the outer comment). The second example shows the opposite problem: -> with one dash doesn’t close the comment, so the browser keeps looking for --> and hides everything after it. If half your page disappeared, look for an open comment just above the gap.

<!--> and <!---> close straight away

If a comment starts with <!--> or <!--->, the browser treats it as a complete, empty comment. Whatever you meant to hide comes after it, so it shows on the page. Always leave something, ideally a space, between <!-- and the first >.

Two empty comments and a real one

Two dashes in the middle

  • -- inside a comment works in browsers, so <!-- a -- b --> is hidden as you’d expect. It used to be against the rules in older HTML, and it’s still not allowed in XHTML and XML, so if your HTML is also read as XML (some feeds and email tools do that), avoid it.
  • --!> closes a comment too. Browsers treat it like -->, so a note like <!-- wow --!> ends right there.
  • Long dash lines like <!-- ---------- --> work in browsers, but for the reasons above, a line of = or * is the safer divider.

A comment can’t go inside a tag

Comments only work between tags, never inside one. In <p <!-- intro --> class="intro"> the browser reads <!--, intro and -- as odd attribute names, and the > at the end of --> closes the tag early. The rest of the tag ends up as text on the page. Put the comment on the line above the tag instead.

Inside the tag vs above it

The same goes for attribute values: title="<!-- note -->" isn’t a comment, it’s just the text of the title. And a comment needs the exclamation mark with no space: <-- note --> and < !-- note --> both show up on the page. The HTML Validator helps when a page looks wrong and you can’t see why.

Comments aren’t private, and they add bytes

The browser doesn’t show comments, but it still downloads them. Anyone can read them: right-click the page and choose View Page Source, or press Ctrl+U (Cmd+Option+U in Chrome on a Mac). They also show in the browser’s developer tools.

💡 Never put passwords, keys, private notes, customer details or “we’ll fix this security hole later” in an HTML comment. If a note mustn’t reach visitors, keep it in a file that isn’t published, or use your template language’s own comment, which is removed before the page is sent.

Every comment is also sent to every visitor on every page load. A few short notes don’t matter, but big blocks of commented-out code do add up. Keep your commented source file for yourself, and publish a copy without them: the HTML Minifier removes comments and extra spaces, and the HTML Cleaner strips comments from HTML you’ve pasted from somewhere else. Both keep Outlook’s conditional comments, which you’ll meet next.

Conditional comments in HTML email

The classic Outlook desktop app on Windows draws emails with Word, which gets a lot of modern HTML and CSS wrong. So email builders use special comments that only that Outlook reads: code inside <!--[if mso]> … <![endif]--> is only used by Outlook, and every other app sees a normal comment and skips it. The opposite, code for everyone except Outlook, uses <!--[if !mso]><!--> and <!--<![endif]-->.

Outlook-only and everyone-else content

The preview above is a browser, so it shows only the second line, just like Gmail or Apple Mail would. The new Outlook for Windows and Outlook on the web don’t use Word, so they skip the [if mso] part like any other comment and show the rest. Internet Explorer had its own version (<!--[if IE]>), but it stopped reading them in version 10, and IE itself is retired, so you can delete those from old websites. To see how an email looks and catch common problems, open it in the HTML Email Viewer.

Comments in CSS and JavaScript

<!-- --> only works in HTML. Inside a <style> block and in .css files, use /* */. In JavaScript, use // for one line and /* */ for several. This example shows why it matters: the HTML-style comment in the CSS doesn’t switch anything off.

/* */ works in CSS, <!-- --> doesn’t
Comments in JavaScript
<script>
  // One line: everything after // is skipped.
  const total = 3; // it can go at the end of a line too

  /* More than one line:
     everything between the markers is skipped. */
</script>
WhereComment syntaxNotes
HTML <!-- note --> One kind, for one line or many. Only between tags.
CSS (<style>, .css files, style="") /* note */ For one line or many. CSS has no // comment: the browser sees broken CSS and drops the next rule or declaration (sometimes the one after it too).
JavaScript, one line // note Everything after // on that line is skipped.
JavaScript, several lines /* note */ Same as CSS. Like HTML comments, these can’t be nested.

You may see old pages with <!-- right after <script> or <style>. That trick hid code from browsers of the 1990s that didn’t know those tags. Today it does nothing useful (in a script it only comments out the rest of that one line), so you can leave it out.

Hide something the right way

Comments are for notes about the code, not for hiding content from visitors. If something should be on the page but not visible right now, like a message that appears later or a section you’re still writing, hide it with HTML or CSS:

The hidden attribute and display: none
  • The hidden attribute hides an element from everyone, screen readers included. Remove the attribute to show it again.
  • display: none does the same from CSS, so you can hide something only on phones with a media query, or switch it with a class.
  • Neither one is secret. Like comments, hidden elements are in the page source. Content people mustn’t see should never be sent to the browser at all.

Questions people ask

How do I write a comment in HTML?

Start it with <!-- and end it with -->, like <!-- This is a comment -->. The browser skips everything in between, so nothing shows on the page. Put it between tags, never inside one.

How do I comment out multiple lines in HTML?

Put <!-- on the line before the code and --> on the line after it. HTML has only one kind of comment and it can span as many lines as you like. Just make sure the code you wrap doesn’t already contain a comment, because the first --> ends the whole thing.

What is the shortcut to comment in HTML?

In VS Code, Sublime Text and JetBrains editors like WebStorm, select the lines and press Ctrl+/ (Cmd+/ on a Mac). The editor wraps them in <!-- -->, and pressing it again removes the comment. Inside a <style> block it uses /* */ instead, and inside <script> the JavaScript comment.

Can I put a comment inside another comment?

No. A comment ends at the first --> the browser finds, so an inner comment closes the outer one early and the rest shows on the page, including a stray -->. Remove the inner comments first, or change their marks for a moment, for example to <!~~ note ~~>.

Why is my HTML comment showing on the page?

Usually one of these: the ! is missing or there’s a space after the < (it must be exactly <!--), the comment is nested inside another one, it starts with <!--> or <!---> (those close straight away), or it sits inside a tag like <p <!-- note --> class="x">. If part of the page has vanished instead, look for a comment that never got its closing -->.

Can visitors see my HTML comments?

Not on the page, but anyone can read them with View Page Source (Ctrl+U) or the browser’s developer tools. Never put passwords, keys or private notes in a comment. If a note mustn’t reach visitors, keep it out of the published file.

Do HTML comments slow down my page?

They’re downloaded with the page, so they add a little to its size. A few short notes don’t matter, but large blocks of commented-out code add up. An HTML minifier removes comments from the copy you publish, while you keep the commented original.

How do I write comments in CSS and JavaScript?

CSS uses /* comment */, in a <style> block, a .css file or a style attribute. JavaScript uses // for one line and /* */ for several. <!-- --> doesn’t work in CSS: the browser skips the marks and still uses the rules between them.

What does <!--[if mso]> mean?

It’s a conditional comment for HTML email. The classic Outlook desktop app on Windows reads the code inside <!--[if mso]> … <![endif]--> and every other email app treats it as a normal comment. Internet Explorer had similar <!--[if IE]> comments, but IE stopped reading them in version 10 and is now retired.

Keep going