Skip to content

CSS Comments: How to Comment in CSS

CSS has one kind of comment. It starts with /* and ends with */, and the browser skips everything in between, whether that’s a note to yourself or a rule you want to switch off for a while. It works on one line or across many. The // comment you may know from JavaScript isn’t valid CSS, and it quietly breaks whatever comes right after it.

How to write a CSS comment

Put your note between /* and */. It can go above a rule, at the end of a line, or anywhere a space could go:

A comment above a rule

The comment doesn’t show on the page and doesn’t change anything. It’s only there for whoever reads the CSS next, which is often you, six months later, wondering why that brown was picked. The same syntax works in a .css file, inside <style> and even inside a style="" attribute:

Comments in a style attribute and inside a value

Both work. In the second paragraph the comment sits between two values of margin, and the browser reads it as margin: 0 20px.

Multi-line comments and section headers

There’s no separate syntax for a longer comment. Start with /*, write as many lines as you like, and end with */. Bigger stylesheets often use this to mark sections, so you can find your way around with the editor’s search:

A stylesheet with section headers
/* ==========================
   Corner Café styles
   1. Base
   2. Header
   3. Menu cards
   ========================== */

/* 1. Base */
body { font-family: Georgia, serif; }

/* 2. Header */
.site-header { background: #7c4a2d; }

The rows of = are just text inside the comment. Any style works, as long as the comment opens with /* and closes with */. A numbered list at the top, like this one, helps once a file runs to hundreds of lines.

Comment out a rule or a line

Wrapping code in a comment switches it off without deleting it. You can do it to one declaration or to a whole rule:

A border and a rule switched off

The note has no border and isn’t italic, because both bits are inside comments. Delete the /* and */ around the border line and the border comes back. When a layout goes wrong, this is how you track down the line at fault: comment it out, look, and put it back.

Why // doesn’t work in CSS

It’s an easy habit to bring over from JavaScript. CSS has no // comment, so the browser reads the slashes as broken code, and its rule for broken code is to skip ahead to a point where it can start again. That skip takes more with it than the comment. At the top level, it takes the next whole rule:

// before a rule drops that rule

Latte is blue, but the price should be green and bold, and it isn’t. The browser read // Prices .price as one long, invalid selector and threw the rule away. Inside a rule, it skips up to the next semicolon:

// inside a rule eats the next line

The text is red, but not bold. With no semicolon at the end of the comment, “make it stand out font-weight: 700;” became one broken declaration and the bold went with it. Change both // lines to /* */ and everything works.

Here’s why so many people think // works. A line like // color: red; inside a rule ends in a semicolon, so the browser drops exactly that one line and nothing else. It looks like a comment, until the day you write one without a semicolon. (Sass and Less files do have // comments, and they remove them when they build the CSS. Plain CSS doesn’t.)

Comments can’t be nested

A comment ends at the first */, wherever it is. So if you wrap a block that already has a comment inside it, the outer comment stops early:

A comment inside a comment

The plan was to switch off both h2 rules. But the comment closed at “too bright */”, so h2 { font-size: 32px; } is live and the heading is huge. Then the leftover */ ran into the p rule and broke it, so the paragraph isn’t blue. To comment out code that has comments in it, remove the inner */ first, or comment out each rule on its own.

/*! comments that minifiers keep

Before a site goes live, its CSS is usually minified: spaces and comments are stripped to make the file smaller. A comment that starts with /*! is the agreed way to say “keep this one”, which is why license notes in libraries are written like that. Here’s what our CSS Minifier does with a small file:

Before
/*! Corner Café theme, MIT license */
/* Header */
h1 {
  color: #7c4a2d; /* brown */
}
After minifying
/*! Corner Café theme, MIT license */h1{color:#7c4a2d}

The normal comments are gone and the /*! one stays. Most build tools and minifiers follow the same convention, though many let you change it in their settings. It also means you can comment freely in your source files: the notes cost nothing once the CSS is minified.

CSS comments vs HTML comments

Each language has its own comment. /* */ only means something inside CSS, and <!-- --> only in HTML:

Which comment hides what

The HTML comment and the CSS comment are both hidden. The /* */ line outside the <style> isn’t, because in HTML it’s ordinary text. It works the other way too: wrapping a CSS rule in <!-- --> inside <style> doesn’t switch the rule off, because CSS ignores those markers and runs the rule anyway. The HTML comments tutorial has a live example of that, and covers comments in JavaScript too.

The keyboard shortcut

In most code editors, including VS Code, Sublime Text and the JetBrains editors, Ctrl+/ (Cmd+/ on a Mac) comments out the current line or the selected lines. In a .css file, or inside a <style> block, the editor uses /* */ for you. Press it again to remove the comment. If nothing happens, your editor may use a different shortcut or your keyboard layout may need Shift for the slash; look for “Toggle Line Comment” in its menus.

Mistakes that break a stylesheet

  • A comment that’s never closed. Everything after a lone /* becomes part of the comment, to the end of the file. The rules above it work and every rule below it is gone. If the bottom half of your styles stops working at once, look for this first. The CSS Validator points it out.
  • Using //. It drops the next rule or the next declaration, as shown above. The CSS Validator points to the line and tells you CSS has no // comments.
  • A comment inside a comment. The first */ ends both, and the rest leaks out as broken code.
  • A space in the markers. / * or * / isn’t a comment marker. The two characters have to touch.
  • Secrets in comments. Comments are sent to every visitor and anyone can read them with View Source or DevTools. Leave out passwords, private links and anything you wouldn’t put on the page itself.

If a stylesheet is hard to read before you add comments, run it through the CSS Formatter first. To write selectors worth commenting on, see the CSS selectors tutorial.

Questions people ask

How do you write a comment in CSS?

Start it with /* and end it with */, for example /* Brand brown for headings */. The browser ignores everything in between. It works above a rule, at the end of a line, inside a rule, in a <style> block, in a .css file and in a style attribute.

How do I write a multi-line comment in CSS?

The same way as a one-line comment: start with /*, write as many lines as you like, and end with */. CSS has only this one kind of comment, so there is no separate syntax for longer notes or section headers.

Can I use // for comments in CSS?

No. Plain CSS has no // comment. The browser treats the slashes as broken code and skips ahead: at the top level it drops the whole next rule, and inside a rule it drops everything up to the next semicolon, which often takes the next declaration with it. Use /* */ instead. Sass and Less do allow //, and remove those comments when they build the CSS.

How do I comment out a CSS rule?

Wrap it in /* and */: /* .note { font-style: italic; } */. To switch off one line, wrap only that declaration: /* border: 2px solid #b45309; */. Remove the markers to turn it back on. Most editors do this for you with Ctrl+/ (Cmd+/ on a Mac).

Can CSS comments be nested?

No. A comment ends at the first */ it meets, so a comment inside another one closes the outer comment early. The code after it becomes live again, and the leftover */ breaks the next rule. Remove the inner comment's */ before commenting out a block.

What does /*! mean in CSS?

It is an ordinary comment as far as the browser is concerned, but minifiers treat a comment that starts with /*! as one to keep when they strip the others. Libraries use it for license and copyright notes so they survive in the minified file.

Can I use HTML comments in CSS?

No. Inside a <style> block, <!-- and --> don't comment anything out: CSS ignores those two markers and the rule between them still applies. Use /* */ in CSS and <!-- --> in HTML. Outside <style>, /* */ is just text and shows on the page.

Do CSS comments slow down my website?

They add a few bytes to the file, which matters little, and they are removed when you minify the CSS before going live. They are also public: anyone can read them in View Source or DevTools, so keep passwords and private notes out of them.

Tools that help