Developer Network Home - Help

YUI Compressor's CSS minifier

YUI Compressor's CSS minifier

This document describes how the CSS minification part of the YUICompressor works.

CSS minifications

This section describes the various ways that YUICompressor makes your CSS smaller.

Stripping comments and white space

YUICompressor strips all the comments and white space that are not required for the CSS to work.

Before the minification:

  1. /*****
  2.   Multi-line comment
  3.   before a new class name
  4. *****/
  5. .classname {
  6. /* comment in declaration block */
  7. font-weight: normal;
  8. }
/*****
  Multi-line comment
  before a new class name
*****/
.classname {
    /* comment in declaration block */
    font-weight: normal;
}

After the minification:

  1. .classname{font-weight:normal}
.classname{font-weight:normal}

Special comments

Stripping comments is not always acceptable. Sometimes you need to retain copyright information. You can use ! at the beginning of the comment to mark the comment as special and it will be preserved.

Before:

  1. /*!
  2.   (c) Very Important Comment
  3. */
  4. .classname {
  5. /* comment in declaration block */
  6. font-weight: normal;
  7. }
/*!
  (c) Very Important Comment
*/
.classname {
    /* comment in declaration block */
    font-weight: normal;
}

After:

  1. /*!
  2.   (c) Very Important Comment
  3. */.classname{font-weight:normal}
/*!
  (c) Very Important Comment
*/.classname{font-weight:normal}

The ! character itself is also preserved. This way you can safely double-minify the same file (probably by mistake). That also allows when reviewing the code (manually or with tools) to see ! and conclude that this comment is there intentionally, not because you forgot to minify the CSS.

Striping last semi-colon

The last semi-colon in a declaration block is not needed. You can keep it in your source for maintenance purposes and let the minifier take care of stripping it out before the site goes live.

Before:

  1. .classname {
  2. border-top: 1px;
  3. border-bottom: 2px;
  4. }
.classname {
    border-top: 1px;
    border-bottom: 2px;
}

After:

  1. .classname{border-top:1px;border-bottom:2px}
.classname{border-top:1px;border-bottom:2px}

Extra semi-colons

Only one semi-colon is required to separate rules, so the minifier will strip an accidentally added one.

Before:

  1. .classname {
  2. border-top: 1px; ;
  3. border-bottom: 2px;;;
  4. }
.classname {
    border-top: 1px; ;
    border-bottom: 2px;;;
}

After:

  1. .classname{border-top:1px;border-bottom:2px}
.classname{border-top:1px;border-bottom:2px}

No empty declarations

Empty declaration blocks don't contribute to the way the stylesheets work, so they can safely be removed.

Before:

  1. .empty { ;}
  2. .nonempty {border: 0;}
.empty { ;}
.nonempty {border: 0;}

After:

  1. .nonempty{border:0}
.nonempty{border:0}

Zero values

When using zero values, the units of measure are not required, so the YUICompressor will strip them. Additionally, when you have two, three of four zeros in margins and paddings, they can be reduced to one.

Before:

  1. a {
  2. margin: 0px 0pt 0em 0%;
  3. background-position: 0 0ex;
  4. padding: 0in 0cm 0mm 0pc
  5. }
a {
    margin: 0px 0pt 0em 0%;
    background-position: 0 0ex;
    padding: 0in 0cm 0mm 0pc
}

After:

  1. a{margin:0;background-position:0 0;padding:0}
a{margin:0;background-position:0 0;padding:0}

Floats

When a value is using a floating point number smaller than 1, the leading 0 is not required.

Before:

  1. .classname {
  2. margin: 0.6px 0.333pt 1.2em 8.8cm;
  3. }
.classname {
    margin: 0.6px 0.333pt 1.2em 8.8cm;
}

After:

  1. .classname{margin:.6px .333pt 1.2em 8.8cm}
.classname{margin:.6px .333pt 1.2em 8.8cm}

Colors values

RGB color values are easier to read, but the hex values are shorter, so YUICompressor will use the more concise form. Addiotionally, colors that follow the pattern #AABBCC can be reduced to #ABC, except when used in IE filter values.

Before:

  1. .color-me {
  2. color: rgb(123, 123, 123);
  3. border-color: #ffeedd;
  4. background: none repeat scroll 0 0 rgb(255, 0,0);
  5. }
.color-me {
    color: rgb(123, 123, 123);
    border-color: #ffeedd;
    background: none repeat scroll 0 0 rgb(255, 0,0);
}

After:

  1. .color-me{color:#7b7b7b;border-color:#fed;background:none repeat scroll 0 0 #f00}
.color-me{color:#7b7b7b;border-color:#fed;background:none repeat scroll 0 0 #f00}

Before:

  1. .cantouch {
  2. color: rgba(1, 2, 3, 4);
  3. filter: chroma(color="#FFFFFF");
  4. }
.cantouch {
    color: rgba(1, 2, 3, 4);
    filter: chroma(color="#FFFFFF");
}

After (no color minification):

  1. .cantouch{color:rgba(1,2,3,4);filter:chroma(color="#FFFFFF")}
.cantouch{color:rgba(1,2,3,4);filter:chroma(color="#FFFFFF")}

Single charsets

Only one charset declaration is allowed per stylesheet, but sometimes when automatically merging stylesheets into one to reduce HTTP requests, you may end up with more than one charset. YUICompressor will keep only the first.

Before:

  1. @charset "utf-8";
  2. #foo {
  3. border-width: 1px;
  4. }
  5.  
  6. /* second css, merged */
  7. @charset "another one";
  8. #bar {
  9. border-width: 10px;
  10. }
