CSS Comments

Beginner
⏱️ 5 min read
📚 Updated: Jul 2026
🎯 4 Examples
CSS Basics

What You’ll Learn

CSS comments let you add notes to stylesheets that browsers ignore. Use them to document code, organize sections, and temporarily disable rules during debugging.

01

/* */

Syntax.

02

Hidden

Not rendered.

03

Document

Explain code.

04

Disable

Debug rules.

05

No //

CSS only.

06

@charset

Comes first.

Introduction

CSS comments are notes written inside a stylesheet for developers. They help you and your team understand why styles exist, mark sections of a large file, and leave reminders for future edits.

Definition and Usage

A comment is any text wrapped in /* and */. The browser completely ignores comment content — it does not affect layout, colors, fonts, or any visual output.

Comments are also useful for temporarily disabling CSS rules. Instead of deleting a rule while debugging, wrap it in a comment and restore it later.

💡
Beginner Tip

CSS does not support // comments like JavaScript. Always use /* */. A “single-line” comment is just a comment that fits on one line — the syntax is the same.

📝 Syntax

Wrap comment text between /* and */:

syntax.css
/* This is a single-line comment */

/*
   This is a multi-line comment
   It can span several lines
*/

Place comments anywhere in a CSS file — between rules, inside rule blocks, or at the end of a line:

inline-comment.css
/* Header styles */
header {
  background: #2563eb; /* brand blue */
  color: #fff;
}

Comment Types

TypeDescription
Single-lineOne comment on one line: /* note */
Multi-lineComment spanning multiple lines for longer documentation
InlineShort note after a property on the same line
Block disableWrap rules in /* */ to turn them off temporarily
/* comment */ No // in CSS Not shown on page

Important Rules

  • Comments cannot be nested — the first */ closes the comment.
  • Comments do not appear on the rendered page but are visible in source and DevTools.
  • In external CSS, @charset must be the first line — no comments before it.
  • Never store passwords, API keys, or private data in comments.

⚡ Quick Reference

QuestionAnswer
Syntax/* your comment */
// comments?Not supported in CSS
Visible on page?No
Disable a ruleWrap it in /* */
Before @charsetNot allowed in strict external CSS
Nested commentsNot supported
Browser supportUniversal

When to Use Comments

Reach for CSS comments in these situations:

  • Section headers — Label groups like Header, Navigation, Footer in large files.
  • Explaining why — Document non-obvious values, hacks, or browser fixes.
  • Debugging — Temporarily disable rules to isolate layout problems.
  • Team handoff — Leave notes for other developers maintaining the stylesheet.
  • TODO reminders — Mark styles that need cleanup or refactoring later.

Skip comments that merely repeat what the code already says, like /* sets color to red */ above color: red;.

👀 Live Preview

Comments exist in your source code but never appear on the page:

