Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

CSS Syntax, Parsing, and Error Recovery

Learn how CSS source becomes tokens and parsed constructs, and how conforming parsers recover from malformed input without turning the lesson into a catalog of property grammars.

Levels: Beginner ~12 min read

Basic

CSS is parsed before it can style anything

A CSS source file is text, but the CSS processing model does not reason about arbitrary characters as if every character were already meaningful syntax. CSS Syntax defines tokenization and parsing rules that turn source text into CSS constructs.

Interactive Runnable example
Edit

Ready to run.

Tokens are the language building blocks

CSS Syntax defines token types such as identifiers, functions, numbers, dimensions, percentages, strings, hashes, delimiters, comments, and whitespace. Other CSS specifications then define how sequences of these tokens form selectors, declarations, property values, and at-rules.

css
1
.notice { margin: 1rem; width: 50%; }

Quick check

Which specification layer primarily defines how CSS source is tokenized?

Whitespace and comments are not styling instructions

Whitespace and comments can separate or annotate source without becoming declarations themselves. Some whitespace is significant to how tokens are formed, while comments are removed during preprocessing. Do not assume that visual formatting in a stylesheet maps one-to-one to the parsed structure.

css
1
2
3
4
/* The comment is not a declaration. */
.card {
  color: black;
}

A missing semicolon is not the same as a missing rule

CSS is designed to keep parsing when it encounters many kinds of malformed input. Error handling is part of the language model: a browser can discard an invalid component while continuing to process later source. This forward-compatible behavior is one reason CSS can evolve without requiring every older parser to understand every future feature.

Interactive Runnable example
Edit

Ready to run.

Grammar ownership is distributed across CSS

CSS Syntax provides the general parsing machinery and value-definition conventions. Selectors defines selector grammar. Individual modules define the grammar of the properties and at-rules they introduce. This prevents the Syntax lesson from becoming a catalog of every CSS feature.

Syntax errors versus unsupported features

A parser can understand the general shape of a construct without being able to apply a feature that a particular implementation does not support. Curriculum examples should therefore distinguish malformed CSS from a valid feature that is outside a target implementation or still developing in the specification.

Quick check

If a browser ignores one invalid declaration but continues to apply a later valid declaration, what concept explains this behavior?

Specification status matters

CSS Syntax Level 3 is a Candidate Recommendation Draft in the W3C publication history. That status is different from a Recommendation, and developing CSS modules may have different maturity levels. The curriculum records those distinctions instead of presenting every draft feature as settled CSS.

Normative source: W3C CSS Syntax Module Level 3.

Quick check

If you can explain the path from source text to tokens to parsed CSS constructs, and why malformed input can be discarded without stopping the entire stylesheet, you understand the shared parsing model. Property-specific grammars remain with their defining modules.

Was this page helpful?

Mark this learning resource complete to track your learning progress.