{"id":1814,"library":"cssbeautifier","title":"CSS Beautifier","description":"A Python library for unobfuscating and beautifying CSS code, improving readability and maintainability. It is currently at version 1.15.4 and is part of the larger `js-beautify` project, with releases tied to its development cycle. The library provides programmatic access to format CSS strings or files with configurable options.","status":"active","version":"1.15.4","language":"en","source_language":"en","source_url":"https://github.com/beautifier/js-beautify","tags":["css","beautifier","formatter","code quality","linter","css3"],"install":[{"cmd":"pip install cssbeautifier","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core functionality is built upon and automatically installs `jsbeautifier` as a dependency for shared logic.","package":"jsbeautifier","optional":false},{"reason":"Python 2/3 compatibility layer, often an indirect dependency for projects supporting a broader range of Python versions.","package":"six","optional":true},{"reason":"Used for reading `.editorconfig` files to configure formatting options.","package":"editorconfig","optional":true}],"imports":[{"note":"While `beautify` can be imported directly, the official examples typically use `cssbeautifier.beautify` for clarity.","wrong":"from cssbeautifier import beautify # Directly importing beautify is less common in docs.","symbol":"beautify","correct":"import cssbeautifier\nresult = cssbeautifier.beautify(css_string)"},{"note":"Used to retrieve a configurable options object.","symbol":"default_options","correct":"from cssbeautifier import default_options\nopts = default_options()"}],"quickstart":{"code":"import cssbeautifier\n\nugly_css = \".container{display:flex;}.item{color:red;font-size:16px;margin:0 auto;}\"\n\n# Beautify with default options\npretty_css_default = cssbeautifier.beautify(ugly_css)\nprint(\"--- Default Beautification ---\")\nprint(pretty_css_default)\n\n# Beautify with custom options\nopts = cssbeautifier.default_options()\nopts.indent_size = 2\nopts.end_with_newline = True\nopts.newline_between_rules = False\n\npretty_css_custom = cssbeautifier.beautify(ugly_css, opts)\nprint(\"\\n--- Custom Beautification (2-space indent, no newline between rules) ---\")\nprint(pretty_css_custom)\n\n# Example of beautifying a file (conceptual, requires a file to exist)\n# Assuming 'styles.min.css' exists with ugly_css content\n# with open('styles.min.css', 'w') as f:\n#     f.write(ugly_css)\n# beautified_file_content = cssbeautifier.beautify_file('styles.min.css', opts)\n# print(\"\\n--- Beautified File Content ---\")\n# print(beautified_file_content)","lang":"python","description":"This quickstart demonstrates how to beautify a CSS string using the `cssbeautifier.beautify` function. It shows both default formatting and how to customize options like indentation size and newline rules using `cssbeautifier.default_options`. The library also supports reading from and writing to files."},"warnings":[{"fix":"Ensure you are using `cssbeautifier` version 1.12.0 or newer. If issues persist, try installing `jsbeautifier` explicitly first: `pip install jsbeautifier cssbeautifier`.","message":"Past versions (e.g., v1.11.0) experienced `pip install cssbeautifier` failures due to packaging issues related to the `jsbeautifier` dependency. While fixed in later releases (v1.12.0), this highlights a potential fragility in the dependency management.","severity":"breaking","affected_versions":"<1.12.0"},{"fix":"For HTML beautification in Python, consider alternative libraries or use the Node.js `js-beautify` CLI/library. The `cssbeautifier` package should only be used for CSS.","message":"The Python `cssbeautifier` library is specifically designed for CSS code and *does not* support HTML beautification. Users familiar with the Node.js version of the beautifier might expect HTML support, but this functionality is not available in the Python package.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always review the output of the beautifier, especially for complex CSS. Test the beautified code to ensure it remains valid and functional. If consistent issues arise with specific CSS constructs, consider opening an issue on the project's GitHub page.","message":"The beautifier may produce unexpected results or even invalid CSS for highly complex, unconventional, or malformed CSS syntax. Historical issues include incorrect handling of `@media` queries, `@font-face` rules, nested `@-rules`, and comments with quotes, leading to extra spaces or parsing errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the Python documentation for the correct `snake_case` option names when setting `BeautifierOptions` attributes. Avoid directly translating CLI flags to Python option names without adjustment.","message":"When configuring options, the Python API uses `snake_case` for option names (e.g., `indent_size`), while the command-line interface (CLI) and some other language implementations (e.g., Node.js) typically use `kebab-case` (e.g., `--indent-size`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}