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:
div { background: #fff linear-gradient( left, red, white ) no-repeat; }
will automatically be converted into this:
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:
@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:
@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:
- Download/clone the files into your theme folder in a folder called
cssCrush
- in your functions.php file, add the following line
require_once 'cssCrush/CssCrush.php';
- in your header.php, replace your existing stylesheet
<link>
code with<?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.