Forum Moderators: not2easy

Message Too Old, No Replies

Can I use ID Twice or More?

will I get any problem if I use same id in 2 or more elements?

         

mms19

11:01 am on Sep 29, 2003 (gmt 0)

10+ Year Member



I got 2 tables with different classes,

<table class="1st"> ....
<table class="2nd"> ....

Now I would like to assign another layout for these 2 tables and I try to set

<table class="1st" id="3rd"> ....
<table class="2nd" id="3rd"> ....

My IE6 seems have no problem to display these 2 tables.

Does this mean I can use the same id in 2 elements?

Thanks.

Nick_W

11:04 am on Sep 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried validating your html [validator.w3.org] to see if it's the right way to do it?

Always the first port of call. After that, if you still having trouble, let's talk ;)

Nick

BlobFisk

11:13 am on Sep 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



2 Quick Pointers:

Classes and ID's cannot begin with a numeral.
You can only use and ID once on a page.

DrDoc

3:42 pm on Sep 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...hence why it's called 'ID'.
How would you like it if some other person was using the same ID# (or SSN# or whatever) you're using? :)

TGecho

4:09 pm on Sep 29, 2003 (gmt 0)

10+ Year Member



Looks like a good time to use multiple classes
<table class="first third"> ....
<table class="second third"> ....

MonkeeSage

8:07 pm on Sep 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cf. The ID Attribute [w3.org]

Jordan

frank_stahl

11:15 am on Oct 1, 2003 (gmt 0)

10+ Year Member



Hi mms19,

well, why do you want to do this in the first place?

Normally, you use an id attribute for elements appearing only once on your page, e.g.:
<div id="navigation">...</div>

If you want to assign a style to more than one element, you should use a class, e.g.
.myclass {...}
.basic {...}

However, you can assign more than one class to an element like this:
<div class="myclass basic">...</div>

This means you could define for instance all you basic table attributes in the "basic" class and more special attributes in additional classes.

I hope this helps.

PCInk

2:55 pm on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It may work in IE6, but IE6 can be wrong. Other browsers may ignore the second definition and some could crash if they want to - more than one id per page is undefined, it's up to the browser to handle it how it wants: it can apply it, it can reject it, it can screw your page up, or it can crash. IE6 chooses to apply it, but this does not mean other browsers will do the same. As stated earlier, use classes. You have more than one class per element, so why you want to use id makes me puzzled?

pageoneresults

3:00 pm on Oct 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now I would like to assign another layout for these 2 tables and I try to set <table class="1st" id="3rd">

One of the best tips I learned here from the CSS forum is that you can assign multiple classes to a single element (as TGecho and frank_stahl mention above). So, if you need to apply additional styling to those tables, you might do something like this...

<table class="ab cd ef gh">

Note: That is four different classes being applied to that one table.

After seeing that tip posted here at the board, I went on a mission to make changes on a multitude of sites where I was able to trim out some CSS bloat that had accumulated over the years.

As mentioned above, IDs can only be referenced once on a page. The validator will tell you the same.