CSS Compactor – Reduces CSS File Size

This is a script I wrote back in 2006 that reduces the file size of CSS files by removing unnecessary whitespace and comments. It’s also capable of taking such a compacted CSS file and re-indenting it to make it readable. For example, it would take the following CSS:

/* sample css */
* {
    margin: 0px;
    padding: 0px;
}

/* define style for the logo */
#header .logo {
    float: left;
    /* another comment */
}

and turn it into:

*{ margin:0px; padding:0px;}#header .logo{ float:left;}

which is equivalent but much shorter CSS code. It can also re-indent it back to:

*{
     margin: 0px;
     padding: 0px;
}
#header .logo{
     float: left;
}


As I said, the script is very old and was written around the time I learned how to use Python, so it is far from perfect. But despite that, it does what it was written for, and it’s useful, so I deemed it worth releasing. You can download it from here: csscompactor-0.4.tar.gz.

The code is released under the GPL, so feel free to modify it. If there is enough interest in it, I’ll probably re-write it so it has prettier and more efficient code.

2 thoughts on “CSS Compactor – Reduces CSS File Size”

  1. Thk,

    Thank you, very useful utility. It work well for unindent!

    PiF

Leave a Reply

Your email address will not be published. Required fields are marked *