+ 2
[DUPLICATE] Is there any difference between div id and class?
I want to know that there is any difference between id and class.
5 Answers
+ 9
Unlike the id selector, the class selector is most often used on several elements. This allows you to set a particular style for many HTML elements with the same class. The class selector uses the HTML class attribute, and is defined with a "." id is used when we have to apply CSS property to one attribute only.
Example
<div id="header_id" class="header_class">Text</div>
#header_id {font-color:#fff}
.header_class {font-color:#000}
(Note that CSS uses the prefix # for IDs and . for Classes.)
However color was an HTML 4.01 <font> tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style is color so the above should read:
Example
<div id="header_id" class="header_class">Text</div>
#header_id {color:#fff}
.header_class {color:#000}
The text would be white.
+ 8
Having an selector involving id would overwrite all the existing style that the element has except for the inline styles...(Due to something called specificity) So you can use id to give special style for an element and class for generic style :)
+ 6
elements cannot have multiple Id and as well as it is unique to the elements..
and whereas classes can be more than one for an element and it can be same and used with different elements
We usually use Id if we want to style one element with unique styling and, for classes, one can use it where you want a common style to more than one element.
+ 5
Note id is also important when using the DOM in js.