CSS Comments
Comments are notes in your stylesheet that the browser completely ignores. They help you (and your teammates) understand why a rule exists, and they let you temporarily switch off code while experimenting.
Comment syntax
A CSS comment starts with /* and ends with */. Everything in between is ignored, and it may span several lines.
/* This is a single-line comment */
/*
This comment
spans several lines.
*/
p {
color: navy; /* a comment after a declaration */
}Note: CSS has no
//line comments. Writing// notein a stylesheet is an error: the browser treats it as garbage and may drop the following rule.
Commenting out code
The most practical use of comments while learning is disabling a declaration to see what it does. Run the example, then remove the /* and */ around the background line and run again.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 220px;
padding: 20px;
margin: 30px auto;
border: 3px solid #264de4;
font: 20px sans-serif;
text-align: center;
/* background: gold; */
/* transform: rotate(-5deg); */
}
</style>
</head>
<body>
<div class="box">Toggle my comments!</div>
</body>
</html>You can comment out entire rules, too:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
/* Disabled while testing:
h1 {
color: white;
background: crimson;
padding: 10px;
}
*/
p { color: darkgreen; font-size: 18px; }
</style>
</head>
<body>
<h1>My heading is unstyled</h1>
<p>Because its whole rule is inside a comment.</p>
</body>
</html>Comments cannot be nested
The first */ always closes a comment. That means you cannot put one comment inside another:
/* outer start
p { color: red; } /* inner comment */
h1 { color: blue; } <- this is NOT commented any more!
*/After /* inner comment */, the comment has ended, so h1 { ... } becomes live code and the stray */ causes a parse error. When you comment out a block that already contains comments, remove or change the inner ones first.
Organizing a stylesheet with comments
Large stylesheets are easier to navigate when divided into sections. A common pattern is a "banner" comment per section:
/* ==========================================
1. Base
========================================== */
body { margin: 0; font-family: system-ui, sans-serif; }
/* ==========================================
2. Header
========================================== */
.site-header { background: #264de4; color: white; }
/* ==========================================
3. Buttons
========================================== */
.btn { padding: 8px 16px; }HTML comments vs CSS comments
Inside an HTML file you use <!-- ... --> comments. Inside a <style> element or a .css file you must use /* ... */.
<!DOCTYPE html>
<html>
<head>
<style>
/* CSS comment inside <style> */
.note { background: #fff3c4; padding: 12px; font: 18px sans-serif; border-radius: 8px; }
</style>
</head>
<body>
<!-- HTML comment: this text is not shown -->
<div class="note">Both kinds of comments are invisible on the page.</div>
</body>
</html>| Context | Comment syntax |
|---|---|
CSS (stylesheet, <style>) |
/* ... */ |
| HTML markup | <!-- ... --> |
| JavaScript | // ... or /* ... */ |
What makes a good comment?
- Explain why, not what:
/* extra padding so the icon does not overlap the text */is useful;/* sets padding */is not. - Note browser workarounds and magic numbers so nobody "fixes" them later.
- Remove commented-out code once you are done experimenting — version control keeps the history.
Tip: Most code editors toggle a CSS comment with Ctrl+/ (or Cmd+/ on macOS).
Test yourself
Which is a valid CSS comment?
// note<!-- note -->/* note */# note
What happens with /* a /* b */ c */?
- Everything is commented out
- The comment ends after
b */, leavingc */as invalid code - It is a nested comment, which CSS supports
- Only
bis commented out