views:

160

answers:

5

I have a messageboard and one of my users has written a greasemonkey script to stylize various elements within the page. He did a great job with it but in order to make his job easier, the first step in his script is to parse the current page and add several css classes to almost all html elements on the page. most of them aren't used to style the page at all, but instead to make it easier for him to query the couple elements per page that he will actually modify. for example class="thread_started_by_user_123 thread_with_456_posts thread_with_789_views thread_last_posted_in_by_user_12345" etc etc

is this a standard practice? are there any downsides to adding lots of unnecessary css classes, either in javascript, or on the server if i were to add them to served pages too.

+2  A: 

In terms of semantics, yes there are downsides. If you have classes that aren't descriptive of what the element actually is and are there for styling purposes only, this can hurt you down the road should you decide to redesign and/or restructure.

for instance, BAD:

<div class="header red left-side">

GOOD:

<div class="header main current-event">
Jason
+1. Overdoing it can be bad as well though, if you have too specific classes (i.e. posted_in_2006 might be relevant but posted_at_8_30_pm is probably completely irrelevant)
You
yeah the excessively specific classes is what i'm worried about. i don't want to get bombarded by requests "can you add such and such class to these elements" but a few important ones i can see being ok as long as the processing/rendering/bandwidth concerns don't put a stop to the whole thing
qntmfred
i'm not saying go overboard and put metadata in the classes... you can, of course, if you want to and they are relevant to what the element is (classes can do more than just style, obv they can be used for jscript/jquery/etc), but if you're relying on them only for styling, i'd prefer to keep them to two classes, three at the absolute MOST
Jason
+1  A: 

Classes are not just for CSS, they're for the categorization of sections of markup. Applying styling based on that categorization is just one use. So if the classes are useful for other categorization purposes then that is fine, and perfectly good practice.

Alohci
+2  A: 

If there is no associated style with a class that's assigned to element, then I believe the browser will just ignore it. So it will not increase your page processing time a lot if that's what you are worried about.

However, if there are lots and lots of classes for each element, then you have to realize that you are using valuable bandwidth and increasing the time it takes to load the entire page that way. You can avoid that problem by externalizing the CSS so that the browser can cache it.

Are you using jquery to query the elements that you really want to modify? It might turn out that its more easy to pick those elements with jquery selectors which seem difficult or impossible with standard JavaScript and thus you can possibly avoid all these extra unnecessary classes.

Bottom line, there is no problem in using lots of classes if they are needed, and that's perfectly fine for production, but if you don't need them, like in your case, there has to be a better solution that you can possibly come up with.

desigeek
yeah we're using jquery selectors
qntmfred
+2  A: 

This looks to be using classes to embed arbitrary metadata into elements, which is certainly not what the class attribute was designed for. Given that its effects begin and end with a greasemonkey script and are thus localized to the client, it seems a harmless enough hack, but not one I'd advise duplicating on the server side.

HTML unfortunately doesn't provide much in the way of alternatives when it comes to metadata other than sticking in invalid attributes, so there is a mechanism that does add semantic meaning to the "class" attribute within existing tags -- namely microformats. There's a lot of breathless buzzwordy hype around microformats, but overall they do revolve around a best practice for areas where going all-xml is out of the question.

thanks, this is one of my concerns as well. i didn't get the sense that css classes are _supposed_ to be used in this manner, but given the lack of suitable alternative, had it become practice to use them that way anyways.
qntmfred
+1  A: 

Just for a data point, I took a look at GMail's HTML yesterday (their buttons are very nice) and it's full of that kind of thing.

Here's an example:

class="goog-imageless-button goog-inline-block goog-imageless-button-collapse-left goog-imageless-button-collapse-right"
AmbroseChapel