@charset "utf-8";
#foo {
    border-width: 1px;
}
 
/* second css, merged */
@charset "another one";
#bar {
    border-width: 10px;
}

After:

  1. @charset "utf-8";#foo{border-width:1px}#bar{border-width:10px}
@charset "utf-8";#foo{border-width:1px}#bar{border-width:10px}

Alpha opacity

The opacity filter in IE has a verbose syntax, which can be shortened using the IE4 syntax

Before:

  1. .classname {
  2. -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; /* IE 8 */
  3. filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80); /* IE < 8 */
  4. }
.classname {
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=80)"; /* IE 8 */
    filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=80);       /* IE < 8 */
}

After:

  1. .classname{-ms-filter:"alpha(opacity=80)";filter:alpha(opacity=80)}
.classname{-ms-filter:"alpha(opacity=80)";filter:alpha(opacity=80)}

Shorter none values

For some properties none and 0 have the same meaning. Instances of "none" are converted to 0 where applicable and safe to do so.

Before:

  1. .classname {
  2. border: none;
  3. background: none;
  4. outline: none;
  5. }
.classname {
    border: none;
    background: none;
    outline: none;
}

After:

  1. .classname{border:0;background:0;outline:0}
.classname{border:0;background:0;outline:0}

CSS hacks tolerance

When stripping comments and performing other minification tasks, the YUICompressor keeps an eye on preserving common CSS hacks. CSS hacks often use parsing glitches in browsers to provide some extra declarations (or hide some) from specific browser versions.

This section describes the hacks that are tolerated by the YUICompressor.

Underscore/star hack

Using _ and * to prefix CSS properties is a simple way to target IE6 and IE7.

Since YUICompressor uses a regular expression based CSS minifier and doesn't validate CSS properties, it can accept pretty much any property. Therefore this hack is preserved.

Before:

  1. #element {
  2. width: 1px;
  3. *width: 2px;
  4. _width: 3px;
  5. }
#element {
    width: 1px;
    *width: 2px;
    _width: 3px;
}

After:

  1. #element{width:1px;*width:3pt;_width:2em}
#element{width:1px;*width:3pt;_width:2em}

Child selector hack

The child selector hack allows developers to hide declarations from IE7 and below using an empty comment.

YUICompressor strips comments but will make an exception and retain empty comments that immediately follow the > character.

Before:

  1. html >/**/ body p {
  2. color: blue;
  3. }
html >/**/ body p {
    color: blue;
}

After:

  1. html>/**/body p{color:blue}
html>/**/body p{color:blue}

IE5/Mac hack

There is a hack that targets IE5/Mac using a bug when parsing comments.

YUICompressor retains comments that look like they are using this hack, but it minifies the comments needed by the hack.

Before:

  1. /* Ignore the next rule in IE mac \*/
  2. .selector {
  3. color: khaki;
  4. }
  5. /* Stop ignoring in IE mac */
/* Ignore the next rule in IE mac \*/
.selector {
    color: khaki;
}
/* Stop ignoring in IE mac */

After:

  1. /*\*/.selector{color:khaki}/**/
/*\*/.selector{color:khaki}/**/

Box model hack

This hack uses valid CSS and there's no special use of comments so it's retained.

Before:

  1. #elem {
  2. width: 100px; /* IE */
  3. voice-family: "\"}\"";
  4. voice-family:inherit;
  5. width: 200px; /* others */
  6. }
  7. html>body #elem {
  8. width: 200px; /* others */
  9. }
#elem {
    width: 100px; /* IE */
    voice-family: "\"}\"";
    voice-family:inherit;
    width: 200px; /* others */
}
html>body #elem {
    width: 200px; /* others */
}

After:

  1. #elem{width:100px;voice-family:"\"}\"";voice-family:inherit;width:200px}html>body #elem{width:200px}
#elem{width:100px;voice-family:"\"}\"";voice-family:inherit;width:200px}html>body #elem{width:200px}

Running and contributing tests

When reporting bugs, it's greatly appreciated if you can provide a test case. In order to create a test you can use your version of the compressor or build it from source.

  1. Checkout or download the code from http://github.com/yui/yuicompressor/. You need the code even if you don't intend to build the tool yourself, because that's where the tests and the test runner are.
  2. Navigate to the root yuicompressor/ directory
  3. Type ant and hit enter

In order for this to work you need a recent Java SDK installed (at least 1.4) and also Ant running. (On the Mac, you can simply do port install apache-ant to get Ant)

There are a number of tests in the tests/ directory, you can run them with the suite script:

  1. cd tests/
  2. ./suite.sh

This command will also run the tests using the JavaScript port of the minifier. Since the compressor is using Mozilla's Rhino (slightly modified), Rhino is part of the code. So it can be used as a JavaScript interpreter to run the JavaScript port.

The procedure to write new tests is as follows:

  1. Create source CSS file in the tests/ directory, e.g. new-test.css
  2. Create a new file with the expected result and name it with a .min extension, e.g. new-test.css.min

JavaScript port

The YUICompressor is written in Java, however there's a JavaScript port of the CSS minification part, which is available separately.

You can grab the source code of the JavaScript port from the repository or just test it in the available web UI.

Support & Community

The YUI Library and related topics are discussed on the on the YUILibrary.com forums.

Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

More about CSS minification and the YUI Compressor:

YUI Compressor on del.icio.us:

bookmark on del.icio.us

be the first to bookmark this page!

Copyright © 2017 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings