Lime Blast (The virtual home of Web developer Daniel Hollands, the place to be if you're looking for articles and tutorials (and rants) on all aspects of the World Wide Web.)
  • Navigation
  • Home
  • About
  • Portfolio
  • Blog

CSS Crush

Posted: 5th April 2012 at 2:00 pm

Comments: Leave a Comment

Contents

  1. Vendor prefixing
  2. Variables
  3. Direct @import and minification
  4. Implementing CSS Crush in Wordpress
  5. Conclusion

Although there is a lot of love for CSS preprocessors such as LESS and SASS, both of which seem to work very well, I wanted to take a moment to talk about a slightly different alternative, the PHP-based CSS Crush.

Much like the other CSS preprocessors, CSS Crush offers a range of features designed to make your life easier. Although these including functions that let you do things like manipulate the opacity of a colour value, or generate a data uri from a relative file path, I’m going to talk about some of the other features that I have experience with.

Vendor prefixing

The first, and possibly most important of these, is vendor prefixing. There has been a lot of talk in recent months about non-webkit browsers adopting the -webkit prefix, which has been spurred on by the fact that, to be honest, vendor prefixes are a pain in the arse.

CSS Crush helps solve issues like this by automatically adding all relevant prefixes to the properties that support them, meaning that this:

1
div { background: #fff linear-gradient( left, red, white ) no-repeat; }

will automatically be converted into this:

1
2
3
4
5
6
7
div {
    background: #fff -webkit-linear-gradient( left, red, white ) no-repeat;
    background: #fff -moz-linear-gradient( left, red, white ) no-repeat;
    background: #fff -ms-linear-gradient( left, red, white ) no-repeat;
    background: #fff -o-linear-gradient( left, red, white ) no-repeat;
    background: #fff linear-gradient( left, red, white ) no-repeat;
}

This is something that (so far as I’m aware) none of the other CSS preprocessors offer out of the box – although some creative use of mixins does offer something similar – but by using CSS Crush as a git submodule, you’ll always have access to the latest prefixes available.

Variables

Another helpful feature is that of being able to define and use variables. This allows for uniformity across all your stylesheets, meaning you can define variables such as:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
@define
{
    /* fonts */
    title: "Times New Roman", Georgia, serif;
    body: Verdana, Arial, Helvetica, sans-serif;
 
    /* colours */
    primary-rgb: 89,168,15;
    primary: rgb($(primary-rgb));
    primary-text: #FFFFFF;
 
    secondary-rgb: 158,213,76;
    secondary: rgb($(secondary-rgb));
    secondary-text: #FFFFFF;
 
    background-rgb: 240,242,221;
    background: rgb($(background-rgb));
    background-text: #222222;
}

then if you need to change a value at a later date, rather than having to do a find/replace, and risk missing some, you simply update the value is one place, and CSS Crush does the rest for you.

Direct @import and minification

One of the things I found particularly useful was the ability to use @import to manage different CSS files for responsive sites. A practical example of this would be something like:

1
2
3
@import url( "mobile.css" ); /* Mobile first */
@import url( "tablet.css" ) screen and (min-width: 768px); /* Tablet and higher */
@import url( "desktop.css" ) screen and (min-width: 992px); /* desktop and higher */

which not only imports the files directly into a single stylesheet, would also put the correct @media tags around the imported code, and then performs minification on it all.

Implementing CSS Crush in WordPress

I’m actually using CSS Crush in my limeBase WordPress starter theme, so that could be a good way for you to get started, but if you wanted to create your own implementation, it couldn’t be simpler:

  1. Download/clone the files into your theme folder in a folder called cssCrush
  2. in your functions.php file, add the following line
    1
    require_once 'cssCrush/CssCrush.php';
  3. in your header.php, replace your existing stylesheet <link>code with
    1
    <?php echo CssCrush::tag( TEMPLATEPATH.DIRECTORY_SEPARATOR.'style.css' ); ?>

Upon viewing your page, CSS Crush will create two new files: .csscrush and style.crush.css, and output the relevant <link> code linking to the new style.crush.css file.

If, before further page views, the original style.css gets updated, CSS Crush will detect the change, and generate a new crushed file for you, otherwise it will continue to serve the cached file.

Conclusion

If you’re already happily using LESS or SASS, then I’m not sure there is much that CSS Crush can offer you. If, however, you’re more interested in a simple way of automatically adding vendor prefixes and performing minification on your CSS, then you can’t go far wrong with CSS Crush.

Written by Daniel Hollands

Daniel Hollands is a jack-of-all-trades web developer trying his best to keep up to date with everything the Web has to offer. He keeps this blog in the hope that someone might find what he has to say useful.

Visit his Homepage, follow him on Twitter, Facebook, LinkedIn, and Google+, or hire him for your next project.

This entry was posted in Credit where credit is due and tagged CSS, CSS Crush, LESS, limeBase, PHP, SASS, webkit, Wordpress. Bookmark the permalink.

Related posts:

  1. Facebook Timeline Photoshop Template

    Seeing as Facebook’s Timeline feature is making the move to Pages, I figured that now was a good time to share the Facebook Timeline Hack Template that I used to style up my Timeline Page. The download includes a Photoshop......

  2. Images in responsive WordPress Web sites

    For the past couple of weeks I’ve been learning how to build responsive Web sites, a process that is simple in principle, but complex in practice once you get down to the nitty-gritty. As I want to use this blog as......

  3. Running a server is hard work

    I was hoping, when I first had the idea for this post, that it would be a run-through of all the steps I has taken in setting up my dedicated server. It was going to include links to the resources......

  4. Scrap it and start again

    After spending almost all of my spare time during the last four weeks working on designing and building the Lime Blast site, I had gotten to a point on Saturday morning where all I had to do was fix the......

Calendar

April 2012
M T W T F S S
« Mar   May »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Categories

  • Credit where credit is due (8)
  • Gaming (1)
  • Just Plain AWESOME!!! (4)
  • News (6)
  • Personal (3)
  • Rants (4)
  • Reviews (1)
  • Share the knowledge (6)
  • Tools and Resources (3)

Archives

  • June 2014 (1)
  • February 2014 (1)
  • January 2014 (1)
  • December 2013 (1)
  • July 2013 (1)
  • June 2013 (2)
  • May 2013 (2)
  • January 2013 (5)
  • December 2012 (2)
  • November 2012 (2)
  • October 2012 (1)
  • July 2012 (2)
  • June 2012 (3)
  • May 2012 (1)
  • April 2012 (2)
  • March 2012 (4)
  • February 2012 (4)
  • January 2012 (1)

Affiliates

UK Cloud Hosting. Fast, Reliable & Infitely Scalable. TSOHost. eCommerce for Designers - LemonStand FreeAgent Small Business Online Accounting

Recent Posts

  • Reset The Net
  • My Thoughts on Shadowrun Returns
  • Laravel Application Development Cookbook
  • It’s simple, we kill the Batman!
  • Useful Composer packages for use with Laravel 4
  • Goodbye Ghost, Hello School Stickers (I start my new job in July)
  • All Around the Wrekin (Photolog)
  • The importance of a good password
  • Once you go Mac, you’ll never go back – but should I?

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3 other subscribers

RSS

RSS Feed RSS - Posts

RSS Feed RSS - Comments

Tag Cloud

Android Apple Asus Beanstalk Behaviours Bitbucket CakePHP Composer cPanel CSS Debian Dedicated Server Dell EXIM Ghost Design Git GitHub Google Apps Hetzner Inspiron Duo iOS iPad iPhone iTunes Jeffrey Way Laravel limeBase Mercurial Microsoft PC Photoshop PHP Plugins Propeller Communications Responsive Web Design SASS Siri SPAM Submodules Tegra 3 Quad-core Transformer Prime Windows 7 Windows 8 Wordpress Yii
© 2013 Daniel Hollands.