Which approach is better to write CSS code? In general I always prefer to use a methodic top-down approach I want to present you in this post. I called this process Four Bubbles Model. The model is based on four progressive phases that helps you quickly develop CSS files and maintain a better control of code you’re writing. The following picture illustrates the four main phases that compose the model:

This model is thought to help web designers develop optimized, simple to maintain CSS files, minimizing the number of classes and avoiding redundant and inconsistent declarations. The following paragraph contain a quick explanation about each single phase.
HTML standard elements
The first phase of the model concerns the reset and redefinition of browser styles for HTML standard elements. For the reset I start adding only a small set of elements that I’m sure to use in my pages, gradually adding new ones when I need.
body, h1, h2, h3, p, ul, li, form, input{
border:0;
margin:0;
padding:0;
}
ul{list-style:none;}
I follow the same approach to redefine HTML standard elements. For example, here is the basic set of HTML elements with which I start:
h1{...}
h2{...}
h3{...}
p{...}
a:link{...}
a:visited{...}
a:hover{...}
a:active{...}
input, textarea{...}
...
<p> for paragraphs, <a> for links, <h1> to define the title of your posts, <h2> to define subtitles and so on.
Main sections
The second phase of the model concerns the definition of the main sections that compose the structure of HTML pages. The picture to right illustrates the main sections of a typical two columns layout. I always use the following standard declarations to identify each section:
#wrapper{...}
#header{...}
#nav{...}
#main{...}
#sidebar{...}
#footer{...}Then, for each section, I define the style of its child elements. For example here is the code for the unordered list that I use for the navigation links:
#nav ul{...}
#nav ul li{...}
#nav ul li a:link{...}
#nav ul li a:visited{...}
#nav ul li a:hover{...}
#nav ul li a:active{...}And here is the code for the links of the footer and for paragraphs within which to add some final information about the web site (such as copyrights and so on.)
#footer ul{...}
#footer ul li{...}
#footer ul li a:link{...}
#footer ul li a:visited{...}
#footer ul li a:hover{...}
#footer ul li a:active{...}
#footer p{...}
Custom classes
The third phase of the model concerns the definition of custom classes for all custom elements that compose web pages. An example of custom class is the class .right that I use to align to right HTML elements or the class .feed-icon that I use with a background image for the title of RSS Feeds section.
.right{float:right;}
.feed-icon{...}
In this phase it’s frequent to add a lot of unnecessary declarations that add lines of “garbage” code into your CSS file. A typical example of unnecessary declaration is the definition of a class .title for the title of posts when you can simply use the tag <h1>.
Code optimization
The last phase of the model concerns the optimization of developed code, removing unnecessary or not used declarations, grouping properties where it’s possible and reducing the length of comments. Final steps is to make a lightweight version of your CSS file using a CSS compressor. These tools allow you to remove exceeding spaces and lines break form your code minimizing dramatically the size of your files. There are some interesting resources that compress CSS files for free such as http://www.csscompressor.com/ or http://www.cssoptimiser.com/. The only advice if you use these tools is to avoid overwriting the original source file otherwise could be very difficult to modify it afterwards using a basic editor. If you use an advanced editor like Dreamweaver this is not a problem because you can use the option “Apply Source Formatting” to revert compressed files to their original formatting.
Conclusions
How you can see the model is very simple and intuitive and I’m sure it can be very useful to help you adopt a methodic approach to CSS coding. For suggestions and improvements about this model please leave a comment.
by
Antonio Lupetti is an italian engineer, pro blogger, Mac user, founder of woorkup.com. He lives in Rome, Italy. Follow Antonio on 

