Post Archive

› May 21, 2005

CSS management tips

  • Reported by Alessandro

Articles on CSS organization and optimization are always welcome by webdesigners. Recently, two articles have catched readers' attention on the subject. The first one is CSS Organization Tip 1: Flags by Douglas Bowman and the second one is Modular CSS by Mike Stenhouse. Both articles advise the precious practice of organizing your CSS by sections but in quite opposite directions.

While Bowman's article suggests a precious hint: simply comment a section with a flag in your single CSS file, Modular CSS indicates a way to split a CSS in multiple files. This last technique could be good for organizing, but difficult in practice. Suppose you're going to redesign, or you may want to offer a style switcher or some alternate style sheets for instance. You'll have to deal with fragmented code and several files. I've always used a single, section-commented CSS file, but that's just a personal strategy.

Talking about CSS management: I wrote an article in Italian more than one year ago which indicates 21 tips to writing optimized CSS. One of the most valuable in my opinion is about ordering properties. Apart from some exceptional cases, the order of declarations in a rule is non influential on how the page will render, but a logical order of properties has always helped me in writing, reading and re-editing my CSS files.

When writing a single rule, I always think of it as a house building from outside to inside. So, how it is displayed and how it's positioned are the first two things. Then the special features (ie the specific element properties, such as text-decoration or border-collapse), the distance from other houses, the thickness of external and internal walls (respectively margin, border and padding) and finally the inner details: background, color and the properties about text. So, that's the ordered list of properties that I use when writing a single rule:

  1. display
  2. float and position
  3. width and height
  4. Specific element properties
  5. margin
  6. border
  7. padding
  8. background
  9. color
  10. font related properties

Hope you will find this approach useful as I do.

Comments

1. May 21, 2005 07:43 PM

Quote this comment

Oliver Posted…

Very helpful.

2. May 23, 2005 06:44 AM

Quote this comment

Mike Stenhouse Posted…

I just came across this as a link-back to my Modular CSS article and thought I'd add a little something... If you want to implement multiple stylesheets with my modular approach then you do the switch on the container file. If your alternative designs feature a different layout but the same typography then you can reuse your typo sheet (@import it into both), leaving you with two more maintainable variations. That said, if your designs are completely different then you'll end up with 10 stylesheets insead of 2, which'd be a bit awkward to maintain. It's all about the project at hand...

Incidentally, I tend to order my CSS properties consistently as well. It makes scanning far easier...

3. May 23, 2005 08:17 AM

Quote this comment

Tony Posted…

I tend to use Mikes approach myself. I generally call the various sheets (typo, layout, design) from a midpass filter sheet, allowing me to break out my IE hacks (iewin.css and iemac.css). Just to granulate further....

4. May 24, 2005 12:10 PM

Quote this comment

Klaus Hartl Posted…

Ordering properties eases your live a lot, especially if you are not the only one who works on a style sheet. In my firm we have established code conventions for HTML/CSS in which, among others, we define a certain order for properties: display, display-specific properties (list, table, ...), positioning, box model, font and so on.

I am now planning to create a CSS definition file for TopStyle Pro with that properties order. It can than be used for the Inspector and the Style Sweeper.

And in case you are interested: I wrote a follow-up to Mike's post about reusing modular style sheets efficiently with some additional thoughts.

5. May 26, 2005 01:45 AM

Quote this comment

Alessandro Posted…

Mike, I fully agree with you: it depends on the project and the coding habits over all. To the readers: it's worthy to mention Mike Stenhouse's follow-up to his article on Modular CSS, a CSS framework.