CSS Comments
In this page:
/* single-line comment */
selector {
property: value; /* inline comment */
}
Why Comment CSS
As a stylesheet grows, it becomes easy to forget why a particular rule exists. Comments capture that context directly in the code, right where future readers will need it. A well-placed comment can save a future developer, including your future self, from re-deriving the same reasoning from scratch.
Example: Why Comment CSS
<style>
/* Fixes a Safari flexbox gap bug -- see issue #482 */
.container {
gap: 16px;
}
</style>
<div class="container" style="display:flex">
<div style="background:#cde">One</div>
<div style="background:#fdc">Two</div>
</div>
Comment Syntax
A CSS comment always starts with /* and ends with */. Anything written between those two markers, even across multiple lines, is completely ignored when the page renders. Unlike some languages, CSS has no single-line comment syntax, so even a one-line note needs the full slash-star wrapper.
Example: Comment Syntax
<style>
/* This is a CSS comment, it can span
multiple lines */
p {
color: blue;
}
</style>
<p>Blue text; the comment above is never rendered.</p>
Commenting Out Code Temporarily
Wrapping an existing declaration in /* */ turns it off without deleting it, which is a fast way to test how a page looks without a particular style. This is a fast, non-destructive way to debug a layout issue before deciding whether to remove the rule permanently.
Example: Commenting Out Code Temporarily
<style>
p {
color: blue;
/* background: yellow; */
}
</style>
<p>Blue text; the yellow background is commented out.</p>
Multi-line Comments
Because a CSS comment simply runs from /* to */, it can span several lines just as easily as one, which makes it useful for longer explanations or section headers. This is especially useful for labeling major sections of a large stylesheet, like '/* Navigation styles */'.
Example: Multi-line Comments
<style>
/*
Navigation styles
Used across every page in the site
*/
nav {
display: flex;
}
</style>
<nav style="background:#eee; padding:8px"><a href="#">Home</a> <a href="#">About</a></nav>
Comment Best Practices
Good CSS comments are short, focused, and explain something the code alone can't, like a business reason, a browser quirk, or a warning not to remove something. Overly obvious comments that just restate the selector name add clutter without adding any real value.
Example: Comment Best Practices
<style>
/* Bad: restates the obvious -- sets color to red */
p {
/* Good: explains why -- matches brand red from the style guide */
color: red;
}
</style>
<p>Text colored with the brand red.</p>
- Using double-slash comments (//) from other languages, which are not valid in standard CSS.
- Forgetting to close a comment with */, which can accidentally comment out a large chunk of the stylesheet.
- Writing comments that just repeat what the code already says, instead of explaining the reasoning behind it.
- CSS comments start with /* and end with */, and everything in between is ignored by the browser.
- Comments are useful for explaining tricky decisions, labeling sections, and temporarily disabling rules.
- CSS does not support single-line // comments the way some other languages do.
CSS comment syntax has been part of the language from the start and is supported identically by every browser.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: