Academic Block

CSS Comment Get Certified

What are CSS Comments?

CSS comments are notes written inside a CSS file. Browsers ignore comments, so they do not change the appearance of a webpage. Comments help you explain your code, organize sections, and make your CSS easier to understand.

Why Use CSS Comments?

Beginners use comments to remember what a CSS rule does. Professional developers use comments to organize large stylesheets, leave notes, and temporarily disable CSS while testing.

Syntax of CSS Comments

A CSS comment starts with /* and ends with */. Anything between these symbols is ignored by the browser.

What this code does: Demonstrates a basic CSS comment followed by simple body, heading, and paragraph rules. The comment is completely ignored by the browser.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* This text is a comment. The browser ignores it. */
body {
    font-family: Arial, sans-serif;
    background: #f0f7ff;
    padding: 24px;
}
h1 {
    color: #0d6efd;
}
p {
    color: #334155;
    font-size: 18px;
}
</style>
</head>
<body>
<h1>CSS Comments</h1>
<p>The comment above is ignored by the browser.</p>
</body>
</html>

Step 1: Create Your First CSS Comment

Every CSS comment starts with /* and ends with */. Comments alone do not style the page — you still need CSS rules.

What this code does: Adds a simple comment at the top, then styles an h1 purple and a paragraph gray. The comment has no visual effect.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* My first CSS comment */
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
h1 {
    color: #4f46e5;
}
p {
    color: #475569;
    font-size: 18px;
}
</style>
</head>
<body>
<h1>My First Comment</h1>
<p>The comment does not change how this page looks.</p>
</body>
</html>

Step 2: Add a Comment Before a Rule

Place a comment above a rule to explain what the rule does. The comment is ignored; only the rule is applied.

What this code does: Places explanatory comments directly above two rules so future readers (or you) know why each rule exists.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
/* Make the main heading blue */
h1 {
    color: blue;
}
/* Style the paragraph text */
p {
    color: #333;
    font-size: 18px;
    line-height: 1.6;
}
</style>
</head>
<body>
<h1>Blue Heading</h1>
<p>Each rule above has a comment explaining its purpose.</p>
</body>
</html>

Step 3: Organize Your CSS with Section Comments

Use section comments to group related rules. This keeps large stylesheets easier to read.

What this code does: Divides the stylesheet into clear sections (Header, Paragraph, Button) using decorative comments so the code stays organized.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
/* ===== Header styles ===== */
h1 {
    color: #0d6efd;
    margin-bottom: 12px;
}
/* ===== Paragraph styles ===== */
p {
    color: #555;
    font-size: 18px;
}
/* ===== Button styles ===== */
button {
    background: #0d6efd;
    color: white;
    border: none;
    padding: 10px 18px;
    border-radius: 6px;
    cursor: pointer;
}
</style>
</head>
<body>
<h1>Section Comments</h1>
<p>Related rules are grouped under section headers.</p>
<button>Click Me</button>
</body>
</html>

Step 4: Disable a Property While Testing

Comment out one property to turn it off without deleting it. In the example below, font-size: 40px is disabled, so the heading stays the default size.

What this code does: Temporarily turns off the large font-size by commenting it out. The heading keeps its color but uses the browser’s default size.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
h1 {
    color: #0056b3;
    /* font-size: 40px;  ← this property is disabled */
}
p {
    color: #444;
    font-size: 18px;
}
</style>
</head>
<body>
<h1>Default Size Heading</h1>
<p>The large font-size is commented out, so it does not apply.</p>
</body>
</html>

Step 5: Disable an Entire CSS Rule

Wrap a full rule in a comment to disable it. Below, the red heading rule is off, so only the green paragraph style is applied.

What this code does: Completely disables a red heading rule by wrapping it in a multi-line comment, leaving only the green paragraph rule active.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
/*
h1 {
    color: red;
    font-size: 40px;
}
*/
p {
    color: green;
    font-size: 18px;
}
h1 {
    color: #0f172a;
}
</style>
</head>
<body>
<h1>Dark Heading (red rule is disabled)</h1>
<p>This paragraph is green because its rule is active.</p>
</body>
</html>

Complete Example

A small page stylesheet organized with comments. Try editing colors in the preview.

What this code does: Shows a complete, well-commented stylesheet for a simple page (body, heading, paragraph, button + hover). Every section is clearly labeled.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* Page background and base text */
body {
    font-family: Arial, sans-serif;
    background: #f4f8ff;
    padding: 24px;
    color: #1e293b;
}
/* Main heading */
h1 {
    color: #0056b3;
    margin-bottom: 10px;
}
/* Supporting text */
p {
    color: #444;
    font-size: 18px;
    line-height: 1.6;
}
/* Primary button */
button {
    background: #007BFF;
    color: white;
    border: none;
    padding: 10px 18px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
}
/* Button hover state */
button:hover {
    background: #0056b3;
}
</style>
</head>
<body>
<h1>Complete Example</h1>
<p>This page uses a well-commented stylesheet.</p>
<button>Click Me</button>
</body>
</html>

Common Mistakes

Always close comments with */. Unclosed comments can disable the rest of your CSS. Also never use HTML comments (<!-- -->) inside CSS.

What this code does: Shows the correct way to write and close comments so the following CSS rules continue to work.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
/* CORRECT: comment is closed, then rules work */
/* Style the heading */
h1 {
    color: #dc2626;
}
/* Style the paragraph */
p {
    color: #166534;
    font-size: 18px;
}
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
</style>
</head>
<body>
<h1>Red Heading</h1>
<p>Green paragraph — both rules work because comments are closed.</p>
</body>
</html>

Best Practices

  • Write short and meaningful comments.
  • Use comments before important sections.
  • Keep comments updated.
  • Always close comments using */.
  • Use comments while testing CSS.
  • Remove unnecessary comments before publishing.
  • Avoid HTML comments inside CSS.
  • Organize large stylesheets with section comments.

Web Resources on How to Add Comments in CSS

1. MDN Web Docs : Add Comments in CSS
2. CSS Tricks.com : The Art of Comments
3. Dev.to : How and Why we add comments in CSS
4. SASS Lang.com : Comments

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.