00 Votes

HTML: ID and CLASS - What is the difference?

Question by Guest | 2018-04-27 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?

ReplyPositiveNegative
0Best Answer0 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.
2018-04-27 at 23:02

ReplyPositive Negative
Reply

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.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.