johnnyk Posted July 5, 2006 Share Posted July 5, 2006 I've always used classes for everything, but I read that you should use and ID if you're only going to use it once. Is there really any benefit to using an ID (considering it would work with a class also). Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/ Share on other sites More sharing options...
moberemk Posted July 6, 2006 Share Posted July 6, 2006 IDs have a higher specificity rating, making them overrulling over classes. So when you give something a class and an ID, the ID's rules come first. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-53655 Share on other sites More sharing options...
Daniel0 Posted July 7, 2006 Share Posted July 7, 2006 I believe IDs have to be unique in order to have valid XHTML (possibly HTML too). So imagine a forum system, where each post row has an ID called 'postrow', that would not make the ID unique, so you'd have to use the classes instead. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-54336 Share on other sites More sharing options...
ShogunWarrior Posted July 9, 2006 Share Posted July 9, 2006 ID are good/neccessary when: parsing, using Javascript, CSS stuff etc.For instance to highlight a row you'd need the ID of that table row.Also, it is better for styling, like:[code]<tr><td class="box" id="left"> </td><td class="box" id="center"></td><td class="box" id="right"></td></tr>[/code] Because you've used class and id you can now assign all the boxes styles, and then tweak each specific one. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-55171 Share on other sites More sharing options...
Daniel0 Posted July 9, 2006 Share Posted July 9, 2006 Yeah, but according to W3C all ids have to be unique in order to be valid XHTML:[quote]An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).[/quote]So you could just use an id attribute on things you know you will be using javascript on. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-55182 Share on other sites More sharing options...
WendyLady Posted July 10, 2006 Share Posted July 10, 2006 Hmmmm -- I'm surprised no one has said this before:A class definition can be used many times on a single page -- for example, a paragraph or a span can have the class [b].bold[/b] and this can be used throughout the page, many times. It is typically a more general style.An ID definition is more restrictive, and each ID should only appear once on a page. An example of this is #submenu --> or using the id="current" for navigation, to show which page a person is on. There is only going to be one submenu on the page with this style, or only one tab can be "current". If you use IDs multiple times, you can get interesting results in different browsers.Introduction to IDs vs. Classes:http://www.tizag.com/cssT/cssid.phpUses the id="current" for navigation (classic tutorial):http://alistapart.com/articles/slidingdoors/Lots & Lots of General Help on CSS:http://www.w3.org/Style/CSS/learning Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-55838 Share on other sites More sharing options...
awesty Posted July 11, 2006 Share Posted July 11, 2006 so what is the point in using id's when you can only use them once when you could easily forget you have already used it and muck up ??? i alway use classes, but sometime use id's for no particular reason other than doing stuff differently :P Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-56106 Share on other sites More sharing options...
moberemk Posted July 11, 2006 Share Posted July 11, 2006 [QUOTE]IDs have a higher specificity rating, making them overrulling over classes. So when you give something a class and an ID, the ID's rules come first.[/QUOTE]Also, Javascript can only call ID's, unless you use the Prototype Library's dollar function. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-56138 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 [quote author=WendyLady link=topic=99592.msg394734#msg394734 date=1152565680]Hmmmm -- I'm surprised no one has said this before[/quote]Look at the post above yours... ;) Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-56246 Share on other sites More sharing options...
obsidian Posted July 11, 2006 Share Posted July 11, 2006 [quote author=ShogunWarrior link=topic=99592.msg394005#msg394005 date=1152457364]For instance to highlight a row you'd need the ID of that table row.[/quote]no necessarily. there are many other ways to reference a specific object (by name, element, etc). however, using the getElementById() function is definitely the easiest way. in fact, you could use javascript to cycle through and pull all the rows on a page of a specific style if you wanted to as well. it would take a bit more coding, but it's possible.all that to say, i think that the principles behind some of the posts above are the biggest benefits to you: keep in mind that if it's a major section of your page (header, content, navigation, footer, etc), you need to use an ID (for reasons mentioned above). But, on the other hand, if you are wanting to assign styles that may be used more than once, by all means, use a class so you can assign the attributes wherever you'd like. also, keep in mind that each element can have multiple classes, but only one ID as well. Quote Link to comment https://forums.phpfreaks.com/topic/13799-class-vs-id/#findComment-56252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.