Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

A touch of class

Rated 3.34 (Ratings: 14) (Add your rating)

Log in to add a comment
(4 comments so far)

Want more?

 
Picture of luminosity

Lachlan Cannon

Member info | Full bio

User since: October 13, 2001

Last login: October 13, 2001

Articles written: 5

Cascading style sheets give site authors a lot of power to play around with. Unfortunately, with that extra power comes the opportunity to abuse markup if you’re not careful. The W3C say so themselves:

Note. CSS gives so much power to the "class" attribute, that authors could conceivably design their own "document language" based on elements with almost no associated presentation (such as DIV and SPAN in HTML) and assigning style information through the class attribute. Authors should avoid this practice since the structural elements of a document language often have recognized and accepted meanings and author-defined classes may not.

So when is it right to use classes?

The short answer

The short answer is, not much of the time. Use classes when you can’t find anything else at all that will let you select the elements a different way.

The long(er) answer

Before you go and use a class you should check to make sure that none of the following would work instead:

  • Is the element you’re wanting to style unique on the page? If so, use an id. An id atribute can be applied to any element on the page, and must be a unique identifier which can’t begin with a number. After you’ve applied an id to an element you can select it for styling by using the selector #id. For example, to select a div with an id of monkey you would use the selector #monkey.
  • Are the elements you want to style the only of their type within a uniquely identified object? For example, if you had a list of links that was the only list of links within a div with an id of menu, then you should select them using that.
  • Is there an existing HTML tag that you can use on the items you want to style, and is the display you want to use typical of how all elements with that tag would be displayed? If so, just use and style that tag. For example, using the <q> tag for marking up quotations, or the <code> tag for marking up fragments of code. HTML includes many of these semantic markup tags.
  • Is there an existing selector you could use to identify just these types of tags, for example the href starting off with http:// to select external links? Use that instead. (Of course this one might vary, depending on browser support for the selector and how necessary it is for everyone to be able to see the effect. If you do find yourself having to use a class in this situation, you should use the other selector too, so that in the future you can yank the class out and it’ll still work.)
  • So on…

Wrap-up

If you can think logically through the problem and find some other way of selecting the element using semantic markup, you should always use that. Markup already defined is useful to non-human processes (such as a search engine spidering your page) as well, since it already has a defined meaning, as opposed to your classes, which machines wouldn’t have a clue about. If you go through your code and think about other ways to select your items, I would be extremely surprised if you needed more than 2 or 3 classes.

The only situation I can think of which would need a lot of classes would be marking up something HTML wasn’t designed for, in which case you should probably be using your own XML-based language instead.

Born in Melbourne, Australia, Lachlan is soon to begin study at University.

Lachlan delights in being sent money, and other small gifts. But if you really have the money to spare ;) give it to something more worthy.

The Longer Answer?

Submitted by teradome on July 9, 2002 - 17:51.

This article brings up some good points.

Honestly, you should always structure your HTML with semantic tags. This is the essence of HTML and why, for example, working an HTML namespace into your XML is better than reinventing the wheel. Sure, using <quotation/> may be clearer than <q/>, but there's plenty more clients that understand <q/> out of the box without you having to write a long schema expressing what you mean.

When building sites, especially accessible sites, it's incredibly valuable to look at tags purely for their semantic value and then style them from there. Why would you <i> italicize text? Are you <em> emphasizing or <cite> citing a source? Putting up a gallery of photos? Please don't use <div class="list">. That's an <ol> ordered list of images...unless there's no order implied, and then it's <ul> unordered. CSS will do the rest for you. You can replace the standard display:list-item definition if it doesn't fit in your overall design...but when you load that same HTML onto a Palm OS device, you'll find that it will work instantly, because the proper meaning has already been applied to the content, and not just been chosen because it renders a little like what you wanted for Internet Explorer.

In this context, CSS Classes are essential. You should almost always use classes in addition to IDs. Inheritance is the most important point of CSS and it is there where you can find the one-document-makeover-of-an-entire-site holy grail of site design everyone talks about. Classes are CSS' way for creating the same relation between HTML elements that semantic tags do for their content, and by extension, IDs should only contain unique changes from a basic definition. The rest should be in a class. Why? Simply because classes can be reused, and IDs can't.

All of this makes the difference between:
&lt;div id="header"&gt;&lt;img id="company-logo" /&gt;&lt;/div&gt;
and:
&lt;h1 id="top-page-header" class="content-header"&gt;&lt;img id="my-company-logo" class="header-logo" /&gt;&lt;/h1&gt;
all the more obvious.

login or register to post comments

very relevant...

Submitted by Junglee on July 18, 2002 - 01:56.

this is quite true. i once had a designer submit a design that was chock-a-block with classes, not related to any semantic markup element - the guy had even applied the same class to completely different elements in the design...
maybe i should forward this article to him.

login or register to post comments

Well written

Submitted by trfc791 on September 15, 2002 - 05:53.

I agree. But the choice between using classes and not using classes should also be dependent on my website and the audience in particular. For example, my site is accessed mainly by friends and people using higher-end browsers so I'm not hesitant to use them freely. In fact, all the content in my site is in some class of some sort. However, a site aimed for the visually impaired, for example, would use virtually no classes since they do not aid in drawing in the audience the site is meant for. I still believe classes should be used sparingly, especially if you don't have to, but it does depend on the kind of site you are developing.

login or register to post comments

class warfare

Submitted by hogvapor on September 16, 2002 - 11:59.

What about similar 'groups' of elements? For example, it may be helpful to have a class of list, including both <ul> and <ol> elements, that share certain UI similarities. If this will always be the case, obviously ul, ol {} would do the trick - but certainly this is not always the case.

Reading this article, I'm not really sure *why* I should avoid classes - avoiding the incompetent use of classes is not the same as avoiding classes altogether. I think that if a developer is specific, and thinks through the 'definition' (criteria for application) of a class, then classes are just peachy-keen.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.