1

Okay I know my question is very very basic and stupid. But I really wanted to know the reason behind it.

I make websites, I follow all the w3 rules. My HTML is valid. But this question hit me this morning.

In HTML we have an ID and a CLASS. Both serve the same functions. But a an ID is used only once and a CLASS is used for a group of elements.

For example:

<div id="menu">
    <div class="menu-item"></div>
    <div class="menu-item"></div>
    <div class="menu-item"></div>
</div>

Can also be written as

<div id="menu">
    <div id="menu-item"></div>
    <div id="menu-item"></div>
    <div id="menu-item"></div>
</div>

Both the above codes would produce the same result, right? I have made whole websites when I first started making websites and was not sure about this rule.

All of those sites were sold out, and none of them were following this rule. One of them being http://www.mmun.org.pk/

So yeah dont thumbs down on this question, I know I could google this too but I wont be able to discuss it with anyone on google.

Thanks :)

Uzair Hayat
  • 518
  • 1
  • 8
  • 21

5 Answers5

2

Lets have example.

Like human is class and we all are its child but we still have unique identification but belongs to same class.

Same as DOM they can belongs to same class but ID must be unique.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
1

IDs should be unique and cannot be used more than one time, where you can use classes for multiple times.

IDs are more specific than classes.

ID's are generally used to target elements using JavaScript.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

nope. never. id and class is different. at javascript, You can use "document.getElementById()" with id. at css, you can define it diffrently with id and class. because Id is unique but class can be everywhere.

1

If you are talking about plain HTML, then you technically the same ID multiple times, but in my experience this causes problems if you ever use the ID as a selector. It is also important to note that in XHTML, duplicate IDs are not allowed, since XML strictly disallows this.

The reason that browsers don't throw an error for this is that they are coded to be somewhat tolerant of bad HTML. Sad, but true.

A little more explanation: browsers offer methods to manipulate the elements in HTML (DOM methods such as document.getElementById()). getElementById will return one and only one element, so it would be confounding to give the same ID to two elements, as you'll only ever be able to access one of them by that ID.

kahowell
  • 27,286
  • 1
  • 14
  • 9
1

These are fundamentally different concepts. Someone will say it better than I can, but...

A "Class" describes certain properties of entities - things they have in common. You cannot be uniquely identified by your class, although I might know a lot about you if I knew your class. If you are "a king", I figure you live in a palace, wear a crown, have servants, ... but I don't know YOU. If you are Willem Alexander, date of birth April 27, 1967, I know exactly who you are.

A "class" is like "king". An "id" is like "Willem Alexander". There can be more than one king; but a unique identifier is unique. You describe properties and methods with a class - but you don't identify an INSTANCE.

I hope this clears things up a bit.

Floris
  • 45,857
  • 6
  • 70
  • 122