Comments and syntax recovery
Definition and purpose
CSS comments are notes between /* and */. They are ignored by the CSS parser. Browsers recover from many errors by discarding invalid declarations while keeping valid neighbors, so a typo does not always ruin the entire stylesheet.
Invalid property, valid neighbor
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> p { colorr: red; /* typo */ color: navy; background-color: white; } </style> </head> <body> <p id="a">Stock updated</p> </body></html>The invalid property is discarded, while the valid declarations apply.
Syntax and mental model
A misspelled property such as colorr is ignored. A recognized property with an invalid value is also ignored. With semicolons, subsequent declarations can still be parsed. Missing a semicolon can merge two intended declarations into one invalid value; missing a closing comment can consume the rest of the stylesheet. Recovery depends on the actual syntax.
Missing separator
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>p { color: navy background-color: white; font-size: 20px; }</style> </head> <body> <p id="a">Check punctuation</p> </body></html>This intentionally broken rule loses the combined invalid declaration; font-size survives.
Common mistakes
CSS does not use // for ordinary comments. Fix the earliest syntax error first, then inspect the remaining declarations; do not assume every later rule survives a missing brace.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Rules and declarations. Next lesson: Browser defaults and developer tools.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Comments and syntax recovery — knowledge check
Five questions on comments and syntax recovery.
Which is a CSS comment?
What happens to an unknown property with a valid semicolon?
Why might color: navy background-color: white; fail?
What is dangerous about an unclosed comment?
Which declaration should survive p { color: nonsense; font-size: 20px; }?