CSS Beautifier

1.15.4 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import cssbeautifier

ugly_css = ".container{display:flex;}.item{color:red;font-size:16px;margin:0 auto;}"

# Beautify with default options
pretty_css_default = cssbeautifier.beautify(ugly_css)
print("--- Default Beautification ---")
print(pretty_css_default)

# Beautify with custom options
opts = cssbeautifier.default_options()
opts.indent_size = 2
opts.end_with_newline = True
opts.newline_between_rules = False

pretty_css_custom = cssbeautifier.beautify(ugly_css, opts)
print("\n--- Custom Beautification (2-space indent, no newline between rules) ---")
print(pretty_css_custom)

# Example of beautifying a file (conceptual, requires a file to exist)
# Assuming 'styles.min.css' exists with ugly_css content
# with open('styles.min.css', 'w') as f:
#     f.write(ugly_css)
# beautified_file_content = cssbeautifier.beautify_file('styles.min.css', opts)
# print("\n--- Beautified File Content ---")
# print(beautified_file_content)

view raw JSON →