In your CSS file /* This note is for developers */
body { color: #1e293b; }
What visitors see Styled text only — no comment visible.
Remember CSS uses /* */ only. // does not work in stylesheets.

Examples Gallery

Practice CSS comments with basic syntax, commenting out rules, multi-line documentation, and inline notes.

🔠 Core Patterns

Start with basic syntax, then learn to disable rules temporarily.

Example 1 — Basic Comment

Add a single-line comment above your styles. The browser ignores it completely.

comment-basic.css
/* This comment is for developers only */

body {
  font-family: system-ui, sans-serif;
  padding: 1.5rem;
  color: #1e293b;
}
Try It Yourself

How It Works

Everything between /* and */ is skipped during parsing. Your styles apply normally.

Example 2 — Comment Out a Rule

Wrap a rule block in a comment to disable it without deleting the code.

comment-disable.css
/*
.highlight {
  background: #fef3c7;
  padding: 0.5rem;
}
*/

p {
  margin: 0;
  font-family: system-ui, sans-serif;
}
Try It Yourself

How It Works

The browser treats the entire commented block as a note. Remove /* */ to re-enable the rule.

📝 Documentation

Document sections and explain individual property values.

Example 3 — Multi-Line Documentation

Use a multi-line comment to describe a whole section of your stylesheet.

comment-multiline.css
/*
  Navigation styles
  - Flex layout for horizontal links
  - Updated: July 2026
*/
nav {
  display: flex;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: #1e293b;
}
Try It Yourself

How It Works

Multi-line comments are ideal for section headers and file-level documentation at the top of a stylesheet.

Example 4 — Inline End-of-Line Comment

Add a short note after a property to explain a non-obvious value.

comment-inline.css
.card {
  padding: 1rem 1.25rem;
  background: #eff6ff;
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px rgba(0,0,0,0.08); /* subtle depth */
}
Try It Yourself

How It Works

Inline comments sit on the same line as a declaration. Use them sparingly for values that need context.

💬 Usage Tips

  • Explain why, not what — Good: /* fix Safari flex gap */. Bad: /* set display to flex */.
  • Section headers — Use banner comments to divide large stylesheets into clear regions.
  • Keep comments current — Update or remove notes when the code changes.
  • Short inline notes — One line after a tricky property is enough.
  • Use version control — Git tracks history; you do not need to keep old code in comments.

⚠️ Common Pitfalls

  • Using // syntax// is not valid CSS and can break your stylesheet.
  • Nested comments/* outer /* inner */ */ does not work; the first */ closes the comment.
  • Comments before @charset — Can invalidate @charset in external CSS files.
  • Outdated comments — Misleading notes are worse than no comments at all.
  • Secrets in comments — Comments are visible in source code and DevTools.

♿ Accessibility

  • Comments are invisible — Never put essential information only in comments; users cannot read them.
  • Screen readers ignore comments — Accessibility text must be in HTML content, not CSS notes.
  • Do not hide content via comments — Use proper CSS or HTML patterns instead of commenting out accessible markup.
  • Document a11y fixes — Comments can explain focus styles or contrast overrides for maintainers.
  • Keep file size reasonable — Very large comment blocks slightly increase CSS file size.

🧠 How CSS Comments Work

1

Parser reads CSS

The browser loads your stylesheet and scans for rules and declarations.

Parse
2

/* */ blocks are skipped

Everything between /* and */ is treated as a comment and ignored.

Skip
3

Valid rules are applied

Only non-commented selectors and properties affect the page appearance.

Apply
=

Documented, styled page

Developers see notes in source; visitors see only the applied styles.

🖥 Browser Compatibility

CSS comments using /* */ have universal support in every browser, including Chrome, Firefox, Safari, Edge, and Opera.

Baseline · Universal support

CSS /* */ comments

Comments work identically in all browsers and have been part of CSS since the beginning.

100% All browsers
Google Chrome All versions
Full support
Mozilla Firefox All versions
Full support
Apple Safari All versions
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
CSS comments 100% supported

Bottom line: /* */ comments work everywhere. The only syntax to remember is that CSS does not support //.

🎉 Conclusion

CSS comments are a simple but essential tool for writing maintainable stylesheets. Use /* */ to document your code, organize sections, and temporarily disable rules during development.

Write comments that explain why, keep them up to date, and never put sensitive data inside them. With good commenting habits, your CSS becomes easier for everyone on your team to understand.

💡 Best Practices

✅ Do

  • Use /* */ for all CSS comments
  • Explain non-obvious styles and browser workarounds
  • Add section headers in large stylesheets
  • Update comments when code changes
  • Use comments to disable rules during debugging

❌ Don’t

  • Use // — it is not valid CSS
  • Comment on every obvious line of code
  • Store passwords or API keys in comments
  • Place comments before @charset in external CSS
  • Leave large blocks of dead code commented out permanently

Key Takeaways

Knowledge Unlocked

Five things to remember about CSS comments

Use these points when writing and maintaining stylesheets.

5
Core concepts
hide 02

Invisible

Not on page.

Render
doc 03

Document

Explain why.

Purpose
off 04

Disable

Debug rules.

Debug
no// 05

No //

CSS differs.

Rule

❓ Frequently Asked Questions

Wrap your note in /* and */. Everything between those markers is ignored by the browser. A comment can sit on one line or span multiple lines.
No. Comments are hidden from visitors. However, they are still visible in View Source, browser DevTools, and downloaded CSS files — never put secrets in comments.
No. Unlike JavaScript, CSS only supports /* */ comments. Using // will not work and may cause parsing errors depending on context.
Yes. Wrap a rule or block in /* */ to temporarily turn it off during debugging. Remove the comment markers when you want the styles back.
In strict interpretation, nothing — not even comments — should appear before @charset in an external CSS file. Place @charset as the very first line when you use it.

Practice in the Live Editor

Open the HTML editor, add /* */ comments in a <style> block, and see how they stay invisible on the page.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful