tags:

views:

700

answers:

7

In using CSS is it best practice to use a div id only once per page. I know each id has to be unique but could be used multiple times. A discussion with my development team and it came up that one person was under the impression that you could only use the div id tag once per page.

Example of 1 id per page:

<page>
  <div id="test">Some Text</div>
  <div class="test12">More Text</div>
</page>

Example of multiple id's per page:

<page>
  <div id="test">Some Text</div>
  <div id="test12">More Text</div>
</page>

I hope that's clear enough. If not let me know and I can try to explain it better.

A: 

id should be unique

see HTML spec

Zack
What exactly was the problem with my answer???
Zack
Dunno... +1 to counteract
Greg
+3  A: 

You can certainly use the id="" attribute as many times as you need, but the contents of the attribute should be unique. Not having a unique value is a HTML error.

If you need multiple items to have the same attribute, then you can set them as a class.

More info is at the W3C - Element identifiers: the id and class attributes (the HTML 4.01 Specification).

Alister Bulman
+1 but edited tag -> attribute
Greg
+1  A: 

It is physically possible to have duplicate id's per page, but the reason you only want to use one id per page with CSS is because of CSS selectors. Doing a CSS select by id is expected to only return a single DOM item.

CLaRGe
A: 

ID's are Unique Assigments for ONE element on A page.

You can use the "ID=" attribute more than once. You will need to make sure that ID="hi" is only used once as it is supposed to be a unique identifier at the element level.

Link to Supporting Documentation: http://www.w3schools.com/tags/tag_DIV.asp

Jeremiah
A: 

You can have many elements with id(s) if that's what you're asking. Just view the source code of this page for instance and you'll see many id(s).

smack0007
+2  A: 

First, tags usually refer to elements such as div, a, img, body, ... Attributes are values given inside the tags such as id, class, href, ...

The id attribute can be specified once for each tag, with the constrain that no two tags share the same id value.

Both your examples are valid.

Ben S