Frank W. Zammetti wrote:
Laurie Harper wrote:
Why do you say that's a bad practice? It's actually both supported and recommended; in fact, the 'name' attribute is deprecated in favour of the 'id' attribute so 'id' is the only way to target CSS to a particular page element rather than an entire display class.

...

OK, you could use class="something-I-made-sure-is-unique" instead of using id, but why do that and loose the ability to have uniqueness checked by validation?

Exactly. class is what should be used. Well, ok, if I'm being fair, this is a debatable point, I can't point to any document somewhere that says there is a right and wrong answer. It is a bad practice to me though based on my experience :)

The reason for this, in my mind, is that an ID attribute must be unique across a document so that you can uniquely address any individual element on the page by name. This makes DOM manipulation much easier, and frankly more like the back end where you are either addressing a specific object instance by name, or accessing the member of a collection. But that's maybe getting a tad off track I suppose :) ...

By separating the CSS class an element uses from the ID you access it with you gain flexibility in being able to manipulate individual objects as well as change the stylesheet more precisely. More importantly perhaps, it eliminates duplication of style definitions.

For example, imagine a situation where you have a series of textboxes, and you want to apply a style to each and also be able to address each individually via scripting...

<input type="text" id="css1">
<input type="text" id="css2">
<input type="text" id="css3">

Ok, so I can certainly address each individually by name. But what does the stylesheet look like? It would have three styles defined that are all duplicate. Kind of inefficient. But, if you do:

<input type="text" id="tb1" class="css1">
<input type="text" id="tb2" class="css1">
<input type="text" id="tb3" class="css1">

Now, you can still address each individually, and now they all use the same style definition. Duplication removed.

I never said you *shouldn't* use 'class' to style elements :-) That's the right answer when you want to apply the same set of styles to multiple elements (although even without the 'class' attribute here, you'd still only need to define one rule in the stylesheet... it would just jave 3 selectors).

Actually, I did find one reference that seems relevant:

http://www.htmlhelp.com/reference/css/style-html.html#id

Specifically, the first sentence of the last paragraph:

"The use of ID is appropriate when a style only needs to be applied once in any document."

That's another way of saying what I was trying to say above :)

Note that there's no corollary there that this is in any way inappropriate, though. If I only *want* to apply styles to one element in the document, this is the way to do it.

But, even putting all that aside... assigning a CSS class to an element with the ID attribute just seems wrong semantically... ID has a pretty specific meaning, it's a unique identifier. class also has a fairly specific meaning in this context, it's defining the supertype of an element (in essence), and doing it otherwise seems like fighting logic :)

There's a difference between class and style, though. Class and ID are two different 'hooks' by which you can associate style with (one or more) elements. CSS provides all sorts of selectors besides class selectors, after all.

I'm not sure about your validation point though... could you clarify that? You may well be pointing something out I've never considered.

'id' is guaranteed unique in any valid document, whereas 'class' is (obviously) not. So, if I know that the thing I want to style should occur exactly once (for example, a title at the start of an article) I can assign it an ID. By attaching styles via the ID, I can be sure those styles will never be applied anywhere else in the document, because the ID must be unique. If it's not, I'll get an error when I validate the document.

On the other hand, if I use 'class' instead of 'id', that class could be re-used elsewhere in the document. That won't be caught by validation, or anything else but visual inspection of the result.

OK, so it probably not something you care about very often ;-) My point was just that, while IDs must be unique and that uniqueness is checked during validation, no such restriction applies to 'class'.

Not trying to go on the attack or anything here :-) Just curious where this is coming from...

Sure, no problem at all. Like I said, I can't point to any official spec or something that says "this is the right way to do it", but I know what I have my people do and I know what has served me best over the years :) I have no doubt you and others have contrary experiences that are just as valid.

Heh :) Have you had any experiences where using CSS ID selectors was actually bad, or caused problems?

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to