CSS Golden Rules: Class Names
Friday 4th March 2005Name your CSS classes according to what they are for, not what they look like.
That is, use names like codeblock
, quotation
, document-title
, error-message
and so on.
I’m currently having to deal with a mess of stylesheets for a client which are full of classnames like bigblack
and reddots
and bottomrightbox
. They are just not helpful. I can see what it looks like by reading the stylesheet, what I want to know is what these classes are expected to be used for! If you have classnames like this, they’ll end up being used indiscriminately, and then you’re in real trouble if you ever want to change anything, because you have no idea what might be affected. But inevitably, at some point, the presentation of the class will get changed, but you can’t change the name because it’s too widely used, so it becomes downright misleading — you have a bigblack
class which actually makes the text green or something…
Even worse are names like nobordertable
. Here we have an extra dose of useless or wrong information, with the implication that this class is for use with tables (again without telling me what *sort* of table it’s for). Well why not specify that with a selector like table.classname
? Then maybe I wouldn’t be finding this class applied to divs, spans, blockquotes….
Addendum 22nd March 2005: The W3C’s explanation is probably clearer.