Class & ID - Looking for some clarification (Full Version)

All Forums >> [Web Development] >> Cascading Style Sheets



Message


Donkey -> Class & ID - Looking for some clarification (9/1/2007 22:37:49)

I understand that IDs are used for items that appear only once on a page, and classes are used for items that appear multiple times on a page and like most of you I use both in the appropriate way.

But Why? The ID seems to be a waste of time - why not just use a class but, here's an idea, if you only want to use it once on a page you can, it works - I've done it.

ID's are great for identifying anchors so that you can link to a particular point on a page, but they really aren't much use in applying CSS rules; classes win every time.

To me there is no logic in having a "special" way of applying CCS rules to an element that only appears once on the page, I can't see that it serves any useful purpose. If it does why not have another for rules that only ever appear twice on a page, or three times or four?

Am I missing something or is the ID just pointless? I think we only use it because we are told we must and its continued use is just custom and practice.

I contend that the use of ID in this way is of no value because it doesn't make building the page easier, doesn't help the page to validate and doesn't make it more accessible.





d a v e -> RE: Class & ID - Looking for some clarification (9/2/2007 1:44:29)

if i remember rightly id's have a greater weight - as far as inheritance goes - than classes, which could be useful




Tailslide -> RE: Class & ID - Looking for some clarification (9/2/2007 3:16:48)

You could perfectly well build a site using classes only.

There are a couple of reasons why using IDs can on some occasions make life easier:

Firstly, if you're using scripting then you need IDs to identify an element using getElementById().

Secondly, as you point out - you can use them as anchors which is very useful.

Thirdly, as Dave pointed out, although you can apply two classes to an element, an id will always hold precedence over a class placed on the same element no matter where on the stylesheet it's written.

Usually with CSS the rule further down the stylesheet will overrule those preceding them - but with this for instance:

#orange p {color:orange;}
.blue p {color:blue;}


the paragraph will still be orange as the id has greater specificity. You'd have to add a class to the paragraph tag itself rather than just the div.

Older browsers didn't understand multiple classes (class="big green") and you do have to be careful even now how you write out your styles concerning multiple classes as IE can mess things up for you if you try to be too clever.

So in the end, yes you can perfectly well create a page without any IDs if you prefer and if you don't use either anchors or javascript.

It comes down to personal preference - personally I feel happier giving the big, unique chunks of the page an id and then giving the smaller, repeated, non-structural stuff classes . But that's just a personal preference apart from the fact that I do use anchors and javascript.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.046875