CSS Comments

CSS course › CSS Basics › Lesson 5 of 96

`/* comment */` — annotate your styles for future-you and teammates.

Definition

CSS uses /* */ for comments — single-line or multi-line.

/* This is a comment */

/*
  Multi-line comment
  Useful for section headers
*/

h1 {
  color: orange;       /* main brand colour */
  font-size: 48px;     /* override Tailwind default */
}

Writing Clear CSS Comments

/* ─── Reset ─── */
* { margin: 0; padding: 0; box-sizing: border-box; }

/* ─── Typography ─── */
body { font-family: 'Inter', sans-serif; line-height: 1.6; }
h1, h2, h3 { font-weight: 700; }

/* ─── Layout ─── */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem;
}

/* ─── Buttons ─── */
.btn {
  /* TODO: extract to design tokens */
  background: orange;
  color: white;
  padding: 0.5rem 1rem;
}

When to Comment

  • Section headers — group related rules.
  • TODOs — reminders.
  • Why, not what — /* fixes Safari flex bug */ beats /* set width */.
  • Magic numbers — padding: 7.5px /* matches header height */.

Common Mistakes

  • // single-line — NOT valid in CSS (works in SCSS).
  • **Nested /* */** — invalid; outer ends at first */.
  • Comments left in production — strip in build (Webpack, Parcel).

Interview Questions

  1. CSS comment syntax? *Hint: /* */.* /* like this */ only — no //.
  2. Can I comment out one declaration? *Hint: yes.* color: red; /* font-size: 16px; */.
  3. Are comments stripped in production? *Hint: minifier.* Yes — minifiers (cssnano, esbuild) remove them.
  4. Why comment "why"? *Hint: 6 months later.* Code shows what; comments explain why.
  5. // works? *Hint: SCSS only.* Only in pre-processors (SCSS, Sass, Less). Not vanilla CSS.

Practice Exercises

  1. Section your file — Add /* === Reset === */ headers. *Hint: 3-5 sections.*
  2. TODO — Add /* TODO: extract colour to var */ to a hardcoded value. *Hint: searchable.*
  3. Why comment — Replace /* set padding 7.5px */ with /* matches header line-height */. *Hint: explanation.*

> 💡 Think Like a Programmer: Comments aren't documentation — they're notes to future-you. Make them meaningful.

Example code

/* see lesson examples */