What is it?
CSSMin takes your CSS file and strips out everything that’s not needed — spaces, extra semicolons, redundant units, and so on. That’s great, but there are loads of programs that do that. A shell script could do that! So what makes CSSMin different?
When you deliver content over the web, best practice is to deliver it gzipped. CSSMin takes your CSS file, and optimises it for gzip compression. This means that there’s a smaller payload delivered over the wire, which results in a faster experience for your users. It does this optimisation by ensuring that the properties inside your selectors are ordered consistently (alphabetically) and in lower case. That way, the gzip algorithm can work at its best.
What this means in practice is that your gzipped CSSMin-ed files are significantly smaller than plain gzipped CSS, and noticeably smaller than files that have been compressed by other means (say, YUI).
What does it do?
- Replaces font-weight values like 'bold' with their numeric counterparts;
- Strips quotes wherever possible;
- Changes as much of the contents to lowercase as possible;
- Strips all comments from the source;
- Removes unnecessary whitespace; and, most importantly,
- Reorders the properties within your selectors alphabetically.
What does it support?
Just about everything! Nested properties benefit from compression (for example, -webkit-keyframes), whilst retaining the appropriate order. Prefixed selectors are moved to the top of the block, so that their non-prefixed counterparts will override them when the browser supports them. Most CSS hacks won't get stripped, but some involving comments might break.
Compile the library with
Run your CSS through it:
# java -jar CSSMin.jar [input] [output]
These are the results of compressing the main CSS file for one of the webapps I develop at work. Note that many of these compressors only offer an online service, which means that they can't easily be used as part of your general build process.
|Original size (bytes)||Gzipped size (bytes)|
CSSMin was originally written, and is maintained by, Barry van Oudtshoorn.
CSSMin is licensed under the BSD License. See LICENSE for details.