If you’re tired of typing vendor-specific prefixes for CSS properties you should take a look at the Bourbon library which includes several SASS mixins and functions to make working with CSS more convenient. In this episode we’ll show you how to use it in a Rails application.

Adding Bourbon to a Rails Application

The application we’ll be working with is shown below. Its design needs some work so we’ll add Bourbon to the app and use some of its features to improve the way the page looks.

Installing Bourbon is easy. All we need to do is add it to the gemfile and run bundle.

To use Bourbon with Rails’ asset pipeline we’ll need to change the way that the default application.css file works. By default this file will use a Sprockets manifest to load each of the other stylesheet assets. The problem with this is that Sprockets compiles each SASS file into CSS individually which makes it difficult to share the Bourbon mixins across the SASS files. To fix this we can use SASS to load all the scss files instead of Sprockets.

We’ll need to rename our the application.css file to application.css.scss, then we can then remove the Sprockets manifest and use SASS’s @import command to pull in the files that we want to include. For now we’ll just include the bourbon file provided by the gem.

/app/assets/stylesheets/application.css.scss

@import"bourbon";

We’ll need to restart the Rails server for these changes to be picked up. After we’ve done that when we reload the page all the styling has gone.

If we look at the /assets/application.css file in the browser we’ll see that it’s blank. Bourbon doesn’t add any CSS to our application directly, it just makes it more convenient for us to add our own CSS through SASS. It’s still up to us to write all the CSS we need for our application so we’ll put the layout and project files back in application.css.scss. We set up these CSS files earlier but now we’ll be able to use Bourbon in them.

/app/assets/stylesheets/application.css.scss

@import"bourbon";
@import"layout";
@import"projects";

When we reload the page now it will bring us back to the design we had earlier.

Modifying The Header

The first improvement we’ll make to our design is to change the font. The page currently uses the browser’s default font, which is usually Times New Roman. Bourbon includes a font-family add-on which provides aeveral variables for setting the type of font. These include fallback fonts for when the first font required isn’t available. We’ll use one of these to set the body’s font.

Next we’ll add a gradient to the header. Bourbon provides a linear-gradient module and we can use this to add a gradient and set its colours. We’ll add a gradient that fades from light grey to a darker grey. Note that we haven’t set a direction for the gradient; this means that the default value of top will be used.

A default background colour is included in linear-gradient so we’ve removed the background-color property from the header. We also want a drop shadow under the header and Bourbon provides a mixin for adding these. We can use this in a similar way to the box-shadow CSS3 property but the mixin will generate all the vendor-specific prefixes for us. We’ll add a black shadow with no offset and with a six pixel shadow with a three pixel spread.

Modifying The Items

Next we’ll make some changes to the list of items. First we’ll add rounded corners to the box that surrounds each project. Needless to say Bourbon has a border-radius module to do just this. The project-specific styling is in a projects.css.scss file so we’ll make this change there and add a six pixel radius.

Next we’ll change the way that the background colour of each item changes when we hover over it. Instead of it changing immediately we’d like it to fade in to that colour. We can use transitions to do this, passing in the attribute we want to change, the time it should take to do the change and the easing effect.

Now the background colour will fade in over half a second when we hover over a project and fade out over the same time when the cursor moves out.

The last change we’ll make to this page is to improve the “New Project” link. We’d like it to look more like a button instead and Bourbon includes a buttons add-on that we can use to make different styles of button. Our link has a new_project class so we’ll use that to style it and turn the link into a simple green button.

/app/assets/stylesheets/projects.css.scss

.new_project {
@includebutton(simple, #3FB344);
}

When we reload the page now we’ll see the rounded corners and the styled link. (You’ll have to take the background colour fade on trust.)

The button has inherited some attributes from the anchor tag such as the underlined text and we want it to be a little larger. We’ll add some more CSS to do this.

An Example Of The CSS That Bourbon Generates

Now that the page is looking better let’s look at the CSS that Bourbon has generated. We can’t show it all here as there’s a lot of it but as an example here’s the generated CSS for the header and we can see the code for the gradient that we added.

There’s a lot of code here that we’ve not had to write manually thanks to Bourbon.

SASS also includes several functions that can help out with generating CSS when dealing with colour changes, variations and adjustments. There’s more information in episode 268 on what can be done with SASS alone.