Preface: This is a geeky post about web development markup languages. If you don't understand HTML and CSS you can probably switch off now. Cascading Style Sheets (CSS) have been with us for 12 years which, in internet time, makes the technology nearly as old as the Earth itself, and yet so many people still seem to struggle with the most basic concepts. In this post I will give several examples of bad CSS along with an explanation of what is wrong in each case. Let's start with an easy one:
What's wrong here? Well, if you put a different class on each kind of element you want to reference you get a bad case of classitis which adds unnecessary markup to your pages. In this example, you don't need the "menuLink" class because you can just use the CSS descendant selector instead:
This second example isn't much shorter (in fact it's a few bytes longer) but the real benefit is in legibility and maintainability. If you decide you want to make all your status messages larger you only need to do it in one place. It's also worth explicitly pointing out the difference between classes and IDs: IDs must be unique on each page and each element can have only one ID whereas classes can be applied to several elements and an element can belong to multiple classes. In the example above you can only ever have one error message on a page. If you want to be able to display several error messages at the same time, you need to use class names:
You can't use presentational words in class names! CSS is all about the separation of content & presentation. By using words like "left", "red", "large" in class names you're mixing your presentation into your content. Instead, use names that describe the purpose of the elements such as "warning", "introduction" or "important". That's it from me - what CSS mistakes have you noticed in other people's code?