Robert
Robert

Reputation: 3074

css div id used only once per page

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.

Upvotes: 3

Views: 14198

Answers (7)

Uttam
Uttam

Reputation: 1

If it is related to the CSS only then we can also use Class instead of Id for DIV tag

Upvotes: 0

Alister Bulman
Alister Bulman

Reputation: 35169

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).

Upvotes: 6

Ben S
Ben S

Reputation: 69382

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.

Upvotes: 4

smack0007
smack0007

Reputation: 11356

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).

Upvotes: 0

Jeremiah
Jeremiah

Reputation: 5514

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

Upvotes: 0

CLaRGe
CLaRGe

Reputation: 1831

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.

Upvotes: 1

Zack Marrapese
Zack Marrapese

Reputation: 12090

id should be unique

see HTML spec

Upvotes: 1

Related Questions