Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W3C errors for duplicate id's

I am using http://validator.w3.org to validate my code.

However I have 45 errors, largely related to duplicate id's.

I thought where possible it would be a good pratctice to re use CSS.

Example code:

CSS

#cloudbullet {
background-color: #85c0bf;
height: 22px;
left: 13px;
margin-top: 7px;
position: relative;
top: 63px;
width: 20px;

HTML

<div id="container1-title" class="">
        <h2>Cloud/Hosting</h2>
        </div>

        <div id="cloudbullet" class="">
        </div>
        <div id="cloudbullet" class="">
        </div>
        <div id="cloudbullet" class="">
        </div>
        <div id="cloudbullet" class="">
        </div>
        <div id="cloudbullet" class="">
        </div>
        <div id="cloudbullet" class="">
        </div>

The effect being that I get 6 css boxes in a vertical row.

Please can someone advise if what I have done is correct and if I should ignore the validation or does this need to be done in a different way?

Thanks,

like image 692
user3580480 Avatar asked Nov 30 '25 03:11

user3580480


2 Answers

For something that appears several times, you want something that classifies them, not identifies them. An id should be unique in the page.

Even though some things work with duplicate identifiers, it's not certain that it works the same in all browsers, and some things certainly won't work. If for example you want to use the identifier to find the elements using JavaScript, you will run into problems.

Use the class attribute instead. CSS:

.cloudbullet {
  background-color: #85c0bf;
  height: 22px;
  left: 13px;
  margin-top: 7px;
  position: relative;
  top: 63px;
  width: 20px;
}

HTML:

<div id="container1-title">
  <h2>Cloud/Hosting</h2>
</div>

<div class="cloudbullet">
</div>
<div class="cloudbullet">
</div>
<div class="cloudbullet">
</div>
<div class="cloudbullet">
</div>
<div class="cloudbullet">
</div>
<div class="cloudbullet">
</div>
like image 191
Guffa Avatar answered Dec 03 '25 13:12

Guffa


An id explicitly is unique. Only a single element in the document must have the id. That's why it's called an id, it's a unique identifier.

If you want to apply the same rule to multiple elements, that's what CSS classes are for.

like image 28
deceze Avatar answered Dec 03 '25 15:12

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!