__________________ Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.
Last edited by L0adOpt1c; 01-23-2013 at 02:01 PM..
Reason: Added link
An "id" is unique and applies to one and only one element on the page. A "class" is not unique and can apply to none, every or some elements on a page.
And am I not correct in saying that you CAN use them for several elements?
It's possible. I've done it.
__________________ Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.
And am I not correct in saying that you CAN use them for several elements?
You can but I think it was a misunderstanding between you two. The way you said it could be used could be misunderstood that it can be used for several elements in the same document which is definitely wrong. It can generally be used on almost all HTML elements, that’s true, but an ID must occur only once per document.
The id selector is used to specify a style for a single, unique element.
The id selector uses the id attribute of the HTML element, and is defined with a "#".
The style rule below will be applied to the element with id="para1":
Example-
#para1
{
text-align:center;
color:red;
}
Now for Class-
The class selector is used to specify a style for a group of elements. Unlike the id selector, the class selector is most often used on several elements.
And am I not correct in saying that you CAN use them for several elements?
It's possible. I've done it.
No you are not correct. You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.
Id's are and always have been meant to be unique. Sorry, you are incorrect.
No you are not correct. You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.
Id's are and always have been meant to be unique. Sorry, you are incorrect.
Sorry, but the person before was correct. Of course you can't use one ID multiple times per page, you can use them across several pages on one website.
__________________ Validate, early and often. | Use W3Schools! |
Search for your answer before posting.
Thank the people that help you!
Please mark things as resolved.
You can give multiple elements the same ID all day long, but its not compliant to the standard and it causes issues if you are trying to target an element with javascript by id if there are mutiple elements with the same id.
It isn't just from JavaScript that specifying multiple copies of the same id breaks. It also breaks in HTML when you link to it.
If the web page were the world and the id was "Birmingham" and you needed to go there - which Birmingham would you go to?
Code:
<div id="Birmingham">England</div>
<a href="#Birmingham">go to Birmingham</a>
<div id="Birmingham">Alabama</div>
Almost all of the functionality that applies to ids relies on the fact that there will be only one.