Tips For Coding Better CSS - Part II

May 21st, 07 | 2 remarks

series_stamp_2of2.gifLast week, in Part I of this set, I talked about small but significant ways to optimize your CSS. In this article I suggest some methods for keeping your CSS organized. While some of the tips here may counteract optimization techniques, they’re helpful in simplifying work within your stylesheet. If you’ve ever tried to work with a fully optimized file, you know what a cluster it can be to sort through. Save yourself a headache by trying some of the following… you can always crunch it up again when you’re ready to FTP.

Use prominent comments

One of the things I’m in the habit of doing is beefing up comments for the main portions of a site, like this:

/* -------------------
CONTENT
---------------- */

Obvious comments can save you a lot of time searching and scrolling during development, and later if you need to make changes after your design is complete. It also helps to “map” your site’s layout, giving you clear indicators of what’s what.

For example, I comment the most prominent sections such as Wrap, Header, Navigation, Content, Sidebar, Search, Comments, and Footer1. Anything else I usually group into Miscellaneous. Commenting my code in this way breaks up the CSS into easily identifiable blocks.

Give meaning to your structure

Every designer acquires a coding style which makes sense to him or her, but the technique I found most effective, is to organize blocks of CSS according to how the page renders. In other words, what section (or div) they fall under. This goes hand in hand with good commenting, and makes finding things a lot easier.

So for example, I’ll list basic HTML formatting rules for body, headings, anchors, paragraphs, images, etc., first. Then I’ll list styles for main sections, such as header, content, sidebar and so forth… down to the footer last. This way, if I have to modify something later, say in the comments section, I know to go directly to that block. Since the stylesheet “matches” the design, I don’t have to dig through my HTML first to get the div name for the item I want to change.

css_comments.gif

I’ve seen stylesheets where the code is grouped by element instead (all the titles together, all the lists, the links, etc). For me, that method makes it too easy to forget what belongs where. This is especially true if you have a complicated design, or often work on multiple sites.

Space out your syntax

A certain amount of “whitespace” is necessary to maintaining a usable style sheet. Good syntax may look something like this:

#content{
margin: 0;
padding: 0;
border:solid 1px #ccc;}

#content h2{
font-size:1.4em;}
Here the selector is on a line of its own, with each property/value pair indented, and on their own. This creates a nice readable format. Many people put the closing bracket on a separate line as well, but I prefer to bring the bracket up, then leave an empty line in its place. Sounds silly, but to me it just looks cleaner. I’m quirky that way. :)

Some designers prefer to skip this format in favor of putting everything on one line, and indenting inherited styles instead. While this does help to keep file size in check, I find it more difficult to scan through when looking for a specific declaration - particularly if the rule is lengthy. An example may look something like this:

#content{margin:0; padding:0; border:solid 1px #ccc;}
#content h2{font-size:1.4em;}

So, that’s it. I hope this tutorial at least gave you some ideas for organizing your styles in a way which makes them a little easier to work with. Do you have any useful (or quirky) CSS coding habits to share?

1Referring to Wordpress themes specifically.


  1. Greg King

    Good tips. It is nice to see someone focus on maintainability instead of optimization. I have found the easier I make CSS to read, the better I can do at removing duplicate style rules.


  2. What Makes a Successful Designer? at reflections

    [...] to Mike, Charity, Tara, Lisa, David Zemen, Paul Johns and Paul B for some fantastic comments on my last [...]

Post your remark

Other Recent Articles

In Reviews »

Clever Development With Coda

07/15/08
If you follow design related blogs, it seems you can't click a mouse anymore without Coda's icon making an appearance. I heard about this software long before buying my Mac, but because it's exclusive to that platform, I was never able to give it a test run. Now eight ... [ » ]

In Inspiration »

Have You Ever?

06/25/08
If you know me very well, you know how important music is in my life. It's playing at all times in my house, and I embrace all kinds of genres. I don't talk about it much on this blog, but I should. Music and creativity, in my opinion, are inextricably ... [ » ]

In Design »

The Evolution of a Logo (I Can Live With)

06/19/08
One of the most frustrating things for me since I started writing on this blog was the lack of a logo I really loved. That's not to say I haven't tried. But logo design, to me, is kind of a thing all it's own and I'm just not very good ... [ » ]