
Writing clean CSS becomes much easier when you know How to Write Comments in CSS? Comments help you explain code, organize sections, and remember why a specific style exists. They don’t appear on the visible webpage, yet they can make CSS code far easier to understand and maintain.
Whether you’re a beginner learning web development in Pakistan, a student practicing HTML and CSS, or a freelancer managing client websites, knowing CSS comments is a useful skill.
Learn the correct syntax, practical examples, and simple ways to use comments while keeping your stylesheet clean, readable, and easy to update.
How to Add Comments in CSS?
The answer to how to add comments in CSS is straightforward. CSS uses an opening /* and a closing */. Everything between these CSS comment symbols becomes a comment. This is the standard method supported by CSS.
/* This is a CSS comment */
You can place a CSS comment above a CSS selector, between rules, or around a specific section. The forward slash and asterisk form the opening comment tag, while the second pair creates the closing comment tag.
i. Basic CSS Comment Syntax
The CSS comment syntax follows one simple pattern:
/* Your comment text goes here */
The comment text can contain normal plain text. You can also write several words or sentences inside the same comment. The browser treats everything between the opening and closing marks as a comment.
/* Header navigation styles */
nav {
background: blue;
padding: 20px;
}
This CSS comment example doesn't affect the navigation. It simply explains the purpose of the following CSS code.
ii. Where Can You Add Comments in CSS?
You can add CSS comments in stylesheet files almost anywhere. A comment can appear before a selector, between declarations, or around a complete code block. You can even use CSS comments in style tag when writing CSS directly inside an HTML document.
<style>
/* Main heading styles */
h1 {
font-size: 32px;
}
</style>
These style tag comments work because the CSS still follows the standard /* */ format. You should not replace them with HTML comment syntax.
How to Comment Out CSS Code?
Sometimes you don't want to delete a CSS rule. You only want to stop it from working temporarily. This is where comment out in CSS becomes useful. By wrapping code in /* */, you can deactivate CSS code while keeping it available for later.
button {
background: blue;
/* color: white; */
padding: 10px;
}
Here, background and padding still work. The color declaration doesn't apply because it has become commented-out CSS. This technique is useful during code troubleshooting because you can test different styling combinations without deleting your original work.
Comment Out One CSS Property
You can comment code in CSS by wrapping only one declaration. This is useful when you suspect one property causes a design problem.
.card {
width: 300px;
/* margin: 20px; */
padding: 15px;
}
The browser ignores the commented margin. The remaining CSS declarations continue working normally. This is a quick way to test a change.
How to Comment Out an Entire CSS Rule?
You can also comment out CSS code that contains an entire CSS rule set. A rule set normally contains a CSS selector followed by declarations.
/*
.card {
background: white;
border: 1px solid gray;
padding: 20px;
}
*/
The entire CSS rule is now inactive. This method is useful when comparing two versions of a design or debugging a layout.
How to Write Single-Line Comments in CSS?
A single-line comment in CSS uses exactly the same /* */ syntax as other CSS comments. CSS doesn't have a separate official single-line symbol. You simply keep the comment on one line.
/* This styles the main button */
.button {
padding: 12px 20px;
}
These single-line CSS comments are excellent for short explanations. They work well above a selector or before a special declaration. They also keep a CSS stylesheet easy to scan.
Can You Use // for CSS Comments?
No. The double forward slash // isn't the standard CSS comment syntax. Developers coming from JavaScript sometimes make this mistake because JavaScript single-line comments use //.
/* Correct CSS comment */
/* // This is not the standard CSS method */
Always use the standard /* */ method in regular CSS. Some preprocessors may support other syntax, but that doesn't make // valid standard CSS.
How to Write Multiline Comments in CSS?
Multiline comment in CSS works by keeping the same opening and closing symbols around several lines. This makes multi-line CSS comments useful for larger explanations or section labels.
/*
Header section
Navigation styles
Mobile menu styles
*/
Everything inside the opening and closing marks becomes comment content. The browser ignores it during rendering. This makes multiline comments useful for CSS code organization.
Multiline CSS Comment Example
Suppose you have a large stylesheet. You can create clear sections like this:
/*
=========================
Product Card Section
=========================
*/
.card {
padding: 20px;
border-radius: 8px;
}
This CSS comment example makes the file easier to navigate. It also helps a team understand the structure without reading every line of code.
CSS Comments Syntax and Examples
The core CSS comments syntax is always /* comment */. You can use it for documentation, organization, or temporary code removal. The following examples show several common uses.
Use | Example |
Short comment | /* Header */ |
Property comment | /* Temporary margin */ |
Section comment | /* Footer styles */ |
Multiline comment | /* Header, menu, and logo */ |
Disabled rule | /* .box { color: red; } */ |
These CSS comments examples show why the same syntax can handle different tasks. You don't need a special format for each situation.
01. Commenting CSS Inside a Rule
A comment inside CSS can sit between declarations when you need to explain a specific part.
.hero {
width: 100%;
/* Keeps text readable on wide screens */
max-width: 1200px;
margin: auto;
}
The comment doesn't become part of the rendered website styling. It simply gives another developer useful context.
02. CSS Comments for Debugging
During code troubleshooting, comments can help you isolate a problem. Imagine a card looks too wide. You could temporarily comment out its width or padding. If the layout improves, you've found a clue.
This approach is safer than deleting the original CSS property. Once testing ends, you can remove the comment or restore the declaration.
HTML Comments vs CSS Comments: What’s the Difference?
HTML vs CSS comments mainly differ in their syntax and location. HTML comments use <!-- -->, while CSS uses /* */. Both remain hidden from normal page rendering, but they belong to different languages.
Feature | HTML Comments | CSS Comments |
Language | HTML | CSS |
Syntax | <!-- comment --> | /* comment */ |
Main purpose | Explain HTML code | Explain CSS code |
Rendered as visible text | No | No |
Used inside stylesheet | No | Yes |
A common beginner error is putting an HTML tag or HTML comment into a CSS file. Remember the language you're editing.
HTML and CSS Comment Syntax Example
An HTML document might contain this:
<!-- Main navigation -->
<nav>
...
</nav>
The related CSS can contain this:
/* Navigation appearance */
nav {
display: flex;
}
Also Read: How to Add CSS to Your Website
Best Practices for Writing CSS Comments
Effective CSS comments best practices focus on usefulness rather than quantity. Don't explain something obvious. Instead, explain why a strange value exists, what a section controls, or why a particular workaround is necessary.
i. Good vs Bad CSS Comments
A weak comment simply repeats the code:
/* Set color to blue */
color: blue;
A better comment explains the reason:
/* Matches the brand color used in the website header */
color: blue;
The second version adds context. That's what good code organization should accomplish. Your comment should help someone understand something they couldn't immediately learn from the code itself.
ii. Organize CSS Comments?
Large projects benefit from discrete sections. You might separate a stylesheet into reset styles, typography, navigation, components, forms, and responsive rules.
This approach improves stylesheet organization and makes CSS code maintenance easier.
Common Mistakes to Avoid When Using CSS Comments
One common mistake is using // instead of standard CSS comment tags. Another is forgetting the closing */. An unclosed comment can cause the browser to ignore much more CSS than you intended.
i. Can CSS Comments Be Nested?
Standard CSS nested comments aren't supported. You can't safely place one /* */ comment inside another.
/*
Main comment
/* Nested comment */
*/
This can cause parsing problems. If you need to explain several levels of information, use clear multi-line CSS comments instead.
ii. CSS Comments in VS Code
If you're using CSS comments VS Code, the editor can make commenting faster. Select the desired code block and use the available keyboard shortcut for your operating system.
In many VS Code setups, Ctrl+K followed by Ctrl+C adds a comment. Ctrl+K followed by Ctrl+U removes one. The Ctrl+Shift+/ shortcut can also toggle block comments depending on the editor configuration and context.
Action | Common VS Code Shortcut |
Add line comment | Ctrl+K, then Ctrl+C |
Remove line comment | Ctrl+K, then Ctrl+U |
Toggle block comment | Shift+Alt+A |
The exact behavior can depend on the language mode and keyboard configuration. So, check your editor if a shortcut behaves differently.
CSS Comments in Style Tags, Divs, and Comment Boxes
CSS comments in style tag follow the same /* */ syntax used in external stylesheets. However, an HTML comment inside a div uses HTML syntax because the div tag belongs to HTML.
There is also a different meaning behind terms like CSS comment box, CSS comment styling, and threaded comments CSS. These refer to designing visible comment areas on websites. They aren't the same thing as code comments.
i. Style a CSS Comment Box?
A visible comment box is an interface element. You might create its shape with a rectangular element, then use a pseudo-element such as ::after to create a small triangular arrow.
Properties such as the clip-path property can also create creative shapes. This is about website design rather than hiding developer notes inside CSS.
ii. Style Threaded Blog Comments?
When working with threaded comments on a blog post, CSS can control indentation, spacing, borders, typography, and reply areas. This can apply to blog comments on platforms such as Blogger or custom websites.
A Blogger dashboard, Blogger theme, or HTML editor may provide different ways to add custom CSS. The exact process depends on the theme and platform. Always back up your theme before making major changes.
Conclusion: How to Write Comments in CSS Effectively
Now you know How to Write Comments in CSS? The main rule is easy: use /* */ around the text or code you want to comment. This method works for short notes, multiline explanations, and temporarily disabled CSS.
Latest FAQs - How to Write Comments in CSS?
How do you write a comment in CSS?
Use /* */ around your text. For example, /* This is my comment */ creates a valid CSS comment. The browser ignores the content when applying styles.
What is the CSS comment syntax?
The standard CSS comment syntax is /* comment */. The opening /* begins the comment. The closing */ ends it.
Can you use // for comments in CSS?
No. // is associated with JavaScript single-line comments and some CSS preprocessors. Standard CSS uses /* */ for comments.
Are CSS comments visible to website visitors?
They don't appear as visible content on the rendered page. However, website visitors can often inspect source code or downloaded CSS. So, never treat CSS comments as private storage.
Do CSS comments affect SEO?
They don't directly improve rankings. Their main value comes from better code readability, organization, and maintenance. Clean code can make development easier, but comments themselves aren't an SEO ranking factor.





