Blog

Front-end Maintainability with Sass and Style Guides

By | January 19th, 2011 at 6:01PM

At Engine Yard, the User Experience team works frequently on the front-end (HTML & CSS) of our application, AppCloud. It’s also common that our Ruby developers write front-end code as well. When the UX team began realigning the AppCloud UI, it involved refactoring the HTML & CSS for better efficiency. We also created a style guide to document the new front-end architecture. We’re sharing the methods we’ve used and how it’s helped us to work more efficiently on the AppCloud UI.

Modular CSS

Haml and Sass are two gems that are easy to include into your Ruby on Rails application. At Engine Yard, we use both of these to maintain our front-end code. Sass variables and mix-ins are amazing resources that make managing styles much easier. This is fantastic for maintaining UI elements and adhering to brand guidelines. Within our AppCloud front-end framework we have variables we’ve set up for our colors based on our branding color palette. That can look something like this:

// --------------------------------------------------
// COLOR
// --------------------------------------------------
$color_border =           #ccc      // light grey
$color_background =       #f8f8f8   // lightest grey
$color_background_alt =   #cfdbe8   // light blue
$color_background_alt2 =  #e3e5cf   // light beige
$color_text =             #555      // dark grey
$color_text_alt =         #999      // medium grey
$color_text_alert =       #910f0f   // bold red
$color_text_callout =     #236DA6   // bold blue

This way, when we need to use the color for our borders, we can just call on the color variable, $color_border. If we decide to change this color, we only need to change it in one place. If we need variations on this color (like for shadows and highlights), we can use that variable combined with Sass’s built-in functions:

h1 {
  border-bottom-color: darken($color_border, 10%);
}

We’ve used the “color_” prefix in these variable names to help avoid any clashes we might run into. It also helps provide some inline context (documentation is always a good thing). The important thing here is we keep our variable and mix-in names descriptive, but we avoid using presentational names that could change one day. By using $color_text_callout instead of $color_text_blue we don’t have to change all of our variable names if we change the text from blue to red. We can use comments to help remind us of hex value colors which are located in the one place we’d make changes, if need be.

Variables and mix-ins are a great way to help improve and clean up semantic HTML (if it’s done right). In our last related blog post, we mentioned that we’re using the 960 Grid System which uses CSS classes like “grid_1”, “prefix_9”, and “alpha”. For the semantic front-end neat-freaks out there, this can be cringe-worthy. However, if you’re not already using a framework like Compass, you can simply modify the 960 Grid System, allowing you to have the best of both worlds (modular efficiency and clean, semantic HTML). You can turn all the class names within the framework into mix-ins, which will make HTML go from this:

<div id="sidebar" class="grid_1 prefix alpha">
  (content goes here)
</div>

to this:

<div id="sidebar">
  (content goes here)
</div>

That’s so much better, isn’t it? We can do this for other common styles like clear fixes, rounded corners, visibly hiding elements, etc. If you define your UI elements’ visual design styles as variables or mix-ins, you can have a much easier time updating these styles in a consistent way.

Maintaining an Interface Style Guide

Interface Style Guides are an essential way to keep track of branding and visual design guidelines and front end architecture requirements. The key to keeping a style guide useful is that it should stay relevant and informative. However, this can be a tedious task that often gets forgotten or avoided.

Online style guides are easier to maintain and distribute as opposed to printed documents. To keep our style guide relevant, it lives in our internal-only admin section on the very same application it describes. We display our color palette alongside the relevant Sass variables and since we’ve built the style guide into the application using the same front-end, we can use the same variables we’re referencing to render this palette. When we change values to these variables, the palette updates automatically.

Another benefit to adding your online style guide to your application is you get one central place to check to make sure all HTML elements are looking the way they’re supposed to. If the styles are broken here, most likely they’re broken elsewhere.

Finally, this method allows development teams to have a reference guide so they know what code they would use to get the effect they’re looking for, such as icons.

