Tidy up your stylesheets
It’s 2009, and while I’ve cleared up my desktop clutter, reassigned new icons to my folders, and archived projects from 2008, there’s something that remains unchecked on my to-do list: clean up CSS code. See, I’ve got a bunch of CSS stylesheets that are live, powering different websites, that are shamefully, shamefully disorganized.
Now, the tidying up of CSS doesn’t sound sexy- and it isn’t. But a clean, well-organized stylesheet is a joy to behold. Especially when it’s five months later and time to make major style changes on a website you haven’t worked on, let alone thought about. With an organized stylesheet, there is very little chance of pulling one’s hair out while wondering why that font won’t change size or begging that sidebar to please, please move 20 pixels to the right.
The truth is, it isn’t very hard at all to tidy up the code on a stylesheet. Here are five quick ways to do it:
- Use sections. Title these. Organize all style definitions into sections, for example: Basic Page Styles, Links, Header, Content, Footer. Make sure to title each of these sections with headers. Yes, a bit of a pain, but oh how you’ll hug yourself later.
- Indent, indent, indent. I heart the tab button on my keyboard, especially when it helps me differentiate various rules (a column within a column, for example) in a stylesheet.
- Think semantic. When deciding what to name your CSS ids and classes, use names to properly identify them as they are (“navigation-menu”) rather than where they are (“top-bar”). That way, when you need to move the navigation menu from the top of the page to the bottom, it won’t suffer an identity crisis.
- Use shorthand. Learn CSS code shorthand (or, like me, use a cheat sheet) and use it. Because padding: 10px 0 5px 3px;is way better than
padding-top: 10px;
padding-right: 0;
padding-bottom: 5px;
padding-left: 3px; - Use master stylesheets. As web designers, web browsers are our medium. Unfortunately, despite it being 2009, browsers still don’t all play nicely together. Minimize the risk of your lovely styles disappearing by using master stylesheets, which basically clear all default browser settings, giving you a clean slate to apply your styles on. You can see an example master stylesheet here.
There are, of course, many other ways to organize your CSS stylesheets- and I’m a firm believer that everyone should use the organizational system that works for them. It might be good to keep in mind, though, that unless you’re working on your personal website, there’s a chance that another designer will have to work with your stylesheet in the future. So be kind to that designer, and try not to be too cryptic. That designer could be you.


Want an avatar? Get a gravatar! • You can link to this comment
You should also add onto #2 by creating a table of content at the top of your stylesheet.
/* Site Table of Contents
1. Base Styling – What can be found in this section?
2. Page Constraints
3. Header Styling
4. Generic Classes
….etc
*/
This is helpful for you, but also for the next developer to come along. On occasion also supply a one liner explaining what can be found in the section. Now for you byte crunching developers this will add a couple KB’s in the mix.
This is something I do for all my sites and the clients really appreciate it.
Nick
Want an avatar? Get a gravatar! • You can link to this comment
Even on complicated markup, I’ve never needed to use a master stylesheet to achieve cross-browser compatibility. I find it often is as large as my CSS (if not larger!) itself. Aside from a few typical IE6 bugs (IE, double margin), a simple one-line reset seems to do the trick.
* { margin: 0; padding: 0; }
Want an avatar? Get a gravatar! • You can link to this comment
Nick, thanks- a Table of Contents on every stylesheet would definitely make a lot of designers happier
Chris- that one-line is excellent indeed, and one I use a lot. But many times I don’t like what it does to my form styles.
Want an avatar? Get a gravatar! • You can link to this comment
Um.. I hate tabs.
I don’t like inflating code and remove whitespace wherever I can. Besides, its easier to look at things without tabs, as you can see more without scrolling.
For projects, I usually have nice little sections. I don’t often use titles unless I am handing something off to someone else that I haven’t worked with because my names for IDs and Classes are obvious. For example, navigation might be #nav. Sidebar is #sidebar. etc…
And in the CSS on my site, I’ve removed all whitespace. So it’s a bit scary to look at. Fortunately, I am very familiar with it. And a simply ‘find’ enables me go to whatever I am looking for.
As for a master stylesheet, I think that I will start to employ that. I get completely irritated when someone goes in and pokes around in code and destroys the few things that they should NEVER touch. So a master stylesheet would be great for separating “DON’T EDIT!!!” from “Can Edit”. haha. …definitely a good idea.
Nice tips.
Want an avatar? Get a gravatar! • You can link to this comment
While there are some useful tips here, I think indenting is not a good idea when writing a stylesheet, though it depends on how you organize it in the first place. I like writing all my declarations belonging to a selector on one line. This can become quite long in length, so indenting actually sends part of my css off the screen.
I myself usually use three levels of headings. One for main sections in my css file, one for components and one for component variants. Probably better illustrated with a picture:
http://users.telenet.be/onderhond/css-file-structure.png
Anyway, apart from the clean look it also helps with finding elements even without using firebug and other tools. It makes your css files more predictable and easier to work with for third parties.
Good article.
Want an avatar? Get a gravatar! • You can link to this comment
I always use headed sections in my stylesheets. The problem often then becomes deciding whether a rule goes in one section or another. Some styles are clearly global, like setting the default font and line-height. Others can easily be assigned to a particular section – say the padding on #content or #footer. But what about making a font tweak in the #footer? You might want to put that in the #footer section, but what if that style is best shared between #header and #footer because it applies to both? And there’s something to be said for having all typography styles in the same place, so it’s easy to make global changes to the typography – but that takes a lot of discipline. In practice I think you’ll always end up relying on ‘find’ in your editor.
Want an avatar? Get a gravatar! • You can link to this comment
Use Firebug; keep your hair.