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.
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.
The YUI Compressor is fantastic. It works on JavaScript and CSS. Check it out.
There\'s also a .NET port of YUI Compressor which allows you to:-
UPDATE 2011: And it\'s now available via NuGet also :)
I like Minify. In PHP and works with CSS or JavaScript.
CSSO is currently best minifier/optimizer.
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
Check out CSSTidy: http://csstidy.sourceforge.net/usage.php
And online at: http://cdburnerxp.se/cssparse/css_optimiser.php
If you are looking for an online tool, try this: https://csscompressor.net/
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.
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).
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.
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.
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
Perl has CSS::Minifier (and an XS version for extra speed).
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
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).
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)
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.
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.
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*\", \";\");
zbugs.com will be a good online tool for you, it will minify your css in a single click
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.