XML technologies / CSS / CSS Cookbook / General topics / Comments in CSS code

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to use comments to structure your CSS code and make it more readable.

Solution

Use the characters /* and */ to insert comments anywhere in your code:

/* Dies ist ein Kommentar */
a {
  text-decoration: none;
}
/* Und dies ebenfalls */
h1, h2 {
  font-size: 100%;
  color: #666666;
}

Discussion

One day, you may look at a stylesheet you wrote a long time ago and no longer remember why you took certain steps. Comments help explain and organize your code so that it remains understandable later. They also help people who did not write the code understand what was intended. Anything between /* and */ is ignored by the browser.

If you divide your code into sections, comments help distinguish them (header, footer, navigation, subnavigation, and so on). Comments can also make testing your pages much easier. Sometimes you are unsure whether a rule is correct or what effect it has. Simply enclose the rule in comment delimiters to disable it:

/*
a {
  text-decoration: none;
}
*/

The rule no longer affects the page’s appearance because the property text-decoration has been commented out. Unless other rules apply to the tag a, links will be underlined again until you remove the comment delimiters.

See also

The CSS 2.1 specification on comments.

  

 

 

 

back to top