The Smashing Book #2 has a great section by Christian Heilmann on how and when to use css classes.
“Classitis” as Christian calls it is when classes are used everywhere. Instead of just using classes selectively to make certain elements standout, people add classes to everything and then let jQuery do it’s magic.
This can quickly turn into a maintenance nightmare because as soon as a class name changes, you’ll have to search your entire code base for all changes.
Here’s an example Christian gives on how NOT to use css classes:
<ul class="shopping-list first"> <li class="list-item">Cucumber</li> <li class="list-item important">Apple</li> <li class="list-item">Orange</li> </ul>
Christian’s main rule for applying css classes is:
add a class to mark the odd one out and to group elements not already grouped.
Here is how to do it right:
<ul class="shopping-list first"> <li>Cucumber</li> <li class="important">Apple</li> <li>Orange</li> </ul>
Instead of using .shopping-list .list-item to style, all you need is .shopping-list li.
You modify the important one with .shopping-list li.important, and then you don’t have to worry about the class specificity of .important versus list-item.
For more good examples of does and don’t about web and design, be sure to check out the excellent Smashing #1 and #2 books.
Jan 02, 2012 @ 08:01:00
Prior to HTML5’s vastly expanded semantics, I believe the tradition was to use IDs for major structural elements that will only be repeated once on a given HTML page (wrap, header, content, footer, etc), for JavaScript/jQuery hooks, and to style other elements that required special attention—as you cited above. With the introduction of HTML5 we can eliminate some IDs altogether as the now becomes which can be styled by invoking the following CSS: header {}. Classes on the other hand are not really intended to style major structural elements. They’re meant for smaller more specific tasks. IDs are best used for general global styles and classes are more suited for specific local styles, unless of course we’re talking about how they apply to javaScript/jQuery, in which case things are quite different.
-Stonefish
Stonefish-Web-Guru.com
Jan 04, 2012 @ 15:22:30
Hi Uriah,
I have noticed some of the HTML5 syntax handling stuff that might of traditionally been handled with IDs. Thanks for reminding us.
I agree, classes being used to small more specific tasks seems to be the trend,
Cheers,
Jonathan