October 9, 2009 at 4:46 am
Antonio,
This succinct post is going to provide an AHA! moment to many people, beginners, journeymen, and masters alike.
Form meets Fundamental meets Function.
Haphazard is as haphazard does. This formula, or method rather, should assist in keeping just that many more liver-zapping ibuprofens out of the mouths of designers/developers. :)
Bravo, and thanks for another enlightenment.
Michael
October 9, 2009 at 12:04 pm
Thanks Michael :)
October 9, 2009 at 10:02 am
Gonna try it. Thanks for sharing!
October 9, 2009 at 10:47 am
Very interesting, it’s cool to see a bit of “method” in the world of the web development, it’s the first time I see a name on a method concerning CSS and I find this good for the credibility of our works! Thanks
October 9, 2009 at 11:23 am
I prefer add custom class before main style section.
October 9, 2009 at 11:39 am
Thanks Antonio for describing a sensible process. I am curious to hear how your final CSS file is ordered. It makes sense divide your CSS file into groups, corresponding to the main sebsections of your page. Child elements and any other elements appearing in each section would obviously be grouped in the appropriate subsection.
But what about custom classes and ids that either appear in many subsections or that are specific to a certain page? Do you put these at the beginning or end? or interspersed between the various subsections?
cheers-
Dave
October 9, 2009 at 11:47 am
Thanx for sharing i will test it.
October 9, 2009 at 2:30 pm
Do you use (c) copyright for your images and website?
What about using Creative Commons?
October 9, 2009 at 3:06 pm
Thanks michael. Im using 960.gs as base css, your help will be tied
October 9, 2009 at 3:07 pm
Good post! Why not use Hawidu CSS to make this workflow super-simple?
October 10, 2009 at 9:40 pm
it’s good but not compatible with IE
October 9, 2009 at 3:29 pm
Howdy! Great site. Great content. Great! I can recommend this site to others!
October 9, 2009 at 3:43 pm
Nice start.
I use a similar approach. But I usually don’t define default rules ( styles ) for the standard elements.
I just jump to the custom classes part. That’s where I usually get those standard elements styled.
And I don’t support the “compression and optimization” idea. CSS’s are so tiny and clear. You won’t benefit that much with compression and optimization. CSS isn’t a programming language.
October 9, 2009 at 6:13 pm
I practiced this – but I didn’t know it had a name :)
October 9, 2009 at 9:44 pm
I’ve been using this method for a while now to relatively good effect. As has already been recommended: I also put my custom classes before the main structural elements.
One thing I started doing more recently that has really helped is to order my attributes alphabetically. #nav a { color: #000; display: block; float: leftl font-family: Helvetica; font-weight: bold; text-decoration: none; } etc … that way the attributes are always where you expect them.
Good post. Thanks.
October 9, 2009 at 10:40 pm
that’s actually how i’ve been doing too. But it is great to see you putting it in such nice way !!
October 9, 2009 at 11:59 pm
Great post, very informative. Thanks
October 10, 2009 at 3:10 am
I really had no idea this practice had a name and had always deemed it commonsensical. Anyway, charming post, as usual!
October 10, 2009 at 4:54 am
A wonderful post!.
I was assigned to this method naturally though i usually forgets to do the fourth bubble. CSS compressor and optimiser :- both are two awesome tools.
October 10, 2009 at 4:57 am
thanks for sharing, excellent
October 10, 2009 at 6:03 am
Thanks Michael. It was a great post.
October 10, 2009 at 6:17 am
This is pretty much how I’ve always done my work. It just makes sense to me. I also have a tendency to do everything inline when I first put a site together and then move it to an external stylesheet and optimize it as necessary… It just always seems to go better for me.
Great article.
October 10, 2009 at 6:45 am
This is how I’ve done CSS for years. To me it’s the most logical format since it uses proper cascading of styles. Base styles (element level) are defined first, then each level is overridden by the next.
October 10, 2009 at 7:08 am
Excellent post !
October 10, 2009 at 7:23 am
I tend to agree with Fabio Silva that CSS compression is an unnecessary complication for many (most?) websites. Otherwise, this is a very good framework.
October 10, 2009 at 11:09 am
Excellent framework and well explained. Thanks!
October 10, 2009 at 12:10 pm
Nice post. Where I’d differ a bit, particularly after reading css recommendations from the Google Page speed firebug plugin: http://code.google.com/speed/page-speed/docs/rendering.html and Mozilla’s efficient CSS article: https://developer.mozilla.org/en/Writing_Efficient_CSS is to use MORE custom classes rather than less. Understanding how the browsers parse the CSS makes you approach your CSS coding differently.
October 10, 2009 at 2:36 pm
I love the way css-programming is envolving. This is how I usually structure my css-files:
* Reset
* Grid
* HTML Standard Elements
* Utilities such as float, clear etc
* Layout
* Section specific stuff like menus, lists inside sidebars etc.
October 11, 2009 at 9:10 am
ow..ow..ow.. i like it. Brings inspiration
October 11, 2009 at 10:13 pm
Thank you, I’ve been looking for a method to organize my css, and this was just what I needed.
October 13, 2009 at 3:28 am
Specially like the visuals.
November 10, 2009 at 11:43 pm
Good Information. Thank you .
November 10, 2009 at 11:44 pm
Very important information.
December 13, 2009 at 6:30 am
Hi, Thanks for this wonderful post, I will certainly be watching for more.
Cheers
January 19, 2010 at 5:24 pm
Thanks for this post Antonio. Works a treat.
sf