Coding Efficient Style Sheets

Nov 26th, 07 | 11 remarks

If you spend a lot of time working with different style sheets, it’s easy to get them mixed up or lose familiarity if there’s one you haven’t cracked open in a long time. One of the most useful habits I picked up was that of labeling (or commenting) my CSS. This gets me reacquainted with my code more quickly, and improves productivity in general. If you’ve never given much thought to code commenting, maybe it’s time!

CSS Branding

Wordpress themers already know that labeling the top of a style sheet and providing a screen shot is the only way to know which theme is which when viewing them from the Presentation tab. But you don’t need to be running Wordpress to know it helps immensely with keeping things organized (especially if you use multiple sheets for one website). Giving each sheet a name, description, URL (and a version number in some cases) is a good practice to get into.

/* Name: Client Site X
Theme URL: http://example.com
Description: 2 column, green, blue & white, fixed-width layout
Version: 1.0
Author: Charity Ondriezek
Author URL: http://designadaptations.com */

Grouping colors

I can never remember the hexadecimal codes for the associated colors I use on a design (or vice versa), so making a change to one is a pain unless I document my color scheme. Rather than copy/paste the hex number into a color picker to see for sure what color it is (or if it’s the right one), why not take the guess-work out and group all your colors together? I usually add something like this at the bottom of my style sheets. Then with a simple shortcut (cmd+end), I can go to the end of the page and grab the color I want immediately.

/* -------- COLORS ---------
dk red: #6f2422
lightest blue: #dbe9f4
lt blue: #acc5ef
mid blue: #3b5b81
dk blue: #091B2F
darkest blue: 0b1d32
--------------------------*/

Don’t forget the small stuff

I tend to keep elements chunked together based on their location within the layout as users see it. So all header styles are together under the label HEADER, and all sidebar styles are under the SIDEBAR label, etc. But it’s important to add comments for little things too, like what a certain style “does”. This helps a lot when working with a string of selectors, as sometimes seen in the Comments styling of a typical Wordpress blog.

/* visual distinction for author comments */
ol.commentlist li.author{background:#fff; color:#333;}
/* visual distinction for alternating comments */
ol.commentlist li.alt {background:#333; color:#fff;}

Scanning with Find & Replace

Ok so this doesn’t actually apply to coding, but it’s where commenting really pays off. Whether you use a full-blown development tool like Dreamweaver, or something lighter and simpler like CSS Edit or Style Master, the ‘Find & Replace’ feature in these apps is probably the single most important time-saver (particularly if your CSS is complicated).

Need to change one color throughout the page (or the site)? Using the color grouping tip, copy the hex you need to switch out and enter it into the ‘Find’ input box. Enter the new hex code into the ‘Replace’ input. Select the search criteria and click ‘Replace All’.

Or let’s say you want to skip to a section quickly to make some edits. Search using just ‘Find’ and it’ll take you to the first result in the list (your comment). Using the tip above as an example, you could refine this even more to search for a keyword or string, such as “author comments”.

If your CSS is separated, indented and/or commented intelligibly, it’s easy to scan through a document to find what you need even without Find & Replace, but I can’t go a day without it. ;) What about you? Got any (regular or quirky) CSS habits that increase your productivity? Is there a feature of your favorite editor you find yourself using over and over?


  1. Riccardo Giuntoli

    Simple use a CSS Framework like YAML [yaml.de] and you ‘ll have a perfect stylesheet(s).

    Best Regards Riccardo Giuntoli.


  2. Armen

    Nice post Charity. I think I’ll put this up on CSS Globe.


  3. Charity

    Hey thanks for the links guys, and for submitting my article Armen! I didn’t know about CSS Globe but I’ve added it to my bookmarks now. :)


  4. Armen

    No problem. It should send you a nice, steady stream of traffic for a day or two.


  5. Zeke

    I really like the idea of grouping colors so that I don’t have to use the color picker. Thanks for the tip. I don’t know why I never thought of it before. When I’m designing in Photoshop I always make a swatch that has all the main colors I’m using in the design.


  6. Chris Coyier

    I really like the concept of distinguishing author comments with a unique class in the CSS, but for this to happen, that unique class needs to be applied to only author comments in the XHTML. Could you explain how this is done?


  7. Charity

    @Zach - I do the same thing. But I find the color grouping in my CSS file very useful for when I’m too lazy to alt+tab over to PS. ;)

    @Chris - it’s done by adding a code snippet containing your email addy to the foreach loop in the comments.php file of your theme. But what a great idea for a tutorial post! I’ll write up something more in depth so I don’t have to squeeze it all into the comments area. I hope you’ll check back for it!


  8. Blue Gum Flat » Blog Archive » Site of the day

    [...] Coding Efficient Style Sheets | Design Adaptations [...]


  9. Refresh: Web programming for practitioners

    [...] This blog post provides a few simple tricks for coding efficient stylesheets — in the sense of having an organised structure which is easy to navigate and to modify. The color grouping seems like the most useful tip. [...]


  10. Israel Gershon

    A nice basic CSS tutorial. My girlfriend will find it very handy :)

    Cheers mate.


  11. design nottingham

    Grouping colours together… now why didn’t I think of that! Just to think of hours wasted scanning through reems of css.. :)

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 ... [ » ]