Any recommendations for a CSS minifier?

I’ll be rooting around Google and trying some out, but I suspected that the smart, proficient and curiously handsome StackOverflow community might have already evaluated the pros and cons of the heavyweights.

Solution 1

The YUI Compressor is fantastic. It works on JavaScript and CSS. Check it out.

Solution 2

There's also a .NET port of YUI Compressor which allows you to:-

  • intergrate the minification/file combining into Visual Studio post-build events
  • intergrate into a TFS Build (including CI)
  • if you wish to just use the dll's in your own code (eg. on the fly minification).

UPDATE 2011: And it's now available via NuGet also :)

Solution 3

I like Minify. In PHP and works with CSS or JavaScript.

Solution 4

CSSO is currently best minifier/optimizer.

Solution 5

If you use Python I would recommend slimmer which is probably not as fast as YUI Compressor but unlike csscompressor.net it doesn't choke on CSS hacks.

I'm biased since I wrote slimmer and I'm currently evaluating YUI Compressor to see how it handles hacks. An example of slimmer in action can be seen if you view the source of crosstips.org

Solution 7

If you are looking for an online tool, try this: https://csscompressor.net/

Solution 8

I've written an ultra fast CSS minifier in C#. The algorithm does not handle Javascript though. Thy this: http://www.ko-sw.com/Blog/post/An-Ultra-Fast-CSS-Minify-Algorithm.aspx.

Solution 9

Try closure-stylesheets.

Beside minification it also supports linting, RTL flipping, and class renaming.

It can also add variables, functions, conditionals, and mixins to CSS.

Also note that some of these features depend on rest of Closure Tools (which are very powerful on their own).

Solution 10

If you're looking for something in PHP, here's the link:-

Fat-Free Minify

Although it's part of the PHP Fat-Free Framework, it can also be used stand-alone.

Solution 11

I find that isnoop's CSS SuperScrub works very well. It can only handle direct links to CSS online though :/ You can get around that though by using your preferred pastebin service to hold the css code and just giving SuperScrub the raw link.

Solution 12

If your site is in ASP.NET, you can let your site do the CSS minification on the fly (so you don't have to do it manually each time you make a change). For example with this:

http://www.codeproject.com/KB/aspnet/CombineAndMinify.aspx

Solution 13

Perl has CSS::Minifier (and an XS version for extra speed).

Solution 14

Others have mentioned YUI Compressor, then the .NET port of it, and I'll add another link to the chain. StyleManager is a server control which wraps up the .NET port of YUI Compressor so you can use it just like you're used to using ScriptManager. It adds a bunch of other nice features too, like CSS constants, tilde (~) resolution w/in your background-image definitions, etc etc. It's tight, well documented, and I've used it on all my recent projects w/o an issue. Check it out - gStyleManager.com

Solution 15

Still "in beta", but should work fairly well. I use the code behind it in every project: http://claudiu.phpfogapp.com/ It's built in PHP and also hosts your *.css file for a fairly large amount of time, surely enough to let you test your code with the minified css. (I would only delete old css files if the space gets crowded on the server).

Solution 16

There is a codeplex project that will plug in to .net websites that will minify and compress the CSS and the JS files. There is also a comparison between the Microsoft AJAX Minifier and the YUI Compressor which shows the YUI coming out slightly better. There is an extra variation which combines the Microsoft Minifier and compression which drastically srunk the file.

Anyway the link is http://xpedite.codeplex.com/wikipage?title=Minifier%20(CSS%2FJavaScript%20Minification%20Handlers)

Solution 17

This is how I did it for MVC3: http://mkramar.blogspot.com/2011/08/css-and-javascript-minify-and-combine.html The beauty of this approach is that it does it all on the fly and you don't have to pre-process files manually or configure post-build.

Solution 18

An online tool (much better than www.csscompressor.net which jacked my css up): http://www.cssdrive.com/compressor/compress.php does an excellent job.

Solution 19

C# example:

css = css.Replace("\n", "");
css = Regex.Replace(css, @"\s+", " ");
css = Regex.Replace(css, @"\s*:\s*", ":");
css = Regex.Replace(css, @"\s*\,\s*", ",");
css = Regex.Replace(css, @"\s*\{\s*", "{");
css = Regex.Replace(css, @"\s*\}\s*", "}");
css = Regex.Replace(css, @"\s*\;\s*", ";");

Solution 20

zbugs.com will be a good online tool for you, it will minify your css in a single click

Solution 21

Have a peek at the latest HTML5BoilerPlate by Paul Irish - it contains a build script to minify all your assets (including PNG's and JPG's). You can see a demo video here.