Manas Tungare

On-the-fly CSS Compression Script in PHP

Web site optimization experts suggest that webmasters try to minimize the number and size of HTTP requests necessary to serve web pages. Web designers often use multiple CSS files because they are easier to manage, but this requires as many HTTP requests as there are CSS files.

This script will allow you to serve all your CSS files as a single HTTP resource, minified (by removing comments and extraneous whitespace), and gzip- compressed. It also requests browsers to cache the CSS content for at least a day before trying to fetch a new version.

Copy/Paste Compression

Note, by using the copy/pasted version instead of the script directly, you’ll lose the benefit of setting cache headers correctly, but this will at least minify your CSS.

Example CSS from Mozilla Developer Network.

Use the Script on Your Server

The best part is that this does not pre-process the files, so it does not add any steps to your deployment process. It’s licensed free for commercial and non-commercial use, with attribution requested.