cssminifycsstidy

Are CSS Minifiers that combine elements destructive?


I found this CSS minifier (http://www.lotterypost.com/css-compress.aspx). There is a section at the bottom of that page labelled "What does the CSS Compressor purposely NOT do?" There are four things, two of which I couldn't understand why they might be destructive:

Combining individual margin, padding, or border styles into a single property.

  margin-top: 10px; 
  margin-right: 0; 
  margin-bottom: 8px;
  margin-left: 30px;

Becomes

  margin: 10px 0 8px 30px;

And combining styles for the same element that are specified in different style blocks.

#element {
   margin: 0;
}

#element {
   color: #000000;
}

Becomes

#element {
   margin: 0;
   color: #000000;
}

I think CSSTidy does both of these. Is the web page above correct? Are there situations where these types of minification might be a problem?


Solution

  • I'm the developer of the CSS Compressor that is the subject of this question (http://www.lotterypost.com/css-compress.aspx), so I'll elaborate with an example of how a compressor can break the CSS cascade if a tool aggressively re-writes it.

    There are many ways of targeting elements in a style sheet, and because a CSS compressor does not have intimate knowledge of the page’s DOM structure, classes, ids, etc., there is no way for a compressor to know if an optimization that crosses bracketed definitions will break or not.

    For example, a simple HTML structure:

    <div class="normal centered">
        <p>Hello world.</p>
    </div>
    

    And some CSS:

    div.normal p {
        margin: 10px 0 10px 0;
    }
    
    div.centered p {
        margin: 10px auto;
    }
    
    div.normal p {
        margin-bottom: 5px;
    }
    

    The uncompressed code would produce a centered paragraph with a 10px top margin and a 5px bottom margin.

    If you run the CSS styles through the CSS Compressor, you'll get the following code, which maintains the order and cascade of the original uncompressed styles.

    div.normal p{margin:10px 0}div.centered p{margin:10px auto}div.normal p{margin-bottom:5px}
    

    Let's say you want to aggressively compress the styles further, by combining the margins of the two div.normal p definitions. They both have the exact same selector, and they appear to redundantly style the bottom margin.

    There would be two ways to combine the margins: you can either combine the two margin definitions into the first (top) div.normal p style or combine them into the last (bottom) one. Let's try both ways.

    If you combine the margins into the first (top) div.normal p style, you get this:

    div.normal p{margin:10px 0 5px}div.centered p{margin:10px auto}
    

    The result of combining the margins that way would result in the bottom margin being incorrectly set to 10px, because the "centered" class would override the bottom margin (because the "centered" style defintion now appears later in the cascade).

    If you combine the margins into the last (bottom) div.normal p style, you get this:

    div.centered p{margin:10px auto}div.normal p{margin:10px 0 5px}
    

    The result of combining the margins that way would result in the paragraph no longer appearing as centered, because the bottom "p" definition would override the left and right margins of "auto" that are defined in the "centered" class.

    So we can see that by combining style definitions that even have the exact same selector can cause some pretty bad problems.

    Would you personally ever write code like this? Maybe or maybe not. Because of the various "weight" rules of the cascade, it is possible to fall into this type of code trap without ever realizing it.

    Furthermore, given the fact that in today's Web pages, multiple CSS files are often combined into one file to hit the server with fewer downloads, it is easy to imagine the CSS Compressor royally screwing up the cascade by re-writing multiple style sheets (possibly written by different people) that are appended together into one file.

    In fact, I wrote the CSS Compressor for this very scenario on my Web site, Lottery Post. Each Web page has many style sheets supporting various jQuery and other components, and the CSS Compressor is used to automatically compress all of those style sheets into one single download. All pages on the site have at least 10 different style sheets combined together, and most pages have more than that.

    For example, if you look at the code behind the CSS Compressor page itself, you will find the main style sheet in the head that looks like this:

    <link rel="stylesheet" href="http://lp.vg/css/d11019.0/j2HKnp0oKDOVoos8SA3Bpy3UHd7cAuftiXRBTKCt95r9plCnvTtBMU2BY2PoOQDEUlKCgDn83h16Tv4jbcCeZ(gupKbOQ9ko(TcspLAluwgBqrAjEhOyXvkhqHA(h5WFDypZDK2TIr(xEXVZtX7UANdFp6n1xfnxqIMR8awqGE)vrwUgY2hrCGNNTt1xV7R1LtCSfF46qdH1YQr2iA38r1SQjAgHze(9" />
    

    The gobbledeegook in the URL is actually an encrypted string containing all the style sheets to combine on the server. The server compresses them and caches the result.

    The space- and time-saving techniques on that one style sheet call include:

    I hope this better explains things and is helpful to those looking to improve page performance!

    -Todd

    MORE INFO:

    To add to the above, imagine if we take your color example, and combine style definitions with the same selectors.

    Here are some uncompressed styles:

    div.normal p {
        margin: 10px 0 10px 0;
    }
    
    div.centered p {
        margin: 10px auto;
        color: blue;
    }
    
    div.normal p {
        color: black;
    }
    

    The CSS Compressor produces the following output:

    div.normal p{margin:10px 0}div.centered p{margin:10px auto;color:blue}div.normal p{color:#000}
    

    If we were to apply aggressive combining of style definitions that have the same selector, we will get the following code.

    Method 1, combining both into the first definition, would incorrectly make the text color blue:

    div.normal p{margin:10px 0;color:#000}div.centered p{margin:10px auto;color:blue}
    

    Method 2, combining both into the second definition, would incorrectly make the text left-aligned:

    div.centered p{margin:10px auto;color:blue}div.normal p{margin:10px 0;color:#000}
    

    The only time combining style definitions with the same selector is 100% error-free is when the definitions appear directly one after the other, but in every other case this technique risks corrupting the cascade of styles.

    I couldn't imagine a case where any developer would write code in this manner (two style definitions with the exact same selector, one immediately after the other), so I concluded that the amount of effort required to code it, and the possibility of an additional point of failure in the compressor, was not worth it by a long-shot.

    Frankly, I would be very concerned about a CSS compressor that combined styles from different definition blocks, because the cascade is a very fragile thing, and it is extremely easy to break the cascade.