What is the difference between id and class on CSS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between id and class on CSS?

I know that the major difference between id and class is that id can be used once per page but classes can be used as many times on a page as needed. Please clearify this statement and this question. 😊

29th Jun 2017, 2:01 AM
Dinesh Bala
Dinesh Bala - avatar
3 Answers
+ 4
You mistly have the differences stated in your question. If I want a .red class that simply assigns the color #FF0000 to whatever it's associated with, then that's fine; however, it's not good practice to do that with IDs. Think of a class as more general, while the ID is more specific.
29th Jun 2017, 2:04 AM
Keto Z
Keto Z - avatar
29th Jun 2017, 5:22 AM
Mario L.
Mario L. - avatar
+ 1
Got it. Thanks @Mario L. An id is unique. It refers to one element. It may occure only once in the document. Classes define several related elements. If a class is addressed (e.g. by CSS or JavaScript) all elements belonging to that class are affected.
30th Jun 2017, 5:24 AM
Dinesh Bala
Dinesh Bala - avatar