Skip to main content

How to Write Comments in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A CSS comment starts with /* and ends with */. The browser skips everything between them, so a comment can hold a note for the next reader, or switch a rule off without deleting it.

Syntax​

Syntax
/* <comment> */
PartWhat it does
/*Starts the comment.
<comment>Anything: a note, or rules to switch off. It can run over several lines.
*/Ends the comment, at the first */ after the start.

MDN's Comments page says this is the only comment syntax in CSS, and that comments can be placed wherever white space is allowed.

The Example​

The example is a page of opening hours. A comment names the heading's rule, and a second comment switches off a rule that would color the paragraph red.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding the comment Main heading above a rule for h1 that sets the color 0f6cbd, and a comment that starts with Switched off for now and wraps a rule for p that sets the color c50f1f, and a body with the heading Opening hours and one paragraph.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Comments</title>
<style>
/* Main heading */
h1 {
color: #0f6cbd;
}

/* Switched off for now:
p {
color: #c50f1f;
}
*/
</style>
</head>
<body>
<h1>Opening hours</h1>
<p>Monday to Friday, 9:00 to 17:00.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the heading Opening hours in blue and the paragraph Monday to Friday, 9:00 to 17:00. in black.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The heading is blue. The comment above its rule changed nothing.
  • The paragraph is black, not red. Its rule is inside the second comment, so Chromium never read it: the page's style sheet held only the h1 rule.
  • Neither comment shows on the page.

Common Mistakes​

MistakeWhat happensDo this instead
Starting a comment with //CSS has no // comment. The line became part of the next rule's selector and broke it: with // Main heading above the h1 rule, the heading stayed blackUse /* */
Putting a comment inside a commentComments can't be nested: the first */ ends the comment, and the rest broke the rule after it, so the heading stayed blackClose each comment before starting another

Troubleshooting​

SymptomLikely causeFix
The rule right after a comment does nothingThe comment uses //, or it's nestedRewrite it as one /* */ comment

FAQ​

Can a comment go inside a rule?​

Yes. color: #0f6cbd; /* brand blue */ inside the h1 rule worked: the heading was blue, and the next declaration in the rule applied too.

How do I switch a rule off for a while?​

Wrap the whole rule in one comment, as the example does with the p rule. Remove the /* and */ to switch it back on.

Conclusion​

Write CSS comments between /* and */, for notes or to switch rules off. Never use //, and never put one comment inside another: both break the rule that follows.

Additional Resources​