EthicsComputerCourses
Join training +91 90532 10052

CSS Comments

CSS · Lesson 20 of 240 Beginner 11 min read Updated

By the end of this lesson you will write CSS comments correctly, avoid the two comment mistakes that silently delete your rules, use comments to find bugs, organise a long stylesheet with section headers, and know what happens to comments when a site is published.

You saw the basic comment syntax in CSS Syntax & Rules, and HTML comments in Comments in HTML. This lesson goes further with CSS.

The syntax

A CSS comment starts with /* and ends with */. The browser ignores everything between them. A comment can sit on one line or stretch across many.

CSS
/* Brand colours for the Jaipur store */
.badge {
  color: #b45309; /* saffron, matches the printed menu card */
}

/*
  Price list styles.
  Used on the menu page and the order page.
*/
.price { font-weight: 700; }

You can put a comment almost anywhere: between rules, between declarations, or at the end of a line. Think of comments as pencil notes in the margin of a textbook. They help the reader, but they are not part of the lesson that gets tested.

There is one place where /* does not start a comment: inside a quoted string. In content: "/* sale */"; the text is just characters to display, and the browser shows them as written.

Rule 1: comments do not nest

You cannot put one comment inside another. The browser ends a comment at the first */ it finds, whatever came before it. Look at this:

CSS
/* Hide the old banner /* added in March */ until the sale */
.offer { color: crimson; }

