CSS Comments
Write clean, readable, and well-documented CSS code.
What are CSS Comments?
CSS Comments are notes written inside CSS code to explain what the code does. Browsers completely ignore comments — they do not affect the design of the webpage.
Comments are written mainly for developers, not for users. They help you and other developers understand the code easily.
Real-Life Example: Like writing notes in a textbook to remember important points.
Why Do We Use CSS Comments?
- To explain complex CSS logic
- To organize large CSS files
- To make code easy to read
- To help team members understand styles
- To temporarily disable CSS code
Important: Good comments save time in the future.
CSS Comment Syntax
CSS uses only one type of comment syntax.
It starts with /* and ends with */.
Basic CSS Comment
/* This is a CSS comment */
p {
color: blue;
}
Note: CSS does NOT support // comments.
Single-Line Comments in CSS
CSS does not have a special single-line comment syntax.
But we can write a single-line comment using /* */.
/* Styling the main heading */
h1 {
color: #2563eb;
}
This is very useful when you want to explain one specific rule.
Multi-Line Comments
Multi-line comments are used when you want to explain something in detail or comment out multiple lines of CSS code.
/*
This section styles the header area
It controls background, text color,
and spacing
*/
header {
background-color: #1e40af;
color: white;
padding: 20px;
}
Commenting Out CSS Code
Comments can be used to temporarily disable CSS rules without deleting them.
Disable CSS Temporarily
button {
background-color: green;
color: white;
/* border: 2px solid black; */
}
Use Case: Testing designs or debugging CSS.
Using Comments to Organize CSS
In large projects, comments are used as section labels to organize CSS files neatly.
/* ================= HEADER STYLES ================= */
header {
background: #2563eb;
}
/* ================= NAVIGATION ================= */
nav a {
color: white;
}
/* ================= FOOTER ================= */
footer {
background: #111827;
}
Best Practices for CSS Comments
- Write meaningful comments
- Avoid obvious comments
- Keep comments short and clear
- Remove unused commented code
- Use comments for sections and logic
Pro Tip: Code explains how, comments explain why.