What’s Your CSS Style?

Emily Lewis writing on Web Standards Sherpa about CSS Style:

When we think of CSS, we naturally think of styles. Styles to control layout, color and typography. Even styles to progressively enhance our user experience.

Lately, when I’m writing my CSS, I’m thinking about a different kind of style: the style I use to actually write my CSS … more specifically, my selectors.

Some good tips, as things to avoid that Emily noted, that are worth repeating here:

  • Don’t add a tag name to your id selectors. ids should be unique, so qualifying it with a tag (e.g. h2#title) is unnecessary and can slow browser matching.
  • Avoid complex selectors. Not only can they be a performance hit, but they can make dealing with specificity far more difficult than need be. I try to keep my selectors to a maximum of three.
  • If you are having problems with specificity, don’t turn to !important. This can not only lead to duplicate markup, but becomes a headache for future maintenance.