Skip to main content

CleanCSS

CleanCSS is a simple pythonic language for CSS inspired by CleverCSS but simpler and with less obstrusive features.

Why

I really liked the idea behind CleverCSS but when used in production I realized that I was trying to get away from its parser by escaping strings and unsupported CSS properties way too often. Using vendor prefixes like -webkit-gradient and values such as rgba resulted in a messy CSS, so I decided to write my own parser for a similar syntax without all the complex features that I did not use anyway and it now works fine for me in REAL modern websites.

Syntax examples

I’m going to keep the CleverCSS examples where possible since the syntax is really similar.

A small example below. Note the indentation based syntax and how you can nest rules:

#header, #footer:
        margin: 0
        padding: 0
        font->
                family: Verdana, sans-serif
                size: .9em

        li:
                padding: 0.4em
                margin: 0.8em 0 0.8em

                h3:
                        font-size: 1.2em
                p:
                        padding: 0.3em
                p.meta:
                        text-align: right
                        color: #ddd

Of course you can do the very same in CSS, but because of its flat nature the code would look more verbose. The following piece of code is the CleanCSS output of the above file:

#header, #footer {
        margin: 0;
        padding: 0;
        font-family: Verdana, sans-serif
        font-size: .9em
}

#header li,
#footer li {
        padding: 0.4em;
        margin: 0.8em 0 0.8em;
}

#header li h3,
#footer li h3 {
        font-size: 1.2em;
}

#header li p,
#footer li p {
        padding: 0.3em;
}

#header li p.meta,
#footer li p.meta {
        text-align: right;
        color: #dddddd;
}

Callbacks

Starting with version 1.4 CleanCSS supports user callbacks to alter the resulting stylesheet. The callback function receives the property name and its value and should return a list of property-value pairs to insert in the generated CSS.

Example:

import cleancss

def callback(prop, value):
        rules = []

        # Include the unaltered rule
        rules.append( (prop, value) )

        # Include a variant of the rule
        rules.append( ("-moz-"+prop, value) )

        return rules

with open('file.ccss') as f:
        print cleancss.convert(f, callback)

The library already includes a callback function that can be used to convert between the most common browser variants

Example:

import cleancss

with open('file.ccss') as f:
        print cleancss.convert(f, cleancss.callbacks.browser_variants)

Library usage

Import the cleancss module and call the convert() function with a file-like object.

Example:

import cleancss
with open('file.ccss') as f:
        print cleancss.convert(f)

Command line usage

Call the cleancss.py without parameters to show the usage instructions. Pass any number of files to cleancss.py and the script will convert the files to CSS and print the result to the console.

Example:

python -m cleancss file.ccss > result.css

LICENSE

Copyright (c) 2010 Massimiliano Torromeo

CleanCSS is free software released under the terms of the BSD license.

See the LICENSE file provided with the source distribution for full details.

Contacts

Metadata

Release files for CleanCSS 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CleanCSS 1.4
File Size Uploaded
CleanCSS-1.4.tar.gz 4.8 kB Details

Release files / CleanCSS-1.4.tar.gz

Download URL CleanCSS-1.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2cd98202ba17d4b575ccfccc419a41818b763162f37825f361186d3818d7d93b
BLAKE2b-256 checksum
How to use checksums
4419874ab765ea55a4b66f9a6bb3656b1c2df8fcae29e3f9c0e8b6cb5edae9ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.4 This release

1 release file

1.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page