CSS Comment Syntax – How to Write Comments in CSS | Complete Beginner's Guide (2026-27)
Comments in CSS are one of the most important habits a developer can build. They let you leave notes inside your stylesheet that the browser completely ignores — but that you and your teammates will thank you for later. Whether you want to explain a tricky rule, label a section, or temporarily disable some code while debugging, CSS comments are the tool for the job. In this tutorial you will learn the exact syntax, see real examples, and understand the best practices every professional developer follows.
📋 Table of Contents
- What Is a CSS Comment?
- CSS Comment Syntax
- Single-Line CSS Comments
- Multi-Line CSS Comments
- Using Comments to Disable CSS Code
- Types of CSS Comments – Overview Table
- Best Practices for CSS Comments
- Common Mistakes to Avoid
- Live Code Example
- Try It Yourself – Interactive Editor
- Practice Quiz
- Frequently Asked Questions (FAQ)
✅ What Is a CSS Comment?
A CSS comment is a piece of text inside your CSS file that the browser completely skips when rendering the page. Comments are only meant for humans — developers reading or maintaining the code. They are invisible to website visitors on the page, though they can be seen in the browser's DevTools or View Source.
CSS comments serve four main purposes:
- ✔️ Documentation – Explain what a section of CSS does and why.
- ✔️ Organization – Label sections of a large stylesheet (e.g., "/* === HEADER === */").
- ✔️ Debugging – Temporarily disable CSS rules without deleting them.
- ✔️ Collaboration – Leave notes for teammates, or for your future self.
✅ CSS Comment Syntax
There is only one comment syntax in CSS. It uses a slash-asterisk opening and an asterisk-slash closing:
p {
/* Set the font size for paragraphs */
font-size: 16px;
}
- Opens with
/* - Closes with
*/ - Everything between is ignored by the browser
- Can appear anywhere whitespace is allowed in CSS
//does NOT work as a comment in CSS
// single-line comment style. Writing // this is a comment in CSS will cause unexpected behavior. Always use /* ... */.
✅ Single-Line CSS Comments
A single-line comment in CSS is simply a /* ... */ that fits on one line. You can place it above a rule, beside a property, or anywhere inside the stylesheet.
✔️ Example 1 – Comment Above a Rule
/* Style all headings */
h1 {
color: #1E3A5F;
font-size: 28px;
}
✔️ Example 2 – Inline Comment Beside a Property
p {
font-size: 16px; /* base body font size */
line-height: 1.7; /* improves readability */
color: #333; /* dark gray, softer than pure black */
}
✔️ Example 3 – Section Label Comment
/* ============================================
NAVIGATION STYLES
============================================ */
nav {
background: #1E3A5F;
padding: 15px 20px;
}
✅ Multi-Line CSS Comments
A multi-line comment uses the same /* ... */ syntax but spans several lines. This is useful for writing longer explanations, file headers, or documentation blocks.
✔️ Example – File Header Comment
/*
Stylesheet: main.css
Project: LearnToSAP Tutorial Site
Author: Pramod Behera
Last Updated: June 2026
Description: Main styles for the tutorial layout,
typography, and component library.
*/
✔️ Example – Multi-Line Explanation
/*
We use a max-width of 900px on article.page-content
to keep line lengths readable on wide monitors.
Research shows 60–80 characters per line is optimal
for reading comprehension.
*/
article.page-content {
max-width: 900px;
margin: 0 auto;
}
✅ Using Comments to Disable CSS Code
One of the most powerful uses of CSS comments during debugging is "commenting out" a rule — wrapping it inside /* ... */ to temporarily disable it without deleting it. This lets you quickly see whether a specific rule is causing a layout problem.
✔️ Example – Commenting Out a Property
h1 {
color: red;
/* font-size: 48px; */ /* disabled for testing */
font-weight: bold;
}
✔️ Example – Commenting Out an Entire Rule
/*
.sidebar {
float: right;
width: 300px;
background: #f5f5f5;
}
*/
.sidebar rule above is completely ignored by the browser until you remove the /* and */ markers. This is a standard debugging workflow used by professional developers every day.
✅ Types of CSS Comments – Overview Table
| Comment Type | Syntax | Use Case | Spans Multiple Lines? |
|---|---|---|---|
| Single-Line Comment | /* comment */ |
Short annotations, inline notes | No (kept on one line by convention) |
| Multi-Line Comment | /* line1 |
File headers, section labels, long explanations | Yes |
| Commented-Out Code | /* .rule { ... } */ |
Debugging — temporarily disable a rule | Yes |
| Section Divider | /* ====== HEADER ====== */ |
Organise large stylesheets into named sections | No |
✅ Best Practices for CSS Comments
Writing good comments is a skill. Here are the professional habits that will make your stylesheets clean, maintainable, and team-friendly:
✔️ 1) Comment the Why, Not the What
The code already shows what it does. A good comment explains why that choice was made.
/* z-index must be higher than the modal overlay (z-index: 999) */
.tooltip {
z-index: 1000;
}
✔️ 2) Use Section Headers in Large Files
In stylesheets longer than 200 lines, always add section headers so anyone can jump to the right part instantly.
/* ============ RESET ============ */
/* ============ TYPOGRAPHY ======= */
/* ============ LAYOUT =========== */
/* ============ COMPONENTS ======= */
/* ============ UTILITIES ======== */
✔️ 3) Mark TODOs and FIXMEs
/* TODO: replace magic number with a CSS variable */
.hero {
margin-top: 73px;
}
✔️ 4) Remove Comments Before Production (Minify)
Comments add bytes to your CSS file. Always run your CSS through a minifier (e.g., cssnano, CleanCSS) before deploying to production. This strips all comments automatically.
✔️ 5) Keep Comments Up to Date
An outdated comment is worse than no comment — it actively misleads the reader. When you change a rule, update or remove its comment.
/* ... */ automatically.
✅ Common Mistakes to Avoid
// This does NOT work in CSSThe
// syntax is from JavaScript and C-style languages. It is not valid in CSS and may silently break your stylesheet.
/* outer /* inner */ this breaks */CSS does not support nested comments. The first
*/ closes the entire comment, leaving "this breaks */" as invalid CSS.
/* This comment was never closedIf you forget the closing
*/, everything after the opening /* will be treated as a comment and all your CSS rules below it will be silently ignored.
color: red; /* sets color to red */This adds noise without value. Only comment code that isn't immediately obvious.
CSS comments are visible in View Source. Never leave passwords, API keys, internal notes, or personal information in CSS comments on a live website.
✅ CSS Code Example – Comments in Action
Here is a complete, real-world CSS file showing how professionals use comments at every level — file header, section labels, inline notes, and debugging comments:
File: styles.css
Author: Pramod Behera
Project: CSS Comment Demo
*/
/* ====== RESET ====== */
* { box-sizing: border-box; }
/* ====== BODY ====== */
body {
font-family: Arial, sans-serif;
background: #f0f4f8; /* light blue-grey */
padding: 30px;
}
/* ====== CARD ====== */
.card {
background: #fff;
border-radius: 10px;
padding: 24px;
/* TODO: add box-shadow in next version */
border-top: 4px solid #0EA5E9;
}
.card h2 {
color: #1E3A5F;
}
/* Commented-out rule for debugging: */
/* .card p { display: none; } */
CSS Comments Work!
The browser ignores all the /* ... */ comment markers in the CSS — only the actual rules are applied. Comments have zero effect on the visual output.
Comments = 0 visual change✅ Try It Yourself – Interactive CSS Comment Editor
Edit the HTML and CSS below to experiment with CSS comments. Try adding your own, commenting out a rule, or writing a section header. The preview updates automatically.
✅ Practice – Yes / No Quiz
1. CSS comments use the /* ... */ syntax?
2. The // syntax is a valid CSS comment?
3. CSS comments are completely ignored by the browser?
4. CSS supports nested comments like /* outer /* inner */ */?
5. You can use CSS comments to temporarily disable a CSS rule during debugging?
✅ Frequently Asked Questions (FAQ)
/* ... */ syntax. Open with /* and close with */. Everything between these two markers is completely ignored by the browser. Example: /* This is a CSS comment */// single-line comment style used in JavaScript. In CSS, the only comment syntax is /* ... */. You can write it on one line to create a "single-line" comment, but the // form does not work in standard CSS./* ... */ will disable it without deleting it. This is called "commenting out" code and is a very common debugging technique./* outer /* inner */ */, the first */ closes the entire comment and the trailing */ becomes invalid CSS syntax. Always avoid nesting comments.