tinycss2

Low-level CSS Syntax Level 3 parser and generator for Python

Library
PyPI
v1.5.1
191 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
67 /100 Good
Development Activity 68
Maintenance 48
Community 72
Maturity 60
Momentum 20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
77 /100 Good
Architecture 82
Code Quality 80
Innovation 70
Learning Curve 75

tinycss2 is a low-level CSS parser and serializer for Python that implements the CSS Syntax Level 3 specification. It tokenizes and parses CSS text into a tree of token/rule objects and can generate CSS text back from those objects, but deliberately stops at the syntax layer: it knows the grammar of CSS (rules, blocks, declarations, at-rules) without knowing the semantics of any specific property or module, leaving that interpretation to the caller.

Maintained by Kozea and supported by CourtBouillon, tinycss2 is the CSS tokenizer/parser underneath WeasyPrint (the HTML-to-PDF renderer) and other tools that need standards-compliant, low-level CSS parsing without pulling in a full browser engine. Its only runtime dependency is webencodings, keeping it lightweight enough to embed in document-processing and static-site pipelines.

What You Get

  • A CSS tokenizer that turns raw CSS text into a stream of syntax tokens per the CSS Syntax Level 3 spec
  • Parsers for stylesheets, rule lists, declaration lists, and individual component values
  • A serializer that turns the parsed AST back into valid CSS text
  • Color parsing modules covering CSS Color 3, 4, and 5 syntaxes (color3.py, color4.py, color5.py)
  • An nth.py helper for parsing :nth-child()-style An+B expressions

Common Use Cases

  • Building document-rendering pipelines (as WeasyPrint does) that need standards-compliant CSS parsing without a full browser engine
  • Writing CSS preprocessors, linters, or minifiers that operate on a syntax-level AST rather than regex-matching CSS text
  • Extracting or rewriting specific CSS constructs (colors, selectors, at-rules) programmatically in build tooling
  • Parsing embedded or user-supplied CSS safely in applications that sanitize or transform stylesheets before use

Under The Hood

Architecture: The library is split cleanly along the CSS Syntax Level 3 spec’s own phases: tokenizer.py implements the low-level tokenizer, parser.py builds on it to parse stylesheets/rule-lists/declarations into an AST defined in ast.py, and serializer.py reverses the process back to CSS text. Separate color3.py/color4.py/color5.py modules layer optional color-value parsing on top without coupling it to the core tokenizer, and nth.py provides a focused parser for An+B micro-syntax used in selectors.

Tech Stack: Pure Python 3.10+, built with the flit_core build backend, with a single runtime dependency (webencodings) for correct CSS byte-to-text decoding per the spec’s encoding-detection rules.

Code Quality: The tests/ directory exercises the tokenizer, parser, serializer, and each color-syntax module against spec-derived cases; the project has a documented code of conduct and is professionally maintained by CourtBouillon, with a public changelog tracked via GitHub releases. Commit activity is modest but steady, consistent with a stable, spec-scoped library that doesn’t need frequent breaking changes.

API Design: The public API exposes a small number of top-level functions (parse_stylesheet, parse_declaration_list, parse_component_value_list, serialize) that map directly onto CSS Syntax Level 3 spec productions, so anyone familiar with the spec can predict the API shape; because it stays at the syntax level, callers must bring their own knowledge of which properties/values are valid, which is a deliberate trade-off rather than an oversight.

Used by 5 apps in this directory

Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,125

Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.

View details
65
Repo Health
81
Technical
61
Dependency
Built with
Python 59%
Jupyter Notebook 21%
Updated 1 weeks ago
Python
70%
Apache 2.0

GPT Researcher

AI Assistants · Productivity

29,650

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
91
Repo Health
91
Technical
63
Dependency
Built with
Python 70%
TypeScript 18%
Updated 6 days ago
HTML
46%
LGPL-2.1

Horilla

ERP · Human Resources

1,437

Open-source HRMS covering recruitment, attendance, payroll, and biometrics in one self-hosted Django application.

View details
91
Repo Health
60
Technical
65
Dependency
Built with
HTML 46%
Python 38%
JavaScript 12%
Updated 4 days ago
TypeScript
44%
Other

Magic

AI Agents · Automation · Low Code Platforms

5,043

Magic is an enterprise-grade open-source AI agent platform combining a generalist AI agent, workflow engine, IM, and collaborative office system for running an AI-powered digital workforce.

View details
70
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 44%
PHP 32%
Updated 1 months ago
Python
97%
MPL 2.0

Taiga Back

Developer Tools · Project Management

854

Self-hosted agile project management backend with Scrum, Kanban, issue tracking, and a full REST API — built on Django and PostgreSQL.

View details
76
Repo Health
74
Technical
64
Dependency
Built with
Python 97%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers