Skip to content

Adding Comments to Your CSS

CSS, Web Design, Frontend Development1 min read

CSS serves as the foundation for defining the layout, colors, fonts, and overall styling of a webpage. As projects expand and multiple developers contribute to the codebase, comprehending the intent behind specific style rules becomes vital. This is where comments come into play. Comments in CSS provide valuable context and insights about the purpose of certain styles, their application, and any other relevant information that aids in understanding the code.

How to Add Comments in CSS

Adding comments in CSS is straightforward. There are two primary ways to include comments: single-line comments and multi-line comments.

Single-Line Comments

Single-line comments are denoted by // and are ideal for adding brief, one-line explanations within your stylesheet. These comments are not rendered by the browser and serve as notes exclusively for developers working on the code.

1/* This is a single-line comment */
2.element {
3 property: value; /* Inline comment here */

Multi-Line Comments

Multi-line comments, enclosed between /* and */, are useful for more extensive annotations, descriptions, or disclaimers. They are particularly handy for documenting sections of code or explaining complex styling decisions.

2This is a multi-line comment.
3It can span across
4multiple lines.
6.element {
7 property: value;

Best Practices for Commenting CSS

While adding comments to your CSS can be immensely beneficial, it's equally important to follow best practices to ensure that your comments remain helpful and do not clutter the codebase unnecessarily.

  1. Be Clear and Concise: Ensure that your comments are clear, concise, and directly relevant to the code they accompany. Avoid ambiguous or overly wordy explanations.

  2. Document Intents and Exceptions: Use comments to document the intent behind specific styles and any exceptions or unusual cases. This provides valuable context for future modifications.

  3. Update and Maintain Comments: Regularly review and update your comments as the code evolves. Outdated comments can lead to confusion and misinterpretation.

  4. Avoid Redundancy: While comments are essential for understanding intricate sections of code, refrain from stating the obvious. Strive to add value with each comment.

Examples of Effective CSS Comments

Let’s examine a few examples of how comments can be used effectively in CSS:

Example 1: Providing Context

1/* Set border color for active state */ {
3 border-color: #4CAF50; /* Green */

Example 2: Documenting Browser-Specific Styles

1/* Target Internet Explorer 10 and 11 */
2@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
3 .element {
4 background-color: #f1f1f1; /* IE-specific background color */
5 }

Example 3: Explaining Complex Layouts

2 Main content area with flexible width,
3 taking into account the sidebar and margins.
5.main-content {
6 width: calc(100% - 250px); /* Adjust for sidebar width */
7 margin: 0 20px; /* Keep some space for readability */