Why is there such a thing as id when you can just have classes with only one element?

From a language perspective, what's the point of creating an id attribute for HTML if you can only have a class with one element? Why not just use classes for everything and complicate the markup?

I can think of three possible explanations, but they don't completely satisfy me, so I wondered if you know why the ID was included in the HTML. My thoughts:

  • Having an ID helps you create CSS styles because its greater specificity allows you to give an ID to one class member that overrides the styles given to other members of that class. This explanation does not completely satisfy me, because you can simply give it an additional class and place the styles for that class at the bottom of the stylesheet, under the section for styles set for individual elements.
  • When selecting elements with jQuery, DOM traversal may stop as soon as an element with that ID is found. Thus, having an id will make the selection faster. This explanation does not satisfy me because I am pretty sure jQuery was created long before IDs and classes already existed.
  • Having an identifier as a language function can help ensure that styles (and selectors) that are supposed to be unique only apply to one element, because everything happens when they are not. This explanation does not satisfy me, because if your site breaks when you accidentally create two elements with the same ID, it doesn't seem to be a particularly effective way of informing you that something is wrong.
+3


source to share


5 answers


The first publicly available description of HTML was a document called HTML Tags, first mentioned on the Internet by Berners-Lee in late 1991 .

There is a description of the anchor tag:



<A NAME=xxx HREF=XXX> ... </A>

HREF
...This allows for the form HREF=#identifier to
refer to another anchor in the same document.
NAME
The attribute NAME allows the anchor to be the destination of a link.

      

I believe the NAME attribute is the predecessor to the element ID: it allows you to directly link to the desired part of the hypertext page (even if it's the same page).

+4


source


IDs are unique values , so when you parse html with something like javascript, you can be sure which element your script will hit.



+3


source


For Javascript, it is in any case getElementById

several times faster thangetElementsByClassName

Test Ops / sec
getElementById 269,235
getElementsByClassName 86,369

ref

Additional information from the specification

What makes attributes of type ID special is that the absence of two such attributes have the same meaning in the corresponding document, regardless of the type of elements that carry them; regardless of the document language, the entered attribute identifier can be used to uniquely identify its element.

Thus, it is a way to uniquely identify an element, where the class selector could only do so by coincidence.

ref

+2


source


There are many reasons, most of which are not even related to CSS. For example, ajax and JS libraries often require unique IDs, and IDs can act as anchors with URL hashes.

+1


source


XML is derived from HTML, but is used to get more general data. In XML it is very often desirable to have a unique identifier (this is also often required in the database). Since XML puts a lot of effort into automated verifiability, the best approach was to simply add the id attribute as a language element. Thus, the XML verifier may throw an error if the same value is assigned to two id attributes.

Later, many XML functions returned to HTML, and I think id is just one of them. It's not strictly necessary, but it's a good thing to have in conjunction with Java Script.

0


source







All Articles