Id vs class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Id vs class

In html what is difference between Id and class?

14th Aug 2018, 8:12 AM
Satyendra Singh
Satyendra Singh - avatar
3 Answers
+ 1
You can assign class to more than one element (tag) e.g. <p class="class-name"> My 1st paragraph </p> <p class="class-name"> My 2nd paragraph </p> <p class="class-name"> My 3rd paragraph </p> You should use id only for one element (tag) ONLY e.g. <p id="id-name"> My 1st paragraph </p>
14th Aug 2018, 8:19 AM
MarJan
MarJan - avatar
+ 1
Check here - https://stackoverflow.com/questions/12889362/difference-between-id-and-class-in-css-and-when-to-use-it Definition from wiki - 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.
14th Aug 2018, 8:29 AM
MarJan
MarJan - avatar
+ 1
ids are also more specific than classes, which is an additional side effect to be aware of when choosing between them. https://css-tricks.com/specifics-on-css-specificity/ The general idea is to keep the specificity low so you have multiple options/methods for trumping styles when needed.
14th Aug 2018, 8:40 AM
Janningā­
Janningā­ - avatar