Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using same ID for multiple HTML tags? [duplicate]

Possible Duplicate:
Several elements with the same ID responding to one CSS ID selector

Below is the example code that I was testing and I got confused. Every one says that we can use or we should use only one time per id, but I have testes using using it multiple times but its giving me the correct output.

What should I do?

It's kinda working same like class for me in this example

code:

<html>
<head>
<style>

#exampleID1 { background-color: blue; } 
#exampleID2 { text-transform: uppercase; } 

</style>
</head>
<body>
<p id="exampleID1">This paragraph has an ID name of "exampleID1" and has a blue CSS defined background.</p>
<p id="exampleID2">This paragraph has an ID name of "exampleID2" and has had its text transformed to uppercase letters.</p>

<address id="exampleID1">

Written by W3Schools.com<br />
<a href="mailto:[email protected]">Email us</a><br />
Address: Box 564, Disneyland<br />
Phone: +12 34 56 78
</address>


<blockquote id="exampleID1">
Here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation here is a long quotation.
</blockquote>

</body>
</html>

Please see the above code and answer me that why we should not use id selector two times in a page while its working fine.

like image 727
gaurav rathor Avatar asked Jun 18 '12 09:06

gaurav rathor


1 Answers

An id must be unique in a page. Use a class if you want to describe a group of elements.

why we should not use id selector two times in a page while its working fine.

You are making an error and depending on every browser that will ever view the page to compensate for it. You can't be sure they all will.

like image 76
Quentin Avatar answered Oct 01 '22 08:10

Quentin