“…there is a huge likelihood that the systems will evolve from lower to higher entropy, which is a rough statement of the second law of thermodynamics.”
— Brian Greene
Add Meta Information
Without a scaffold, CSS has the tendency to grow like an untamed garden during the development process. To begin designing a scaffold for your styles, it’s helpful to make comparisons between your stylesheet and a book.
Much like stylesheets, books are descriptive narratives that rely on a strict organizational scheme in order to make sense. So it follows that—just as books use a “table of contents” to allow for easy navigation between sections—we too can generate a system that allows us more easily navigate and maintain our CSS.
At the top of each stylesheet, I list meta-information about the stylesheet itself and provide a table of contents that will help other designers understand the structure of my markup. My list usually includes these items:
- Author
- Title
- Description
- Table of Contents
- Important Colors
By prepending an “=” sign to your content list entires, you can easily navigate between important sub-sections of your stylesheet by using the “find” feature of your chosen editor. For example, if you wanted to quickly navigate to the “typography” section, you would search for “=T.”
Organize Property/Value Pairs
Craftsmanship is all about paying attention to details: meticulousness begets quality. I always make it a point to alphabetize the property/value pairs inside my declaration blocks. Doing so makes it much easier locate specific properties inside of long declaration blocks and adds a level of predictability and usability to my stylesheets.
Of the two examples below, which do you think is easier to parse?
Group Dependent Property/Value Pairs
We can take the organization of declaration blocks one step further by logically grouping dependent property/value pairs. This is a technique I use sparingly so as not to disrupt the rhythm of my markup, but I have found it helpful in two common situations: with positioning offsets and with browser specific CSS3 declarations.
In the case of positioning, I feel that it makes sense to indent and group positioning offsets like “top,” “right,” “bottom” and “left” underneath the property they’re helping to define, in this case “position.” I often change the value of more than one offset at a time and to me it makes sense to place them in the context of each other in the mark up.
Quarantine CSS3 Declarations
Browser specific CSS3 declarations tend to be unruly and visually disruptive so I make it habit to quarantine them from the rest of my property/value pairs. I feel it also makes sense to separate these styles so they are easier to update as browser begin to support them natively (in which case the browser specific extensions can be dropped).
Admittedly, when doing this you’ll need to take the cascade into account, but as a rule I find this technique to be a helpful time saver.
Format Declaration Blocks Consistently
The way your format your stylesheet is largely a matter of personal preference&emdash;I have seen many different approaches&emdash;but there is at least one rule that must be followed strictly: be consistent!
I format my declaration blocks following a simple method. If there are less than three property/value pairs, I keep the declaration block on a one line:
If there are more than three property/value pairs, I format the declaration block using multiple lines.
Add Some Panache
Finally, a little trick I picked up from designer/front-end developer Dan Cederholm: add a little panache to your stylesheet by including an ascii version of your client’s logo.
While perhaps completely unnecessary, I’ve always felt that taking this extra step lends an air of quality to your work. It’s a memorable and surprising touch that says: I care.
Here is a link to my favorite ascii art generator:
http://www.text-image.com/index.html