Registry / serialization / tinycss2

tinycss2

JSON →
library1.5.1pypypi✓ verified 26d ago

tinycss2 is a low-level CSS parser and generator written in Python. It can parse CSS strings, return objects representing tokens and blocks, and generate CSS strings from these objects. Based on the CSS Syntax Level 3 specification, it handles the grammar of CSS but does not know specific rules, properties, or values. It is actively maintained with regular releases.

pip install tinycss2
INSTALL
IMPORT
SIG · TINYCSS2
T
tinycss2
serializationpythonv1.5.1
Install
1.8s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.8s · import 0.010s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse_stylesheet
import tinycss2 rules = tinycss2.parse_stylesheet('body { color: red; }')
Primary function for parsing full CSS stylesheets.
ast nodes (e.g., QualifiedRule, IdentToken)
from tinycss2.ast import QualifiedRule, IdentToken
While often not directly imported, AST node classes reside in the `tinycss2.ast` module for inspection and manipulation of parsed output.

Parses a CSS string into an Abstract Syntax Tree (AST) of tokens and blocks, then iterates through the parsed rules and declarations.

import tinycss2 css_input = '#my-id div { width: 50%; height: 100px }' rules = tinycss2.parse_stylesheet(css_input) # The parsed output is a list of nodes for rule in rules: if rule.type == 'qualified-rule': print(f"Qualified Rule: {rule.prelude[0].value} {rule.prelude[2].value}") # Example: 'my-id', 'div' for declaration in tinycss2.parse_declaration_list(rule.content): if declaration.type == 'declaration': print(f" Declaration: {declaration.name}: {declaration.value[0].value}{declaration.value[0].unit}") # To serialize back to CSS (approximate) serialized_css = ''.join(node.serialize() for node in rules) print(f"\nSerialized CSS: {serialized_css}")
Debug
Known issues
deprecatedThe `tinycss2.parse_declaration_list` function was deprecated in v1.3.0.
fix
Use `tinycss2.parse_blocks_contents` instead for parsing declaration lists.
affects: >=1.3.0
breakingPython 3.6 support was dropped in v1.2.0, and Python 3.5 support in v1.1.0. The library currently requires Python 3.10 or newer.
fix
Ensure your project uses Python 3.10 or a more recent compatible version.
affects: >=1.2.0
gotchatinycss2 is a low-level CSS parser. It understands CSS syntax (tokens, blocks, qualified rules, at-rules) but does not interpret the semantics of specific CSS properties, values, or selectors. It does not provide high-level representations of browser-rendered styles.
fix
Users are expected to build their own higher-level logic on top of tinycss2's parsed output if they need to interpret CSS rules beyond their grammatical structure.
affects: All versions
breakingIn tinycss2 v0.5, the default behavior for parsing functions changed to preserve CSS comments and top-level whitespace in the output AST. Previously, these were skipped by default.
fix
If upgrading from very old versions and expecting comments/whitespace to be omitted, pass `skip_comments=True` and `skip_whitespace=True` to parsing functions like `parse_stylesheet` or `parse_declaration_list`.
affects: >=0.5
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tinycss2'
The tinycss2 library is not installed in your current Python environment.
fix
Install the tinycss2 package using pip: `pip install tinycss2`
TypeError: argument must be a string or bytes, not <class 'int'>
Parsing functions like `tinycss2.parse_stylesheet()` expect a string or bytes object as input, but received an incompatible type.
fix
Ensure the input to the parsing function is a `str` (for `parse_stylesheet`) or `bytes` (for `parse_stylesheet_bytes`) object. For example, `css_string = '.selector { color: red; }'`.
AttributeError: 'list' object has no attribute 'type'
The `tinycss2.parse_stylesheet()` function returns a list of tokens, not a single token object, so you must iterate over the list to access individual token attributes like `type` or `value`.
fix
Iterate over the returned list to access attributes of individual tokens: `for token in tinycss2.parse_stylesheet(css_string): print(token.type)`
ImportError: cannot import name 'parse_stylesheet_string' from 'tinycss2'
The function name `parse_stylesheet_string` does not exist in the `tinycss2` module; the correct function for parsing a CSS string is `parse_stylesheet`.
fix
Use the correct import and function name: `from tinycss2 import parse_stylesheet`
Upgrade
Version history
1.5.1latest on PyPI · released Nov 23, 2025
Audit
Dependencies
webencodingsrequiredCharacter encoding aliases for legacy web content.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
tinycss2 — pip install tinycss2 · libregistry