Syntax
Use /* comment text */. CSS comments can span multiple lines.
CSS comments help future readers understand intent, but they should explain decisions instead of repeating what the code already says.
/* Explain why this rule exists. */
.button:focus-visible {
outline: 3px solid #8cffc1;
}
CSS Basics
CSS comments start with /* and end with */. Anything inside is ignored by the browser.
Good comments are useful when the reason behind a rule is not obvious: a browser workaround, an accessibility decision, a layout constraint or a design-system boundary.
Bad comments repeat the property name, leave dead code behind or become outdated. In CSS, outdated comments are dangerous because they make the stylesheet feel less trustworthy.
Use /* comment text */. CSS comments can span multiple lines.
Do not put one CSS comment inside another CSS comment.
Explain why a rule exists when the code alone does not say enough.
Remove old commented-out code before production.
Visual model
CSS becomes easier when you can see the parts: what is selected, what is declared, what the browser loads and what the final interface receives.
/* Keep focus visible for keyboard users */
Examples
/* Keep focus visible because this button has a custom background. */
.button:focus-visible {
outline: 3px solid #8cffc1;
outline-offset: 4px;
}
/* Header is sticky only on screens with enough vertical space. */
@media (min-height: 760px) {
.site-header { position: sticky; top: 0; }
}
/* Make the text red */
.error { color: red; }
/* old button maybe use later
.button { background: purple; padding: 90px; }
*/
Rules that matter
Beginners often try to learn CSS by collecting declarations. That is backwards. Learn the decisions first, then the properties become much easier to remember.
Comment browser fixes, accessibility choices, design tokens and surprising layout values.
Do not write comments that only restate the property name.
A short comment can make a long file easier to scan, but too many markers become clutter.
Commented-out CSS is not a version-control strategy. Git already remembers old code.
A wrong comment is worse than no comment.
Never place internal tokens, private URLs or sensitive notes in CSS comments.
Production thinking
CSS can become large quickly. Comments are one of the few ways to preserve reasoning, but only if they carry information that the code cannot express by itself.
Comments are useful for protecting accessibility rules. A clear note can prevent someone from removing focus outlines or reducing contrast later.
Production CSS can keep useful comments, but remove experiments, TODOs without owners and commented-out alternatives.
Comments are not visible content for SEO. Their value is maintainability, which indirectly helps the quality of the site over time.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read the lesson. Answer these questions out loud or write the answers in your own notes. CSS becomes real when you can explain what the browser is doing.
Senior audit upgrade
Production builds often minify CSS. Comments may disappear, so comments should explain intent for developers before build time, not carry information users need.
Use comments for decisions, constraints and warnings, not for repeating the next declaration.
Do not rely on CSS comments being visible in production output. Build tools usually strip them.
Place the comment near the rule it protects, especially for accessibility or browser-support decisions.
Explains an accessibility reason.
Repeats "set color red" above color: red.
Commented-out old CSS can hide dead decisions.
Section labels can help in long CSS files.