Episode 7 of 53

Comments in CSS

Learn how to add comments to your CSS code for better readability and documentation.

Comments help you document your CSS code. They are ignored by the browser and are purely for human readers.

CSS Comment Syntax

CSS uses the /* ... */ syntax for comments:

/* This is a single-line comment */

/*
   This is a
   multi-line comment
*/

h1 {
    color: navy; /* Set heading color */
    font-size: 2rem;
}

Important Notes

  • CSS does not support // single-line comments (unlike JavaScript)
  • Comments can appear anywhere in your stylesheet
  • Comments can span multiple lines
  • Comments cannot be nested inside other comments

Best Practices

  • Use comments to organize sections of your stylesheet
  • Explain why you made a decision, not just what the code does
  • Use section dividers for large stylesheets
/* =========================
   HEADER STYLES
   ========================= */
header {
    background: #2c3e50;
    padding: 1rem;
}

/* =========================
   MAIN CONTENT
   ========================= */
main {
    max-width: 800px;
    margin: 0 auto;
}

Good comments make your code easier to understand and maintain, especially in team environments.