Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

CSS Forum


You are currently viewing our CSS Forum as a guest. Please register to participate.
Login



Reply
Old 05-28-2007, 04:25 PM class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
I know that an id must be unique and a class is reusable. But what is the difference other than that? Why classify something as an id when you could just have everything be a class and reuse it if need be? I know for alot of things the situation would never come up, it doesnt make sense to have two "title"s or some such thing. Is this mostly just an organization issue for the designer?
zm0nky is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-28-2007, 04:39 PM Re: class vs. id
highanddry's Avatar
Close Talker

Posts: 880
Name: Jacob
Trades: 0
As you probably know, ID's are used once for non-reoccurring items such as the menu bar, nav bar content box etc. One reason for the distinction is for organization I suppose but a better reason is that DIVs have higher priority and take precedence over Class properties. IDs have higher specificity than classes. This means that, in situations where a class's properties might not be applied (for reasons of specificity and inheritance), an ID's properties probably will be
highanddry is offline
Reply With Quote
View Public Profile
 
Old 05-28-2007, 08:47 PM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
Awesome, thanks for the reply. I think I understand inheritance for the most part any chance you could give me an explanation or point me in the direction of something that could give an explanation further on all this inheritance and specificity stuff?
zm0nky is offline
Reply With Quote
View Public Profile
 
Old 05-28-2007, 08:53 PM Re: class vs. id
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
It's for client scripting. If you need to use js to grab and manipulate an element on the page, you use document.getElementById to do it. From that point you might change its class, or do any number of things.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 05-28-2007, 09:10 PM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
Quote:
Originally Posted by ForrestCroce View Post
It's for client scripting. If you need to use js to grab and manipulate an element on the page, you use document.getElementById to do it. From that point you might change its class, or do any number of things.
So then none of it is ever taken into consideration unless JS is being used? or can JS not affect classes but it can affect IDs? To be honest im not really sure what part of my question you are answering here
zm0nky is offline
Reply With Quote
View Public Profile
 
Old 05-28-2007, 09:21 PM Re: class vs. id
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
javascript can affect classes as well.

an ID would be used to apply styling to an InDividual item, where as a class can apply to a single element or several.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-28-2007, 09:33 PM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
I knew all that, what I a trying to get the answer too now is what highanddry said about inheritance and specificity. What are the 'situations where a class's properties might not be applied (for reasons of specificity and inheritance)' be?
zm0nky is offline
Reply With Quote
View Public Profile
 
Old 05-28-2007, 09:45 PM Re: class vs. id
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
Yeah, but it's much more difficult to grab an element by its class using the DOM. You can use a foreach loop and some if logic to pick the right one, but something like <span onClick="document.getElementById('xxx').style.disp lay = 'none'"> is probably the main reason to use IDs ( in elements other than the span in that example ).

Z, do you use FireFox? If not, you should give it a try, and install FireBug. It will show you the inheritance path for any element on a page. But here's an example - what color is the link?

<style>
a { color: red; }
.test { color: blue; }
#lnk { color: green; }
</style>

<a href="url.com" id="lnk" class="test">anchor text</a>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 05-29-2007, 01:19 AM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
Ok I think I got it for now. Thank you for the answers and the suggestion to use FireBug I was only using WebDev at the moment. Talk added to those that helped, I appreciate it.
zm0nky is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 01:33 AM Re: class vs. id
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
It's green, by the way, showing how ID takes precedence in the inheritance path ... but FireBug actually shows this, instead of talking about it. Click the attachment for a screen-shot of the cascading part of CSS in action the way FireBug shows it.
Attached Images
File Type: gif FireBug.gif (6.3 KB, 3 views)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 05-29-2007, 01:15 PM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
ok, say we are not using an ID and assign something a class. Is there anything that takes priority away from the class?
Sorry I forgot to put the green in the last reply. I took the example and ran it through the browser to make sure, after I guessed of course.
zm0nky is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 04:25 PM Re: class vs. id
LadynRed's Avatar
Defies a Status

Posts: 10,016
Location: Tennessee
Trades: 0
Here's some info on Class vs. ID's from css-discuss.org wiki

http://css-discuss.incutio.com/?page=ClassesVsIds
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 05-29-2007, 04:38 PM Re: class vs. id
Average Talker

Posts: 15
Name: z
Trades: 0
Awesome, thank you for the article!
zm0nky is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to class vs. id
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58548 seconds with 13 queries