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

Id vs class

we already know that id and class are used to call them in css, but whats the different between the 2 and when should we use them?

8th Nov 2016, 7:29 PM
Rodrigo Tallar
Rodrigo Tallar - avatar
5 Answers
+ 4
ID's are unique Each element can have only one ID Each page can have only one element with that ID Classes are NOT unique You can use the same class on multiple elements. You can use multiple classes on the same element. Sure you can have more than one element with the same ID... However, your code will not pass validation if you use the same ID on more than one element. JavaScript depends on there being only one ID per element. If you have more than one ID for an element, then your javascript will draw calls from the wrong element. If you'd like to know more visit here https://appendto.com/2016/04/css-class-vs-id-which-one-to-use/
5th Dec 2016, 11:44 PM
Chris Coder
Chris Coder - avatar
+ 2
No...one element has its unique id. For an example: <style> #main{ background: #000; border: 1px solid #AAAAAA; padding: 10px; color: #fff; width: 100px; } #main #main { background: red; border: 2px solid #AAAAAA; padding: 20px; color: blue; width: 25%; } </style> <div id="main"> <p> HTML Learning </p> </div> <div id="main"> <p> CSS learning </p> </div> If you try this, you will get that first id's values are overriding with the second one.
8th Nov 2016, 7:49 PM
Rajib Kumar Bhowmik
Rajib Kumar Bhowmik - avatar
+ 1
id (#) is used for single elements styling and class (.) can be used for multiple elements. ID is unique.
8th Nov 2016, 7:36 PM
Rajib Kumar Bhowmik
Rajib Kumar Bhowmik - avatar
+ 1
what does that mean? i cant have 2 elements with the same id?
8th Nov 2016, 7:37 PM
Rodrigo Tallar
Rodrigo Tallar - avatar
- 3
can I have your facebook address.....???
12th Nov 2016, 5:18 PM
Rakibul Islam Sifat
Rakibul Islam Sifat - avatar