CSS id and class naming
May 30, 2010
I'm a developer, I like structure, I like clean code, I like DRY code. I find writing CSS incredibly painful.
CSS1 was first published in December 1996, so I ask, why in 2010 does it feel like CSS is just as painful as it was 14 years ago?
A few great CSS tools exist to make the process of defining layout and structures much easier. A few different examples include; Baseline, Sass, Less, 960, Yahoo! grids and Blueprint. Unfortunately nothing quite cuts the mustard. We've tried a few of these at Unboxed, but they still fail to some degree on the bigger projects.
Where are the standards? You can't lay all the blame on IE6 can you? Recently Jason Cale dedicated a fairly lengthy blog post to why you should hit return whilst writing your CSS.
A very simple problem I often find is what I should name different elements, and if I should use ids or classes. After often hearing the two @ubxd CSS heroes Attila and Will have lengthy debates on the best way of doing things, I think the real answer is that there isn't one.
So I decided to look around the web, see what other people are doing, see what the top websites of the world were doing. I downloaded the top 1 million websites from Alexa, and wrote a quick app that scrapes these sites, and tracks the top names used for ids and classes.
So here are the results (I stopped at 20000 results):
|Top class results|
|Top id results|
So, not that interesting I'm afraid. But what is interesting?
- Some of top ids are elements in HTML5 for example "footer", "header" and "nav" so we're making some progression in standards.
- The use of id and class seems to be used interchangeably, there really is no common standard.
- We see heaps of sites using "footer" and "header" as a class name. Is that ok?
So even though the results are not too interesting, I think they serve as a little reference guide, so that in the future I can name things similarly to other people, so that other developers will be able to easily read my code.