dhermanus Posted September 22, 2006 Share Posted September 22, 2006 Hi I would like to ask. Whats the different between ID and class?Here's a quote I've got from the web "The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one."Is this true? Because I tried using ID on more than 1 element... and it works... on TWO elements.eg. <html><head><style type="text/css">#top { background-color:red} </style></head><body><div > <h1 id="top">Chocolate curry</h1> <p id="top">This is my recipe for making curry purely with chocolate</p> <p>Mmm mm mmmmm</p> </div></body></html>Thanks,Naruto Link to comment Share on other sites More sharing options...
aspnetguy Posted September 22, 2006 Share Posted September 22, 2006 an ID must be unique so it can only be used once while classes can be applied many times.That is the main differences. ID is used in other things besides CSS, like JavaScript to access different elements of the ID. Link to comment Share on other sites More sharing options...
jesh Posted September 22, 2006 Share Posted September 22, 2006 To further aspnetguy's answer, many browsers will allow you to use the same ID for HTML elements but it's really bad practice. Link to comment Share on other sites More sharing options...
aspnetguy Posted September 22, 2006 Share Posted September 22, 2006 To further aspnetguy's answer, many browsers will allow you to use the same ID for HTML elements but it's really bad practice.Thanks, if it is bad practice I usually don't bother mentioning that they can do it...no need to learn this the wrong way Link to comment Share on other sites More sharing options...
jesh Posted September 22, 2006 Share Posted September 22, 2006 Thanks, if it is bad practice I usually don't bother mentioning that they can do it...no need to learn this the wrong way Good advice, I'll follow it. Link to comment Share on other sites More sharing options...
iammatto Posted September 22, 2006 Share Posted September 22, 2006 Just a bit more...According to the W3C, an id must be unique and is used to identify an html element, while the class assigns the class name to the element, and multiple elements can share the class name. Honestly, the id shouldn't be thought of as a way to apply a style, but more to identify an element, which in turn, css can specify a style to be applied to...it's a subtle difference, which I may have made even more confusing with my wording, but to me seems to be an important differenc. The link below should help a bit morehttp://www.w3.org/TR/html4/struct/global.html#h-7.5.2 Link to comment Share on other sites More sharing options...
dhermanus Posted September 23, 2006 Author Share Posted September 23, 2006 Thanks again... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now