The comment ends after March. The words until the sale */ are now treated as CSS. They are not valid CSS, so the browser joins them to the next rule and throws that whole rule away. Run this example to see it happen.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Nested comment</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }

    /* Hide the old banner /* added in March */ until the sale */
    .offer { color: crimson; font-weight: bold; }

    .note { color: #12355b; font-weight: bold; }
  </style>
</head>
<body>
  <p class="offer">Mango season sale: 20% off at Priya Fruits, Rohtak</p>
  <p class="note">Shop open 8 am to 9 pm</p>
</body>
</html>

The offer line stays plain black, because the .offer rule was lost. The note below is dark blue and bold, because the damage stopped at the end of the broken rule. Remove the inner /* and */ and run it again: the offer turns crimson.

This is the most common way to break a stylesheet with comments. It usually happens when you comment out a block of code that already contains a comment.

Rule 2: there is no // comment

JavaScript, PHP and Sass let you write // note for a one-line comment. Plain CSS does not. A // is not a comment in CSS: it is two slash characters that the browser cannot understand.

What gets lost depends on where you put it. Above a rule, the // text joins that rule's selector, and the whole rule is dropped. Inside a block, it joins the next declaration, and that declaration is dropped.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>No slash comments</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }

    // Heading for the scorecard
    h1 { color: #8a3324; }

    .score {
      color: #12355b;   // team colour
      background: #e3f6e8;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <h1>Ranji Trophy: Kerala vs Rajasthan</h1>
  <p class="score">Kerala 312/7 at stumps</p>
</body>
</html>

You should see a black heading, not maroon, because the h1 rule was dropped. The score is dark blue and bold, but it has no green background. The // team colour text swallowed the background declaration on the next line, up to its semicolon. Change both comments to /* ... */ and everything works.

If you later learn Sass, you will find that its // comments are allowed and are removed when Sass turns your code into CSS. That is a Sass feature, not a CSS one. See SCSS, Indented Syntax & Comments.

Commenting out code to debug

Commenting out means wrapping working code in a comment so that the browser skips it, without deleting it. It is one of the fastest ways to find which declaration causes a problem.

Step 1: find the declaration that breaks the layout

Kabir's product card looks squashed, and he cannot see why. The card's rule has several declarations. Comment them out one at a time until the problem disappears.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Debug with comments</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .card {
      border: 1px solid #c9d6ea;
      border-radius: 8px;
      padding: 1rem;
      width: 120px;
      background: #f7faff;
    }
  </style>
</head>
<body>
  <div class="card">
    <h2>Steel tiffin box</h2>
    <p>Three tiers, leak-proof lid. ₹649</p>
  </div>
</body>
</html>

Change width: 120px; to /* width: 120px; */ and run it. The card spreads to a comfortable width, so you have found the culprit. Now you can choose a better value, or delete the line. Because you commented it out instead of deleting it, you could undo the test in one step.

Step 2: do it faster

In VS Code, put the cursor on a line, or select several lines, and press Ctrl+/ (Cmd+/ on a Mac). The editor wraps them in /* */. Press it again to remove the comment. Your editor setup is covered in Installing VS Code for CSS Development.

Browser DevTools offer the same trick without editing files. In the Styles panel, each declaration has a checkbox. Untick it to switch that declaration off and watch the page change. More on this in CSS Debugging & DevTools.

Comments that organise a stylesheet

A real stylesheet can grow to hundreds of rules. Comments that act as section headers make it easy to jump around, like chapter titles in a book. Many teams also put a short contents list at the top.

CSS
/* =========================================
   Meera's Boutique, Kochi: main stylesheet
   Contents:
   1. Base
   2. Header and navigation
   3. Product cards
   4. Footer
   ========================================= */

/* ---------- 1. Base ---------- */
body { font-family: system-ui, sans-serif; }

/* ---------- 2. Header and navigation ---------- */
.site-header { background: #12355b; }

Search for "3. Product" in your editor and you land on the right section at once.

A good comment explains why, not what. /* red text */ above color: red; adds nothing, because the code already says that. Compare it with this:

CSS
.notice {
  /* Extra space so the text clears the fixed header on small phones */
  padding-top: 4rem;
}

That comment tells the next developer something the code cannot: the reason. Without it, someone might "clean up" the large padding and break the phone layout.

Comments and minification

Before a site goes live, its CSS is often minified: a tool removes spaces, line breaks and comments to make the file smaller, so it downloads faster. Your well-commented source file stays on your computer, and visitors get the compact version. For example, this source:

CSS
/* ---------- Buttons ---------- */
.btn {
  /* Brand green from the logo */
  color: #ffffff;
  background: #0b6e4f;
}

becomes something like this after minifying:

CSS
.btn{color:#fff;background:#0b6e4f}

So you never need to delete helpful comments to save space; the build step does it for you. Most minifiers keep comments that start with /*!, a common way to keep a licence notice in a library's file. You will learn the tools in CSS Minification.

Remember that a stylesheet is public. Anyone can open it from the page or in DevTools, and an unminified file shows every comment. Never write passwords, private server addresses or rude remarks in a comment.

Common mistakes

  • Nesting comments. The first */ ends the comment, and the leftover text breaks the next rule.
  • Using //. It is not a comment in CSS and silently removes a rule or a declaration.
  • Using <!-- --> in CSS. That is HTML comment syntax. Inside a .css file or a <style> element, use /* */.
  • Forgetting the closing */. Everything after the opening /* becomes a comment, often the rest of the file.
  • Comments that repeat the code. Explain the reason, not the obvious.

Interview questions

1. What is the comment syntax in CSS?

CSS has one comment syntax: /* to open and */ to close. It works on one line or across many lines. There is no single-line // comment in standard CSS.

2. What happens if you nest comments in CSS?

The comment ends at the first */, so the text after it is parsed as CSS. That text is usually invalid, so it becomes part of the next rule's selector or declaration and the browser drops it. The result is a style that silently stops working.

3. What happens if you use // in a CSS file?

The slashes are not recognised as a comment. Before a rule, they make the selector invalid and the whole rule is ignored. Inside a declaration block, they corrupt the following declaration, which is ignored up to the next semicolon.

4. How do comments help when debugging CSS?

You can comment out a declaration or a rule to switch it off without deleting it, then check whether the problem goes away. This narrows down the cause quickly and is easy to undo. DevTools checkboxes in the Styles panel do the same thing live in the browser.

5. Do comments slow down a website?

Comments add a little to the file size of unminified CSS. In production, minifiers remove them, so you can comment the source freely. Comments starting with /*! are usually kept for licence notices.

Key points

  • CSS comments use /* ... */ and can span several lines; inside a string, /* is plain text.
  • Comments cannot nest: the first */ closes the comment and the leftover text breaks the next rule.
  • // is not a CSS comment; it silently drops the rule or declaration that follows it.
  • Comment out declarations to find bugs; use Ctrl+/ in VS Code or the checkboxes in DevTools.
  • Use section headers to organise long files, and write comments that explain why.
  • Minifiers remove comments before publishing, so comments are free in source but always public.

Exercise

Aarav has written this stylesheet for his tuition centre in Jaipur, and two of its rules do not work:

CSS
// Header
.header { background: #12355b; color: white; }

/* Old footer style /* kept for reference */ remove later */
.footer { color: #555; }

.fees { font-weight: bold; }

Find both problems and explain what the browser does with each one. Rewrite the stylesheet so every rule works, and add a short contents comment at the top with section headers for Header, Footer and Fees.

Show solution

The // Header line is not a comment, so it becomes part of the .header selector and the whole rule is dropped. The nested comment ends at the first */, after reference, so remove later */ becomes part of the .footer selector and that rule is dropped too. The .fees rule works.

CSS
/* ==============================
   Aarav Tuition Centre, Jaipur
   Contents: 1. Header  2. Footer  3. Fees
   ============================== */

/* ---------- 1. Header ---------- */
.header { background: #12355b; color: white; }

/* ---------- 2. Footer ---------- */
/* Old footer style kept for reference; remove later */
.footer { color: #555; }

/* ---------- 3. Fees ---------- */
.fees { font-weight: bold; }

Quick Quiz

1. Which is a valid CSS comment?

2. What happens with /* a /* b */ c */ followed by .menu { color: red; }?

3. A declaration block contains color: navy; // brand and then padding: 1rem; on the next line. What happens?

4. What usually happens to comments when CSS is minified?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call