{"id":1434,"library":"cssutils","title":"cssutils CSS Parser and Manipulator","description":"cssutils is a Python library for parsing and manipulating CSS (Cascading Style Sheets). It provides a full DOM interface for CSS stylesheets, rules, and declarations, allowing for programmatic creation, modification, and serialization of CSS. The library is currently at version 2.11.1 and sees regular maintenance releases, typically every few months.","status":"active","version":"2.11.1","language":"en","source_language":"en","source_url":"https://github.com/jaraco/cssutils","tags":["css","parser","stylesheet","css-parser","dom"],"install":[{"cmd":"pip install cssutils","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"cssutils","correct":"import cssutils"},{"note":"The 'cssutils.css' module and custom classes were removed in version 2.x. All functionality is now accessed through the main `cssutils` module via the W3C DOM API.","wrong":"from cssutils.css import CSSStyleSheet","symbol":"CSSStyleSheet","correct":"import cssutils\nsheet = cssutils.CSSStyleSheet()"}],"quickstart":{"code":"import cssutils\nimport logging\n\n# Suppress cssutils logging for cleaner output in example\ncssutils.log.setLevel(logging.CRITICAL)\n\n# Parse a CSS string\nsheet = cssutils.parseString('a { color: red; font-size: 16px; }')\n\n# Accessing rules and properties\nrule = sheet.cssRules[0] # Get the first rule\nprint(f\"Selector: {rule.selectorText}\") # Output: Selector: a\nprint(f\"Color: {rule.style.color}\") # Output: Color: red\nprint(f\"Font Size: {rule.style.fontSize}\") # Output: Font Size: 16px\n\n# Modifying a rule\nrule.style.color = 'blue'\n\n# Creating a new stylesheet and adding a rule\nnew_sheet = cssutils.CSSStyleSheet()\nnew_sheet.add('p { margin: 10px; }')\n\n# Add the modified rule from the first sheet to the new one\nnew_sheet.add(rule)\n\n# Serialize the new stylesheet to a string\nprint('\\n--- New Stylesheet ---')\nprint(new_sheet.cssText)","lang":"python","description":"This quickstart demonstrates parsing a CSS string, accessing and modifying properties within a CSS rule, and creating a new CSS stylesheet programmatically. It also shows how to serialize a `CSSStyleSheet` object back into a CSS string."},"warnings":[{"fix":"Rewrite code to use the main `cssutils` module and its W3C DOM-compliant methods and objects (e.g., `cssutils.parseString()`, `cssutils.CSSStyleSheet()`). Do not use `from cssutils.css import ...`.","message":"Major API overhaul in version 2.0.0. All custom API previously found in `cssutils.css` (e.g., `cssutils.css.CSSStyleSheet`, `cssutils.css.Rule`) and other custom classes were removed. All functionality is now exclusively available through the W3C DOM interface.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Customize serialization preferences using `cssutils.ser.prefs`. For example, `cssutils.ser.prefs.indent = 4` to set indentation or `cssutils.ser.prefs.keepemptyrules = False` to remove empty rules.","message":"Default serialization (`cssText`) might not produce the exact output format expected (e.g., indentation, line breaks, specific property ordering).","severity":"gotcha","affected_versions":"All 2.x versions"},{"fix":"Configure the `cssutils.log` level to `INFO` or `DEBUG` to see more detailed parsing messages, especially during development or debugging problematic CSS. Example: `cssutils.log.setLevel(logging.INFO)`.","message":"cssutils uses Python's `logging` module for warnings and errors during parsing. By default, only `WARNING` and above messages might be shown, potentially masking minor parsing issues.","severity":"gotcha","affected_versions":"All 2.x versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}