Web Development by Alistair Robinson

« Blog home

CSS Abstracted: Update


June 8th, 2010 4 Comments

In a recent post I described my first foray into…well now, what am I supposed to call them? CSS frameworks? No, that doesn’t work, because Blueprint and YUI and 960 and others are often described as such. CSS meta-frameworks? That appeals to me, but it’s rather obscure. CSS compilers? Yes, perhaps.

But wait. There are CSS compilers, and then there are the libraries and frameworks and plugins that are built around them. Well, I guess that’s where the term “meta-framework” comes in.

One such meta-framework is Compass, which is built around the core language and compiler Sass. I mentioned Compass briefly in my other post. At the time, I had dismissed it as being more than I required, or more than I wanted to get into, but in the end I realized if I was going down this route at all I might as well do it right – and Compass does it right. It seems by far the most thorough, mature solution I’ve found, with a good heritage and a lot of success.

So that’s what I’ve settled on using now. There might be some kind of tighter integration achievable if you’re using Rails, but what I’m doing is using it to generate CSS as I locally develop a site in WordPress, and the same for a site built on the GetSimple CMS, and a Django project as well. You can use it with anything, and when you come to deploy it you can exclude all the Compass stuff, leaving no trace of it on the live site, except perhaps in the tell-tale auto-generated layout of the CSS code.

Your best bet is probably to go read the excellent documentation, but here’s some basic stuff from my own experiences…

Initial steps:

1. Install Ruby
2. Install Ruby Gems
(if you’re on a Mac you may have these already. I guess you could find out by carrying out the next step)

3. Install Compass from the command line:

> gem install compass

With these installed, whenever I begin a project I set up Compass in the theme or template directory by running the following command inside that directory:

> compass create --syntax sass

There are loads more options for the create command (See the documentation). Note also I’m using the SASS syntax, because I prefer it. There are two choices, and projects default to SCSS. See here for more information on the differences.

Incidentally, it’s interesting that SCSS is now the default syntax for Sass (confusingly, the name of the language/compiler is also the name of one of its two syntaxes). The SCSS syntax looks just like CSS itself, and the idea is to lessen the unfamiliarity for designers who are already very skilled in CSS. After all, there’s no reason in principle why they should have to learn a new syntax in order to take advantage of the power of Sass and Compass, which is independent of the syntax, and independently valuable.

However, I’m sticking with the SASS syntax, because I find it neater and easier to write, and I don’t mind admitting that it’s simply a welcome novelty.

That create command creates a configuration file, config.rb, and three directories, most important of which are src and stylesheets. src is the where you write the code, and stylesheets contains the generated CSS.

The last essential piece of the puzzle is how the CSS is generated when you’re developing. All you need to do is run the following command in the directory in which you set up Compass:

> compass watch

This runs in the background and watches for changes, then re-generates the CSS when it finds them.

Getting ready to code

I’m currently working with a config.rb that looks like this:

require 'ninesixty'
# Require any additional compass plugins here.

project_type = :stand_alone
# Set this to the root of your project when deployed:
http_path = "/"
css_dir = "stylesheets"
sass_dir = "src"
images_dir = "/images"
output_style = :compact
line_comments = false
# To enable relative paths to assets via compass helper functions. Uncomment:
relative_assets = true

It’s good when starting out to use a _base.sass “partial” to set up your variables and imports. Mine looks like this:

@import compass/reset
@import compass/utilities
@import 960/grid
@import compass/css3
@import my_mixins.sass

// VARS
$box_shadow_color: #666
$light_grey: #DBD7CA
$bg_blue_color: #AFE4E4
$bg_pink_color: #FBBBC0
$bg_orange_color: #FDBB79
$font_family: "Georgia", "Times New Roman", "Serif"

// OVERRIDE DEFAULTS
$default-border-radius: 3px
$default-text-shadow-h-offset: 0px
$default-text-shadow-v-offset: 0px
$default-text-shadow-blur: 5px
$ninesixty_grid_width = 960px
$ninesixty_columns = 24

+font-face("Tuesday Regular", font-files("tuesday-webfont.woff", woff, "tuesday-webfont.ttf", truetype, "tuesday-webfont.svg", svg), 'tuesday-webfont.eot')

See here for more on Compass best practices.

Mixins

Notice at the top of the base partial I’m importing another partial file, _my_mixins.sass. Mixins are wonderful: they let you collect together styles and mix them into your stylesheet wherever you like. Compass comes with many of them, but you can also define your own, which is what _my_mixins.sass is for:

=my_hover_link()
  text-decoration: none
  &:hover, &:focus
    text-decoration: underline

=my_focus()
  &:focus
    outline: dotted 1px #222

These are used in the actual stylesheet source code (eg. in screen.sass) like this:

a
  +my_hover_link
  color: #004379

Grids

I’m also using the 960 grid system (Compass also comes with Blueprint). With Compass, using a grid system is very nice indeed.

#wrap
  +grid-container
#content
  +grid(10,16)
  +alpha
  +clearfix
#side
  +grid-prefix(1,16)
  +grid(5,16)
  +omega
  +clearfix

One thing that’s good about this set-up is that you can code using a grid system without using any unsemantic class names like “prefix_2 grid_8.” Compass takes care of that when it generates the CSS. Effectively you’re using the grid as it really should be used, only in design and development. Compass calculates the relevant widths for your semantically-named elements, based on the 960 mixins, and there’s no underlying skeleton of unsemantic classes left behind in your HTML and CSS.

NOTE: Using +grid-container instead of +grid-system(12) means you don’t end up with all the 960 grid classes in your style sheet, which won’t be required because of your use of the mixins.

This is just the basics. There’s lots of stuff online, but you can’t do better than the Compass documentation, and it’s worth taking a look at the site of Chris Eppstein, the man we have to thank for it all.

Tags: , , , , ,
Posted in web development | 4 Comments »

4 Responses

  1. Any reason you’re mixing the Sass 2 style variables with Sass 3 variables?

    That is, Sass 2 are ! prefixed with underscore separators. Like: !variable_name

    But Sass 3 variables are $ prefixed with hyphen separators. Like: $variable-name

    Sass actually understands both formats, which means even if the base library is out of date and using ! and underscores, Sass will map your calls to $ and hyphens to the correct variable.

  2. Alistair says:

    Hm, I did wonder in passing why I was doing that.

    Thanks, I’ve updated it.

  3. [...] CSS Abstracted: Update – Web Design and Development in Edinburgh Share and [...]

  4. [...] the original post here: CSS Abstracted: Update – Web Design and Development in Edinburgh This entry was posted on Tuesday, June 8th, 2010 at 10:13 pm and is filed under Uncategorized. [...]


©2010 Alistair Robinson