+ 11

What is id attribute and class attribute and what is the difference between it?

21st Mar 2019, 6:57 PM
Aditya Tiwari
Aditya Tiwari - avatar
2 Réponses
+ 6
Long story short: ID is (or should be) unique and references one single HTML element. Class describes a type of an element or a structure which that element belongs to. ______________________________ ID gives an (HTML) element (tag) a specific "reference/address" One rule is that an ID should be unique (eg.: if you got a DIV element with ID "myDiv" there should be no other element (of any kind) with the same ID. Think of it as a person. Each person is unique. There might be a doubleganger (similar looking), but still it's not the same person. Hence, an element with an ID can be easily referenced with (Javascript for example) without further information. A Class is mainly used for styling with CSS. It gives the elements a structure. Class attributes follow other rules. A class attribute can contain multiple values separated by spaces (that means that the element belongs to different classes). Besides that a class doesn't need to be unique. There may be several elements with the same class.
21st Mar 2019, 7:36 PM
Pete Wright
Pete Wright - avatar
+ 4
//Id and class are selector of css which are used to select html element. The difference between an Id and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one.
21st Mar 2019, 7:23 PM
Sudarshan Rai
Sudarshan Rai - avatar