We continue to evolve the AppCloud front-end through iterations; good organization and documentation is essential to make this front-end architecture work for both designers and developers on our team. What methods does your company or team use? We’d love to hear from you.

  • Anthony Short

    I'd love to see a live, working example of your styleguide. I've been using very basic styleguides for our sites, but I'd really like to see how other people are tackling the problem. Do you include blocks and layouts in the styleguide or just titles and text?

    We've been using vanilla CSS and I've found keeping the styleguide up to date is difficult. Once I convince the other team members to switch to SASS I think it will be much easier to maintain.

    One other thing I've found with styleguides is that they really help to modularize your CSS, as you won't be able to put the styleguide together easily if you haven't.

  • http://www.behance.net/designabile Francesco

    Very interesting article.

    I'm working on a RoR application and I'm looking forward to introducing Haml and Sass in next release.

    Thank you for sharing your way.

    All the best

  • http://drnicwilliams.com drnic

    As an insider, I've now looked at it and its awesome. For many of the sass modules, there is a sample page for each showing how buttons, forms, widgets will look & feel. Its deployed with AppCloud and accessed to internal staff via the Admin console. Very nice.

  • http://www.milkrock.net Ramiro Jr. Franco

    I'd also be very interested in looking at a live style guide, or even just a sample of the one used at engine yard.

  • http://www.cliquetools.com Eric Lanehart

    Very timely, I was just working on such a guide myself the other day for our application, even did the same trick of using Sass variables to drive some palette swatches. The style guide is just one piece of a larger project to standardize our UI components, constructing a "toolkit" with accompanying HIG with heavy inspiration from Apple, Palm, and Android. Over time I want to incorporate knowledge learned from user testing as well.

    It's been a hard sell to management but I feel at some point our web applications grow sufficiently complex to where maintenance can really become a blocker to progress. Desktop applications are built atop systems such as these, but the web's otherwise wonderful openness pushes us all to reinvent the wheel on a regular basis in regards to UI, even within our own projects.

  • http://rax.tumblr.com aurelian

    Probably not the best way to display sass color related variables and show the color palette: https://gist.github.com/788099

    Ended up with this http://twitpic.com/3rr8dk

    Works for me(tm).

  • Johan

    Great post; Sass is truly awesome.

    I extracted the most frequent stuff I always write in CSS into some sort of a personal Sass framework. Includes reset, utils, helper classes and a system of imports. Check it out here: https://github.com/johanbrook/dyluni. Suggestions are welcomed.

  • http://rohitarondekar.com Rohit Arondekar

    Nice article. I'm a recent SASS convert and have found it very useful in simplifying my stylesheets.

    But I think you could have also mentioned the ability to nest rules. That has helped me make my stylesheets so much more maintainable.

    Would love to see another article on the style guide implementation if thats possible. :)

  • http://www.sessiondigital.com Edward Chen

    Great post! we're thinking of using SASS on a project which utilises the same html framework, but loads of different colour themes. At the moment we're separating the colour elements into a separate stylesheet and creating classes for these border/background/text/link colours etc. but It'll be great to try out the SASS technique. Love the idea about a style guide too!

  • Tim Brandes

    Coming from paper based advertising all you usually got is a small CI book but thats not appropriate for the web. I have an application where a user can choose from a set of colors, so discovering how to put together a flexible set of variables thats easily extensible lead me directly to sass which serves great for this purpose. Very enjoyable read :)

  • http://ptcampbell.me Patrick

    A good read Jina, nice article.

    Is it true these variables might be introduced to the official CSS spec, thus rendering Sass and such obsolete?

  • http://heybigname.com Justin Seiter

    Yeah, it looks like variables, mix-ins, etc are currently being tested and could end up official sometime down the road. I don't think SASS or Less will become obsolete. They'll all just become options for a designer to consider before starting a project. On that note, as it stands, some of the pieces of the proposed native CSS syntax just look too wordy to me.

  • http://websitecenter.ca Iouri

    'Compass style' framework comes with modified version of blueprint css. So you can modify the dimension of the grid and the size of grid elements.

  • Dale Sande

    Hey Anthony, I have been creating ‘styleguides’ for my day job for a while now and have come up with a pretty standard solution that I like a lot.  

    It is written in PHP/LESS but you will get the idea.  I am currently working on a Ruby/SASS version as well.  http://axle.dalesande.com/styleguide/

  • http://www.levityisland.com/ Lawrence Wang

    “You can turn all the class names within the framework into mix-ins”

    I’ve thought about doing it this way but one concern is that it adds a lot of duplication to the final “compiled” stylesheet that the browser gets. Have you observed any performance impact from this?

  • http://twitter.com/ScottKellum Scott Kellum

    Using @extend in Sass prevents duplication by consolidating the properties in one place.