Tokenization and parsing
Definition and purpose
CSS parsing converts source text into tokens and syntactic structures, then validates declarations against property grammar. Error recovery allows many valid declarations and later rules to survive invalid input.
Invalid value recovery
<!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> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { color: blue; color: 12px; font-weight: 700; } #b { color: green; } </style> </head> <body> <p id="a">Status</p> <p id="b">Next rule</p> </body></html>The invalid color is ignored; surrounding declarations survive.
Syntax and mental model
A declaration has a property name, colon and value. An unknown property or invalid value is normally ignored; a later valid declaration can still apply. An invalid selector in a normal selector list invalidates the whole rule, unlike forgiving selector lists such as :is(). Missing delimiters can consume later tokens, so recovery is not guaranteed to preserve every subsequent rule. Inspect the CSSOM for accepted rules and declarations rather than assuming the source is the parsed representation.
Forgiving list
<!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> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a, :no-such-pseudo { color: red; } :is(#a, :no-such-pseudo) { color: blue; } </style> </head> <body> <p id="a">Status</p> <p id="b">Next rule</p> </body></html>The normal list is invalid; :is() retains the valid branch.
Common mistakes
Do not confuse an invalid declaration at parse time with invalidity after var() substitution; the latter can use defaulting instead of falling back to an earlier declaration.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Animation and transition precedence, Formatting context edge cases, Subgrid and intrinsic contributions, Animation performance. Next lesson: CSSOM and stylesheet APIs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tokenization and parsing — knowledge check
Five questions on tokenization and parsing.
What happens to color:12px?
What happens to a normal selector list with an invalid member?
Which is a forgiving selector list?
Where can accepted declarations be inspected?
What can a missing semicolon do?