cssutils
A CSS Cascading Style Sheets library for Python
What it is and what it does
cssutils is a CSS parser and DOM builder for Python that reads CSS stylesheets into a structured object model you can inspect and modify programmatically. It implements the W3C DOM Level 2 Style specification and supports CSS 2.1 and parts of CSS 3, including namespaces, media queries, page rules, and custom properties. The package does not render CSS—it is purely for parsing, validation, and serialization.
You use it to load a CSS string or file, walk through rules and properties, modify values or priorities, add or remove rules, and serialize the result back to text. It depends on more_itertools and encutils for utility functions. The library is mature (first released in 2007) and actively maintained, though it is not thread-safe and requires Python 3.10 or later.
Use it for:
- Programmatically modify CSS stylesheets in build pipelines or code generation tools.
- Validate CSS syntax and property values before deployment or in linting workflows.
- Extract and analyze CSS rules, selectors, and properties from stylesheets for auditing or transformation.
- Build CSS dynamically by constructing rules and properties in code and serializing them to text.
- Parse CSS from HTML documents or external sources and manipulate them as structured data.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
cssutils parses and builds CSS stylesheets as a DOM structure without rendering—it reads, modifies, and serializes CSS according to W3C specifications.
Yes, if you need to parse, validate, or programmatically modify CSS as structured data. Install friction is low, maintenance is active, and there are no known security vulnerabilities. The copyleft license (LGPL-3.0-or-later) requires attention if you plan proprietary distribution. Not suitable for rendering or for thread-concurrent use.
Install
cssutils on PyPI
pip
pip install cssutilsuv
uv add cssutilspoetry
poetry add cssutilsInstalling cssutils
Before you install
Low friction install with only two runtime dependencies (more_itertools, encutils). Active maintenance with a recent release; last commit 2026-04-29. Marked as Production/Stable.
License in practice
Licensed under LGPL-3.0-or-later (copyleft). Derivative works and modifications must be distributed under compatible terms; proprietary use requires careful license review.
Quickstart
import cssutils
css = 'a { color: red; }'
sheet = cssutils.parseString(css)
for rule in sheet:
if rule.type == rule.STYLE_RULE:
rule.style['color'] = 'blue'
print(sheet.cssText)
Requires Python >=3.10. cssutils is known to be thread unsafe.
Verify before relying
- Whether the package handles modern CSS features beyond CSS 2.1 and CSS 3 (e.g., CSS Grid, Flexbox, custom properties edge cases).
- Performance characteristics when parsing large stylesheets or many rules in sequence.
- Specific validation strictness for CSS properties and whether it can be configured.
Package facts
| License | LGPL-3.0-or-later (copyleft) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — more_itertools, encutils |
| Maintenance | actively maintained — 109 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 9,443,090/month — #1,533 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: cssutils-2.15.0-py3-none-any.whl
Keywords: CSS, Cascading Style Sheets, CSSParser, DOM Level 2 Stylesheets, DOM Level 2 CSS
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
dict2cssdict2css converts Python dictionaries to CSS…
permissive · top 15,000 on PyPI
pynlinerConverts CSS stylesheets to inline styles in…
permissive · top 15,000 on PyPI
cssselectcssselect parses CSS3 selectors and translates…
permissive · top 1,000 on PyPI
css-inlineInlines CSS from style and link tags directly…
permissive · top 5,000 on PyPI
pyScssCompiles Sass/SCSS stylesheets to CSS,…
permissive · top 15,000 on PyPI
premailerConverts CSS style blocks into inline style…
permissive · top 5,000 on PyPI
tinycss2tinycss2 parses CSS strings into token and…
permissive · top 1,000 on PyPI
cssselect2cssselect2 parses and matches CSS4 selectors…
permissive · top 1,000 on PyPI
dominateDominate is a Python library for writing HTML…
copyleft · top 5,000 on PyPI
cssbeautifierBeautifies, unpacks, and deobfuscates CSS code…
permissive · top 5,000 on PyPI