Coding Efficient Style Sheets
Nov 26th, 07 | 11 remarksIf 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.
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.
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.
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?

Simple use a CSS Framework like YAML [yaml.de] and you ‘ll have a perfect stylesheet(s).
Best Regards Riccardo Giuntoli.
Nov 27, 07 | 3:15 amNice post Charity. I think I’ll put this up on CSS Globe.
Nov 27, 07 | 6:27 pmHey 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. :)
Nov 27, 07 | 9:35 pmNo problem. It should send you a nice, steady stream of traffic for a day or two.
Nov 28, 07 | 6:28 amI 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.
Nov 29, 07 | 1:44 amI 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?
Nov 29, 07 | 12:09 pm@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!
Nov 29, 07 | 1:32 pm[...] Coding Efficient Style Sheets | Design Adaptations [...]
Nov 30, 07 | 2:02 am[...] 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. [...]
Dec 11, 07 | 5:16 amA nice basic CSS tutorial. My girlfriend will find it very handy :)
Cheers mate.
Mar 25, 08 | 4:46 amGrouping colours together… now why didn’t I think of that! Just to think of hours wasted scanning through reems of css.. :)
Jun 19, 08 | 2:54 am