Organize styles with comments and find common errors.
CSS can look correct in an editor while producing an unexpected result in the browser. Comments and a few debugging habits can help you find out why.
Debugging means investigating why the page behaves differently from what you intended, then checking that a change fixes it.
A CSS comment starts with /* and ends with */. The browser ignores its contents when applying styles:
/* Heading */
h1 {
color: green;
}A comment can also span several lines:
/*
Heading styles for the page.
Keep related declarations together.
*/
h1 {
color: green;
font-size: 18px;
}Use comments to label sections or explain a decision that would otherwise be unclear. Keep these syntax rules in mind:
/* ... */ for both single-line and multiline comments.//; it is not a CSS comment marker.*/ closes the comment, even if another /* appears inside it.Put a declaration inside a comment to turn it off temporarily:
h1 {
/* color: green; */
font-size: 18px;
}Here, the font size still applies, but this rule no longer supplies a text color. Another rule or the browser's default styles can determine the color.
This is useful when testing which declaration causes a problem. Restore the declaration after the experiment if you still need it.
Suppose this rule does not change the heading as expected:
h1 {
color: green
font-size: 18px;
}The first declaration is missing a semicolon ;. The browser reads the next line as part of the color value, effectively producing green font-size: 18px. That is not a valid color, so neither intended declaration takes effect.
Add the separator:
h1 {
color: green;
font-size: 18px;
}The browser skips invalid CSS and continues reading where it can. That is why the page still appears even though these styles do not work. The semicolon lets the browser read the two declarations separately.
Use one small loop instead of changing several things at once:
; after color: green.Two changes failing together are a clue. Fixing just the separator lets you test whether they shared a cause.
Now complete the same process for a new symptom: a .notice rule is present, but the paragraph remains unchanged. First check whether the selector matches the paragraph, then inspect the declaration for a valid property and value, and finally check for a competing rule. This order moves from “does the rule apply?” to “can the browser read it?” to “which valid rule wins?”
Browser developer tools, usually called DevTools, let you inspect the HTML and CSS behind the displayed page.
In Chrome, right-click the heading and choose Inspect. In the Elements panel:
color or font-size.The Chrome DevTools CSS reference describes these views.
You can edit a value or temporarily disable a declaration in DevTools to test an idea immediately. For a lasting fix, make the corresponding change in your source file, save it, and reload the page.
Use this checklist to narrow down the cause:
Change one thing, observe the result, and then continue. If the problem is hard to isolate, reduce the example to the affected HTML and the rules needed to reproduce it.
Try it yourself
HTML + CSS
/* ... */ for notes or temporary experiments, and avoid nesting comments.