Architecture overview
The CSS engine is organized into several interconnected systems:Parser
Tokenization and parsing of CSS stylesheets
Properties
Property definitions and value handling
Selectors
Element matching and specificity
Cascade
Style resolution and inheritance
CSS parsing
The CSS parser (CSS/Parser/) converts text into structured data:
Tokenization
The tokenizer (Parser/Tokenizer.h) breaks CSS into tokens:
- Identifiers: Property names, keywords
- Functions:
rgb(),calc(),var() - Strings: Text in quotes
- Numbers: With or without units
- Delimiters:
:,;,,,{,} - At-keywords:
@media,@keyframes,@font-face
Component values
Parsed CSS is represented as component values (Parser/ComponentValue.h):
Parser structure
The main parser (Parser/Parser.h) handles CSS grammar:
The parser follows the CSS Syntax Module specification, using a token stream and component value representation that matches the spec’s algorithms.
Property system
CSS properties are defined in JSON files and generated into C++ code.Properties.json
The central property definition file (CSS/Properties.json) contains metadata for all CSS properties:
Generated files
FromProperties.json, the build system generates:
PropertyID.h/cpp: Enum of all propertiesGeneratedCSSStyleProperties.h/cpp: Property metadata functionsGeneratedCSSStyleProperties.idl: JavaScript bindings
Property parsing
Custom property parsing goes inCSS/Parser/PropertyParsing.cpp:
Properties.json definition. Custom parsing is only needed for complex grammars.
CSS values
CSS values are represented byStyleValue subclasses (CSS/StyleValues/):
Value types
- Keywords:
auto,none,inherit,initial - Lengths: Pixels, ems, percentages
- Colors: Named, hex, rgb(), hsl()
- Images:
url(), gradients - Calculations:
calc(),min(),max(),clamp() - Lists: Space or comma separated values
Computed values
Values go through computation to resolve relative units:Selectors
Selectors (CSS/Selector.h) determine which elements styles apply to:
Selector specificity
Specificity determines which rules win when multiple match:- ID selectors: (1, 0, 0) -
#header - Class selectors: (0, 1, 0) -
.button,[type="text"],:hover - Type selectors: (0, 0, 1) -
div,span,::before
Pseudo-classes
Defined inCSS/PseudoClasses.json:
Pseudo-elements
Defined inCSS/PseudoElements.json:
::before- Insert content before element::after- Insert content after element::first-line- Style first line of text::first-letter- Style first letter::selection- Style selected text
The cascade
The cascade determines the final computed style for each element:Cascade algorithm
- Collect declarations: Gather all matching rules
- Sort by origin and importance:
- User-agent important declarations
- User important declarations
- Author important declarations
- Author normal declarations
- User normal declarations
- User-agent normal declarations
- Sort by specificity: Higher specificity wins
- Sort by order: Later declarations win
Style inheritance
Inherited properties (Properties.json with "inherited": true) automatically cascade from parent to child:
color,font-family,font-sizeline-height,text-alignvisibility,cursor
width,height,margin,paddingborder,background,positiondisplay,overflow
Keywords and enums
Keywords.json
All CSS keywords are defined inCSS/Keywords.json:
Enums.json
Grouped keyword sets become enums (CSS/Enums.json):
Adding or modifying properties
As documented inDocumentation/CSSProperties.md, adding a CSS property involves:
1. Update JSON data
EditCSS/Properties.json:
2. Add custom parsing (if needed)
Most properties parse automatically. For complex ones, add toCSS/Parser/PropertyParsing.cpp:
3. Add computed value getter
InCSS/ComputedValues.h:
4. Use the property
In layout or painting code:The build system automatically regenerates C++ code from JSON files. No manual enum editing needed!
Media queries
Media features are defined inCSS/MediaFeatures.json:
At-rules
At-rules like@font-face, @media, @keyframes are defined in CSS/Descriptors.json:
Transform functions
CSS transforms are defined inCSS/TransformFunctions.json:
Math functions
CSS math functions (CSS/MathFunctions.json):
Units
All CSS units are defined inCSS/Units.json:
Quirks mode
CSS quirks mode provides compatibility with old browsers:- Hashless hex colors:
color: fffinstead of#fff - Unitless lengths:
width: 100instead of100px
Related components
LibWeb
Overall rendering engine architecture
DOM
Document structure and elements
Layout
How computed styles become visual boxes