Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Rules  ›  Capstone Project

Comments

Comments 12 minCSS comments are written between slash-star and star-slash, do not nest, are visible to every visitor, and should explain why rather than what.

Comments

Six months from now someone opens the café's stylesheet and finds margin-top: -3px; on the price. Is it a hack for a font quirk, or a mistake? A single comment would have answered in one line. Comments are notes for humans that the browser ignores.

The rule

A CSS comment opens with /* and closes with */, and nothing else counts.

/* ---------- Menu ---------- */

.price {
  color: green;
  /* Nudge up so the price lines up with the dish name. */
  margin-top: -3px;
}

/*
  Old festival colour, kept until the owner decides:
  .special { background-color: orange; }
*/

A comment can sit on its own line, beside a declaration, or across many lines. Good uses:

  • Explain why, not what. color: green; explains itself; the reason for -3px does not.
  • Label sections so a long file can be scanned.
  • Switch code off temporarily while testing, by wrapping it in a comment.

Three rules that trip people up:

  • // is not a CSS comment. Browsers treat it as ordinary, invalid text, which breaks whatever comes next.
  • Comments do not nest. A comment ends at the first */ it meets, even if you opened another /* inside it.
  • Comments ship to every visitor. Anyone can read them in the browser's developer tools, so never write passwords, private notes or internal URLs in them.

The shortcut

When you see a comment, find its first */. Everything from /* to that point is ignored; everything after it is CSS again, whether or not you meant it to be.

Commenting out a block that already has a commentStep 1 of 4
  1. The attempt

    /* .special { color: orange; /* festival */ } */

Drills

Answer fast — trust the rule.

quick drill

Which of these is a valid CSS comment?

quick drill

A stylesheet has the line // old colour directly above .price { color: green; }. What happens?

quick drill

Which is the best comment to leave in the café's stylesheet?

quick drill

Where does the comment in /* a /* b */ c */ end?

Remember

  • CSS comments are /* ... */ only; // breaks the next rule.
  • Find the first */; that is where the comment ends.
  • Comment the why, and never put secrets in shipped CSS.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainComment with care

A teammate adds the note // festival colours on the line directly above .special { background-color: orange; }, and the special dish loses its orange background. Explain why, show the correct way to write the note, and give one guideline for what a good CSS comment says.