Please note that you may have been redirected (you might like to make a note of the URL in the address bar of your browser and update accordingly) This is a permanent archvie but is no longer actively maintained. Please visit http://joshuaink.com for the latest updates.
Avoiding classitis
Friday June 10, 2005
With a few of my compadres taking a more active interest in web standards, I find myself in the position of tea swilling consultant in chief. Which all boils down to more writing preaching. So, continuing along the theme of "simple introductions", here is a look at two methods I employ to avoid the overuse of classes.
Note: Those of you a bit further down the line may have something to say about the choice of an ID on the body tag due to CSS signatures and the likes, but this is way ahead of our particular game and so I have chosen an ID simply to save confusion. It is also a perfectly valid way of doing things.
Sometimes it's unavoidable
Perhaps the one "missing feature" of Internet Explorer that frustrates me most is adjacent sibling selectors. As an example, here is a list of links styled as a navigation bar using only adjacent sibling selectors, not a class in sight. But it does not work quite as expected in Internet Explorer, oh no siree. It does degrade quite gracefully in IE but out there in the real world, where IE is king, it's probably not going to be what we want. In this situation there would be little choice but to give each list item a class.
The point I am trying to make here is that, because of lousy browser support for nice CSS features such as adjacent sibling selectors, extra classes are sometimes inevitable. This aside, we can still take some steps to reduce them.
ID on the body tag
I came to this later than most but it is one of things I wish I'd know about when I was first starting. I really could spend all day going through examples of just how powerful an idea this is but let's just do something simple to whet your appetite.
Let's say you want a coloured box on your pages, you want it red on the Home page but green on the About page. You might, as this example shows, apply a class to the div and call it something like class="red-box" for the Home page. You might then put another div in place for the About page and give it class="green-box", as shown in this example. The HTML for these two boxes might look something like this:
- <div class="red-box">
- <p>Contents</p>
- </div>
- <div class="green-box">
- <p>Contents</p>
- </div>
And the CSS a little like this:
- .red-box {
- background:#900;
- padding:1em;
- width:40%;
- margin:0 auto;
- border:3px dotted #000;
- font:77%/160% Arial, Helvetica, sans-serif;
- color:#FFF;
- }
- .green-box {
- background:#9C0;
- padding:1em;
- width:40%;
- margin:0 auto;
- border:3px dotted #000;
- font:77%/160% Arial, Helvetica, sans-serif;
- color:#FFF;
- }
Three days later you decide that the Home page should have the green box and the About page the red, doh! Time to move around that HTML.
If we had, instead, given the div a more meaningful class name, let's say for example class="alert-box" and applied an ID to the body tag (<body id="home"> and <body id="about">) it would have been a lot quicker for us to swap around those colours. The HTML for both of those different coloured boxes might then look something like this:
- <div class="alert-box">
- <p>Content</p>
- </div>
The CSS could then be altered to work with the body ID, thus:
- #home .alert-box {
- background:#9C0;
- padding:1em;
- width:40%;
- margin:0 auto;
- border:3px dotted #000;
- font:77%/160% Arial, Helvetica, sans-serif;
- color:#FFF;
- }
- #about .alert-box {
- background:#900;
- padding:1em;
- width:40%;
- margin:0 auto;
- border:3px dotted #000;
- font:77%/160% Arial, Helvetica, sans-serif;
- color:#FFF;
- }
We can also take this a step further and optimise the CSS a little. As long as we know that every instance of .alert-box shares some common styling, and provided we are using an external style sheet, we could change the CSS to this:
- .alert-box {
- padding:1em;
- width:40%;
- margin:0 auto;
- border:3px dotted #000;
- font:77%/160% Arial, Helvetica, sans-serif;
- color:#FFF;
- }
- #home .alert-box {
- background:#9C0;
- }
- #about .alert-box {
- background:#900;
- }
Every instance of the .alert-box div now shares a common set of styling rules and we adjust each as we like depending on the part (e.g. Home or About) of the site we are in. So now our Home page example is green and our About page example is red and changing them back would take a few moments in the CSS file, whilst the HTML remains untouched.
Making IDs work harder
A good example of classitis, and something I see quite frequently, is a div used for the page head with a H1 inside. The HTML often looks like this:
- <div id="header">
- <h1 class="header-heading">The page heading</h1>
- </div>
The CSS that follows may end up looking like this:
- #header{
- width:80%;
- padding:1em;
- height:150px;
- background:#FF0;
- border:1px dotted #000;
- }
- .header-heading{
- font-size:90%;
- color:#000;
- text-transform:lowercase;
- }
Unless, for some reason, there are going to be multiple H1s -- all in need of their own style -- within the header div, we have no need of the class and can do this instead:
- <div id="header">
- <h1>The page heading</h1>
- </div>
The CSS that follows may then end up looking like this:
- #header{
- width:80%
- padding:1em;
- height:150px;
- background:#FF0;
- border:1px dotted #000;
- }
- #header h1{
- font-size:90%;
- color:#000;
- text-transform:lowercase;
- }
If we take a look at the example with the class and compare it to the example without, there is no difference in how it looks.
Conclusion
The benefits of reducing the amount of classes in your code may not be immediately apparent from looking at the simple examples given above. When applied to a complete site build, however, it can mean a significant reduction in the "weight" of the site; less CSS, less HTML, less time taken and a site that is easier to re-style on a whim.
Avoiding classitis does take practice but the key is planning. Sit with your design comps for a while, and start to plan how the HTML will be marked up, what can be shared? How can I/is it possible to target specific elements without using a class? As said, a few extra classes may be unavoidable due to crappy browsers but that's no excuse for not at least trying to avoid the dreaded classitis.




AkaXakA
1155 days ago
Joshua Brewer
1155 days ago
Alex Giron
1154 days ago
Martin W
1154 days ago
Thomas B. Aschim
1154 days ago
kartooner
1154 days ago
anonymous
1154 days ago
Joshua Kendall
1154 days ago
Justin Reid
1154 days ago
Chris
1154 days ago
Matt Wilcox
1154 days ago
Matt Wilcox
1154 days ago
Andrew
1154 days ago
Payton
1154 days ago
Tom
1154 days ago
Matt Wilcox
1154 days ago
Graham Bancroft
1154 days ago
Justin Reid
1154 days ago
Matt Robin
1154 days ago
Jens Meiert
1154 days ago
Kev
1154 days ago
hink
1154 days ago
John
1154 days ago
magicien
1151 days ago