CSS Comments

CSS comments are used to explain code and make stylesheets easier to understand. Comments are ignored by the browser and do not affect how styles are applied. They are very helpful for documentation and teamwork.

Why Use CSS Comments?

CSS Comment Syntax

CSS uses a specific syntax for comments. A comment starts with /* and ends with */.

/* This is a CSS comment */

Single-Line Comments

Although CSS does not officially support single-line comments, you can still write short comments on one line using the same syntax.

/* Header styles */
header {
  background-color: #333;
}

Multi-Line Comments

Multi-line comments are commonly used to describe sections or explain groups of CSS rules.

/*
  Navigation Bar Styles
  These styles control the main menu
*/
nav {
  display: flex;
  justify-content: space-between;
}

Using Comments to Organize CSS

Comments are often used to separate different parts of a stylesheet for better readability.

/* =========================
   Layout Section
   ========================= */

.container {
  max-width: 1200px;
  margin: auto;
}

Commenting Out CSS Code

You can temporarily disable CSS rules by commenting them out. This is useful for debugging and testing.

/*
.button {
  background-color: blue;
  color: white;
}
*/

Best Practices for CSS Comments

Common Mistakes with CSS Comments

Tip: Well-written comments make your CSS more professional and easier to manage in the long run.