Using Compass in your Drupal project

15
Jan
2013

Back in the day I used to roll with good old CSS and it did the job. Mostly because it didn't need any special maintaining and the code was simple.

Things changed when I first started working on some more beefy projects. Changing of various stuff followed by constant modifying of css was truly a pain in the ass, you ask why? Simply because there was so much css that maintaining caused severe pain in my head.

I'm sure a lot of people would agree that CSS can easily become a mess, no matter how careful you are.. Although Compass won't solve this problem completely, it will make your work more comfortable.

This article should serve as an informative post to all you guys writing regular CSS to consider using a preprocessor.

What is Compass?

A quote from the official website "Compass is an open-source CSS Authoring Framework" which pretty much sums it up.

Think of it as a CSS with extras or just CSS having Tom Selleck's moustache.. Wait what ?

American actor Thomas William "Tom" Selleck

Why Compass?

I'm not the programming type of guy, so I was kinda afraid at first(you know, command line and stuff).

But it's surprisingly easy to use. Installation is very straightforward and there's plenty of documentation available on the Compass website.

I've been doodling with it for some time and found it to be very clever and fun! I mean.. using nesting, variables, various helpers and mixins?

Syntax

You have two syntaxes, SASS or SCSS.. You can also combine both in the same project, but not in the same file(You don't put Sass into SCSS files.. and vice versa)

SCSS is basically enhanced CSS where you can use all the goodness Compass offers(keeping the syntax same).

I prefer Sass because it's cleaner and more interesting though semi-colons and curly braces are invalid. Since it's based on indentation(white-space sensitive or simply 'tabbing') you can make nice readable sections of CSS.

Example

SCSS:

#foo {
  border: {
    style: solid;
    color: #fdfdfd;
  }

  .content,
  .main-content {
    background-color: #ccd;
  }

  .content {
    border: {
      style: dotted;
      color: #fdfdfd;
    }
  }
}

Sass:

#foo
  border-style: solid
  border-color: #fdfdfd
  
  .content,
  .main-content
    background-color: #ccd
    
  .content
    border-style: dotted
    border-color: #fdfdfd

CSS:

/* line 3, ../sass/screen.sass */
#foo {
  border-style: solid;
  border-color: #fdfdfd;
}
/* line 7, ../sass/screen.sass */
#foo .content,
#foo .main-content {
  background-color: #ccccdd;
}
/* line 11, ../sass/screen.sass */
#foo .content {
  border-style: dotted;
  border-color: #fdfdfd;
}

Installation

To get this working on Windows, install ruby installer first.

Next open command line and enter:

  gem install compass

OS X or Linux:

  sudo gem install compass

Try it out!

Create a new project

  c:\>compass create my_project -x sass

This -x sass part means we're preferring sass.. Of course you can just leave it empty if you don't want to use Sass syntax.

  c:\>compass create my_project

That way we would use the default SCSS.

Compass watch

You can make Compass check your project for any changes and it will automatically compile them to a flat css that can be used anywhere.

Navigate to your project directory and type compass watch.

  c:\my_project>compass watch

  >>> Compass is polling for changes. Press Ctrl-C to Stop.

That's cool, but how do I use it in Drupal?

As I mentioned above, Compass produces a flat css file that can be used anywhere, and that's what we need.

Putting config.rb in the root of your theme folder is all you need to get it running.

I like to keep the folder structure the same as Compass produces, but that's really just a personal preference.

Any modules?

You can use Compass stylesheet tool module, but you'll need PHP and ruby on your server.

With this module you can compile CSS using devel or cron.

I believe there is more than just this one, but I had no need to try them out since it's just so simple to get it running.

I guess we're done here

We just scratched the surface here, there is so much more to learn.

Still not entertained?. Then you will want to read more about SASS and Compass. It probably won't make you a better coder, but will definitely save you some hair.

About the author

Tomas Vanya's picture

Drupal Themer - I'm just a common guy with passion and huge interests in 3D graphics, mostly game art and newest web technologies like HTML5. Nature is my number one inspiration and that's where I'd love to spend most of my free time.

LIKED THE POST? SPREAD THE WORD!

del.icio.usStumbleUponDiggFacebookLinkedIn

8 comments so far

adam's picture

Good intro. Thanks for the article.

The SCSS syntax is useful for existing projects because vanilla CSS is fully contained in the SCSS syntax. This means that you can import your existing stylesheets and slowly add SCSS goodness over time without throwing away all of your existing code.

Also, to be clear, SASS and Compass are separate projects, although you seem to be treating them as one and the same here. Their relationship is analogous to that of jQuery and javascript. It's entirely possible to use SASS independent of Compass, if you want.

noone's picture

Interesting point-- So then what does compass bring to SASS that makes it more desirable than SASS on its own?

adam's picture

Compass is kind mixed bag in terms of the functionality that it provides. Your best bet for this is to look here to get an idea of what it provides out of the box:

http://compass-style.org/reference/compass/

In the same way that jQuery has a plugins, you can also add to Compass using extensions. So, the sky's the limit in terms of the capabilities for Compass.

Zohar Stolar's picture

There are a few Compass (and SASS in general) powered Drupal themes, which give you a very big head start.
We use our own amazing Sasson (http://drupal.org/project/sasson) but there are a couple more.
Sasson uses the powers of Compass to bring advanced features like compass watch, responsive design, flexible layouts, and more, to a Drupal themer, making the themer's life so much fun!

Go compass!

boney's picture

Thanks, I was looking for some good documentation on compass. At http://www.webnethosting.net we have a lot of Drupal clients asking CSS related queries. Compass seems like a nice alternative to create simple easy css files. Is there any similar plugin for wordpress and joomla!

kim@PSD to Drupal's picture

Thanks for sharing the information,even I feel that compass is the better framework.

johny ragil's picture

I prefer Sass because it's cleaner and more interesting though semi-colons and curly braces are invalid. Since it's based on indentation(white-space sensitive or simply 'tabbing') you can make nice readable sections of CSS. http://www.clubmz.com/
http://www.prlog.org/10308124-cell-phone-spyware-best-spy-ware-for-cell-...

John's picture

Awesome! I have been using Compass for a while and I love it.

Add new comment