0 0 Votes

HTML: ID and CLASS - What is the difference?

Question by Guest | 27/04/2018 at 00:19

Elements within an HTML document can have both an ID and a class. So, for example, <h1 id="idname"> or <h1 class="classname">.

Now I'm wondering what the difference between ID and CLASS should actually be. As far as I know, the point is to get a reference for your CSS. In my example, you can assign certain properties with #idname or .classname. However, it makes no difference to me to write #idname {width: 100%} or #classname {width: 100%}. The browser also accepts both spellings.

Why do you need IDs and CLASSes at all? Would not one label suffice as a name reference for an object?


Stefan Trost

Show Profile | Message
0Best Answer
0 Votes

There is a clear difference between ID and CLASS: an ID is unique, but a CLASS is not.

This means that each HTML element may have only one unique ID and also within one HTML document only a single element with the same ID may occur. With a CLASS, however, this is different: Each element may have any number of classes and a class may also be used multiple times for different elements within one document.

The fact that browsers also accept multiple IDs within one document and are nevertheless applying the CSS correctly is because browsers are very tolerant of errors in the HTML source code. Since a large proportion of HTML pages on the Internet do not consist of valid HTML, the browser manufacturer has no choice but to adapt to it.
27/04/2018 at 23:02

ReplyPositive Negative

Related Topics

PHP: Sending an E-Mail
Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own articles on askingbox.com. How to do.