Tips for Coding Better CSS - Part I

May 17th, 07 | 8 remarks

series_stamp_1of2.gifOpen source design communities and Wordpress theme sites are wonderful because they offer you the ability to dive in head first and see what’s going on behind the scenes of a design… and without costing you a dime. I love to see how other people code their CSS, and I’m always picking apart themes just to see what I can learn from them.

It’s amazing actually, the differences in the way people approach the task of structuring their CSS. After a while, you begin to get a sense of who really knows what they’re doing, and who has a good grasp, but needs a nudge in the right direction. On that note, I’d like to share with you a few things I’ve learned, which help me stay consistent when working with CSS. I’ll start with some optimization tips, and move on to cover ideas for organization (in Part II). Stay tuned. :)

Ditch that measurement

From time to time, I see themes which have a measurement attached to values of zero in the stylesheet. For example:

margin:0px;

It’s actually unnecessary, as no measurement can really be attached to zero, because well, it’s zero! This may seem insignificant, but if you have an extensive stylesheet, shaving the pixels/ems/percentages (or whatever measurement you choose) off your declarations with zero values can save precious bytes when it comes to file size.

Work the cascade

You may have heard of the terms divitis (or classitis), and it’s a poor coding habit that befalls many beginners. Along that vein, I’ve seen several themes where styles were added via extra divs or classes, but the same effect the designer was trying for could’ve been accomplished just by using element declarations within the cascade.

The use of too many divs clutters up code, increases file size, and affects semantic markup in an adverse way. There are times when extra hooks just can’t be avoided, but whenever you can, try to use the natural cascade of your XHMTL documents. Here’s a brief example of an extra hook:

.title{color:#69c;}
< div id="content">
< h2 class="title">Title< /div>

Using the cascade, it would look like this:

#content h2{color:#69c;}
< div id="content">
< h2>Title< /div>

Though the class of title still carries meaning, it isn’t necessary. Using the cascade instead means lighter CSS, and lighter HTML. Fore more on the cascade, here’s an excellent article.

Keep it short

I love CSS shorthand. Not only because it reduces files size, but it’s less to type! Border, color, background, font, margin and padding styles can all be condensed into shorthand. Most seasoned designers are aware of this, but for you beginners, it’s definitely something to run with. So for example, rather than coding:

margin-top: 10px;margin-right: 2px;margin-bottom: 10px;

margin-left: 5px;
With shorthand, you can simply code:

margin: 10px 2px 10px 5px;

The order goes clockwise, but an easy way to remember is by using TRouBLe as an acronym of sorts - or Top, Right, Bottom, Left. Shorthand can even be taken a step further if you have repeating values. So if you want a top and bottom margin of 10px, and a left and right margin of 5px, you can code:

margin: 10px 5px;

If your margin on all sides is 10px, simplify it even more with:

margin: 10px;

Cool huh? Ok you get the idea now.

Go global

Another well known and yet underused tactic for swiftly setting page styles is by using a global selector. You can “zero out” any default browser settings for margin and padding, which will inadvertently affect your design, with one simple rule:

*{margin: 0; padding: 0;}

This global rule seriously reduces code bloat by saving you from repeating margin/padding settings all the way down the cascade. The asterisk functions as a wildcard, and any declarations set forth under it will be applied to everything, so be careful adding anything to this. ;)

That concludes Part I of this tutorial series. If you enjoyed this article feel free to comment with any optimization tips of your own, and I hope you’ll check back soon for Part II!


  1. Paul Enderson

    Actually, sometimes there is a valid reason to specify a zero value - if it’s to reset a particular browser’s styling.

    See this for more info: http://developer.yahoo.com/yui/reset


  2. Charity

    You’re right Paul! Specifying 0 is common and often necessary, and I see now that maybe I wasn’t very clear on that first tip. I was trying to suggest that a unit of measurement with 0 is unnecessary. So declaring {margin:0PX;} only needs to be declared as {margin:0;}. Does that makes sense? If you take another gander at the code you linked, you’ll notice there’s no specific unit attached to any of the zero values - they’re just zero. :) Sorry if that was confusing.


  3. Paul Enderson

    OK. I get that now you’ve pointed it out! I do remember reading a while ago, an article that said that measurement type *should* always be specified…

    I can’t find it now (of course), but I have a funny feeling that it was something to do how browsers make use of CSS when printing a page. I wish I’d bookmarked it!


  4. Charity

    What do you know, I’m in the same situation! I can’t find the original source where I read that tip. I thought it was something I saw long ago in one of my CSS books, but now I can’t be sure. If you ever remember where that article was, please drop me a line. I’d be curious to read it.


  5. Quick and Easy Tools For Optimizing Your CSS | Design Adaptations

    [...] you read my article on tips for optimizing your stylesheets, you may already be putting some of those things into practice. Here then, are a few nifty (and [...]


  6. Tara

    Love your tip about how to remember top right bottom left (see I remember already!) I was wondering how I would get that to stay in my head. Thanks


  7. Charity

    Sure Tara! Every little bit helps right? ;)


  8. Tips For Coding Better CSS - Part II | Design Adaptations

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

Post your remark

Other Recent Articles

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

In Freelancing »

Family and Freelancing - The Ultimate Balancing Act

05/29/08
A couple weeks back FreelanceSwitch had an excellent article on how so many freelancers tend to disrespect their own time. It was an article I seriously needed to read, because I'm one of them. It's often not by choice, however. Due to parental demands or household responsibilities which must be ... [ » ]