cssmin.js

cssmin.js is a JavaScript port of YUICompressor's CSS minifier.

The motivation

Minifying CSS helps reduce file sizes and makes your pages faster and your users happier. YUICompressor is cool but is written in Java, which can be a blocker for some folks - you know JVM, command line, classpaths... No more excuses, now you have a simple light JavaScript version. And as you know, JavaScript is everywhere, so you can run it however you want, integrate with your editor and so on.

The links

The integration

If you want to integrate the library into your environment, it's really easy. It's just one file with one function in it. So, just a simple function call:

var result = YAHOO.compressor.cssmin(input_css_code);

The credits

Julien Lecomte - creator of YUICompressor
Isaac Schlueter - he maintains the YUICompressor and is the author of the original cssmin utility which was ported to Java by Julien.

Ha, what about a quiz? Guess the language of Isaac's original cssmin and I'll send you a free copy of Even Faster WebSites and I'll sign my chapter. Seriously.

UPDATE: For Ruby folks, there's a Ruby port from the Ryan Grove.

This entry was posted on Wednesday, September 23rd, 2009 and is filed under CSS, JavaScript, performance, yui. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

38 Responses to “cssmin.js”

  1. Greg Schechter Says:

    I’m going to guess Isaac wrote it in PHP

  2. Twitter Trackbacks for cssmin.js / phpied.com [phpied.com] on Topsy.com Says:

    [...] First Tweet 15 minutes ago stoyanstefanov Stoyan Stefanov Highly Influential New blog post: cssmin.js http://www.phpied.com/cssmin-js/ view retweet [...]

  3. Stoyan Says:

    heh, the shortest contest ever :)

    Greg, send me your mailing address to ssttoo at gmail

  4. P.Sorokin Says:

    Add to the end for correct work in IE6
    //:first-letter and :first-line for IE6
    css = css.replace(/:first-letter+/g, “:first-letter “);
    css = css.replace(/:first-line+/g, “:first-line “);

  5. Stoyan Says:

    Thanks P.Sorokin!

  6. CSS munging - a FAILed experiment / phpied.com Says:

    [...] phpied.com Stoyan’s blog about (x)html, ajax, bookmarklets, browsers, css, firebug, javascript, json, mdb2, mysql, pear, performance, php, phpbb, tools, yslow, yui, writing, music,… life and everything. « cssmin.js [...]

  7. Steven Paul Says:

    I need a perl version of this script, for server side optimization…

  8. Ron Peled Says:

    Any idea how to perform CSS minimization for ASP.NET C#?

  9. Performance Calendar » Blog Archive » Reducing the payload: compression, minification, 204s Says:

    [...] – yes, the same YUI compressor that does JavaScript minification. I’ve actually ported the CSS minification part of it to JavaScript (it’s in Java otherwise) some time ago. There’s even an online form [...]

  10. 15 Cara Optimasi Blog Agar Tampil Lebih Cepat : Smartplus Network Indonesia Says:

    [...] Teknik kompresi CSS ini sebenarnya cukup sederhana, hampir sama dengan cara diatas yaitu dengan cara membuang spasi yang tidak diperlukan. Memadatkan pengkodean CSS juga bisa menghebat waktu download, Parsing dan waktu eksekusi pada suatu halaman. Untuk mempermudah kita melakukan kompresi pada CSS, silahkan gunakan tools gratis yang sudah banyak disediakan seperti YUI Kompresor dan cssmin.js. [...]

  11. 15 Optimizations Your Blog For Maximum Speed - ZoomTemplate Blog Says:

    [...] 3. Perform Compression CSS (Stylesheet) CSS compression is actually quite simple, almost the same way with the above is by removing unnecessary white space. Compacting CSS coding can also be intensified when downloading, parsing and execution time on a page. For simplicity we do the compression on CSS, please use the many free tools available such as YUI Kompresor and cssmin.js. [...]

  12. Markus Says:

    Attention if you have Microsoft filters !

    For example:
    -ms-filter:”progid:DXImageTransform.Microsoft.Alpha(Opacity=60)”;

    The output will be:
    -ms-filter:”alpha(opacity=100)”;

    Cheers Markus

  13. Moses Muriuki Says:

    All my css are called from my database with relevant links to the database how do I implement this tool?

  14. BJ Says:

    +1 to Markus’s comment about Filter; the DXImageTransform version of the filters (IE5+) are NOT the same as the shorter IE4 ones. From MSDN:

    “When you use the Internet Explorer 4.0 filter syntax, margins can affect how some filters are applied and rendered. An object’s boundary can be clipped when it is set too close to the filtered content. For example, if you apply a Glow filter to an object’s text without a margin, the object boundary clips some of the glow effect if the text is next to the boundary.”

    “With Internet Explorer 5.5 syntax, the Shadow, DropShadow, and Glow filters extend the boundary of an object to make room for the filter effect, while the Internet Explorer 4.0 versions clip the filter effect at the boundary.”

    “In many cases, color is expanded to include the Alpha component (#AARRGGBB) in the Internet Explorer 5.5 filters.”

    http://msdn.microsoft.com/en-us/library/ms532847(v=vs.85).aspx

    Has this been fixed in CSSMin, and indeed YUI Compressor?

  15. Jesper Says:

    Thanks – very usefull

  16. WPO: Din CSS drar laddningstid – så minimerar du den | Web Performance Optimization (WPO) | Adam Hassan Says:

    [...] du antingen använda dig av plugins till WordPress eller av gratistjänster som YUI Compressor och cssmin.js.Jag testade själv att att slumpvis välja ut två webbplatser, Trygg-Hansa & Nordea, och [...]

  17. Web design, SQL, and .NET for the young, up-and-coming developer - Dot Net Yuppie - Blog Archive » Compiling Multiple CSS and Javascript Files During Buildtime with CSSMin and JSMin in Visual Studio Says:

    [...] your code files.  Each of the tools can be downloaded for free at the following developer sites: cssmin-js and jsmin.  For the purposes of this article, I have saved both files (cssmin.js and jsmin.exe) [...]

  18. willya Says:

    thanks for this awesome program :)

  19. Hammad Baig Says:

    thanks PHPIED, it’s very usefull

  20. 15 Cara Optimasi Blog Agar Tampil Lebih Cepat | Ada Gratis Two Says:

    [...] pada CSS, silahkan gunakan tools gratis yang sudah banyak disediakan sepertiYUI Kompresor dan cssmin.js.   4. Letakan CSS (Stylesheet) di Atas Adakalanya kita tidak meletakan CSS di header, biasanya [...]

  21. Poltargaista Says:

    Greetings. How can I run the script on http://garaj-bg.com/ ?
    It’s on a shared hosting, using Apache. I don’t understand much to tell you more.

  22. Techtutshub Says:

    Really nice post, it’s very usefull

  23. Tools to Analyze and Make your Website Load Faster | Learn-Share-Earn Money Online Says:

    [...] a page of your site. It can help reducing latency, enhancing compression and faster loading. Cssmin.js tool can help you on this [...]

  24. Hernán Says:

    Sorry, not understand how to use it or implement.

  25. 15 Cara Optimasi Blog Agar Tampil Lebih Cepat | lashinta.com Says:

    [...] Teknik kompresi CSS ini sebenarnya cukup sederhana, hampir sama dengan cara diatas yaitu dengan cara membuang spasi yang tidak diperlukan. Memadatkan pengkodean CSS juga bisa menghebat waktu download, Parsing dan waktu eksekusi pada suatu halaman. Untuk mempermudah kita melakukan kompresi pada CSS, silahkan gunakan tools gratis yang sudah banyak disediakan seperti YUI Kompresor dan cssmin.js. [...]

  26. 15 Cara Baru + Optimasi Blog - Just another WordPress site - Blog Says:

    [...] Lakukan Kompresi CSS (Stylesheet) Teknik kompresi CSS ini sebenarnya cukup sederhana, hampir sama dengan cara diatas yaitu dengan cara membuang spasi yang tidak diperlukan. Memadatkan pengkodean CSS juga bisa menghebat waktu download, Parsing dan waktu eksekusi pada suatu halaman. Untuk mempermudah kita melakukan kompresi pada CSS, silahkan gunakan tools gratis yang sudah banyak disediakan sepertiYUI Kompresor dan cssmin.js. [...]

  27. | Loïc Lefloch Says:

    [...] La compression d’un fichier css permet d’accélérer le téléchargement, l’analyse et le temps d’exécution Un compresseur css permet de réduire significativement la taille d’un fichier css. Par exemple csscompressor.com propose plusieurs options tel que compress color, compress font-weight, Remove unnecessary backslashes, Remove unnecessary semi-colons. On peut choisir le niveau de compression. Selon le niveau choisi, les lignes du fichiers seront soit regroupées sur une seule et même ligne, soit sur plusieurs.  http://www.phpied.com/cssmin-js/ [...]

  28. Optimisation d’un site web | Loïc Lefloch Says:

    [...] La compression d’un fichier css permet d’accélérer le téléchargement, l’analyse et le temps d’exécution Un compresseur css permet de réduire significativement la taille d’un fichier css. Par exemple csscompressor.com propose plusieurs options tel que compress color, compress font-weight, Remove unnecessary backslashes, Remove unnecessary semi-colons. On peut choisir le niveau de compression. Selon le niveau choisi, les lignes du fichiers seront soit regroupées sur une seule et même ligne, soit sur plusieurs.  http://www.phpied.com/cssmin-js/ [...]

  29. Optimisation d’un site web | Loïc Lefloch Says:

    [...] La compression d’un fichier css permet d’accélérer le téléchargement, l’analyse et le temps d’exécution Un compresseur css permet de réduire significativement la taille d’un fichier css. Par exemple csscompressor.com propose plusieurs options tel que compress color, compress font-weight, Remove unnecessary backslashes, Remove unnecessary semi-colons. On peut choisir le niveau de compression. Selon le niveau choisi, les lignes du fichiers seront soit regroupées sur une seule et même ligne, soit sur plusieurs.  http://www.phpied.com/cssmin-js/ [...]

  30. Albin Says:

    Really great stuff for optimizing my sites page load speed :)

  31. cssmin.js | timhecker.it Says:

    [...] cssmin.js is a JavaScript port of YUICompressor’s CSS minifier. This entry was posted in Dalla rete and tagged compression, minification, sviluppare. Bookmark the permalink. ← This is Responsive [...]

  32. Page speed best practices | Creative Technologist Reference Says:

    [...] Several tools are freely available to minify JavaScript, including the YUI Compressor and cssmin.js. [...]

  33. sandeep Says:

    nice post!

  34. Minimizzazione del CSS | Web performance Says:

    [...] Diversi strumenti sono liberamente disponibili per la minimizzazione del codice, compreso il compressore YUI oppure cssmin.js. [...]

  35. Amanda Says:

    Amazing post about compressing css. Thank you.

  36. Bikubik Says:

    Thank you for the best tool for compressing css files!

  37. Web site Optimization | Hellbach blog Says:

    [...] cssmin.js [...]

  38. Neha Jobs Says:

    Attention if you have Microsoft filters !

    For example:
    -ms-filter:”progid:DXImageTransform.Microsoft.Alpha(Opacity=60)”;

    The output will be:
    -ms-filter:”alpha(opacity=100)”;

    Cheers Markus

Leave a Reply