What is the difference in id and class selectors?(by example) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference in id and class selectors?(by example)

https://code.sololearn.com/WnIe7iltC8CR/?ref=app

3rd Oct 2017, 10:01 AM
(BøɍƝ ťọ̄ Ćọɗë)
(BøɍƝ ťọ̄ Ćọɗë) - avatar
9 Answers
+ 5
@Born To Code, I didn't notice you used it twice, by right there should only be one element having a distinct Id inside a HTML document, if you want to implement a CSS rule on multiple elements use class instead of Id. You'll understand this as soon as you begin JavaScript course where you'll be manipulating DOM element, to get a reference to a distinct element JavaScript uses a function named getElementById, which accepts a string indicating a unique element's identifier. The function is supposed to return one element for your JavaScript code to work with. Now imagine if you have multiple elements by the Id, it'll be confusing. Contrary, class rule can be implemented for multiple elements because JavaScript uses the function getElementsByClassName which returns multiple element references, each reference represents an element that applied the aforementioned class. Hth, cmiiw
3rd Oct 2017, 11:16 AM
Ipang
+ 5
@Born To Code, no problem mate, I help when and where I can :) Yeah, that's great, good luck with the CSS & JavaScript course too. Everyone here in SL are considered my friends, as long as they don't go naughty, it's better to have friends, and sad to have enemies. Peace be with you mate :)
3rd Oct 2017, 2:14 PM
Ipang
+ 4
CSS rules by class can be implemented on many items, while the Id is unique, it can only be implemented on one and only item in the document, you can't have more than one element having the same Id on a document. Your code already explained the concept, one element with unique Id and multiple elements with same class. Hth, cmiiw
3rd Oct 2017, 10:39 AM
Ipang
+ 2
@lpang still didn't understood! what is the meaning of id implemented only on one and only. but i used it two places so still didn't understanding answered of difference!
3rd Oct 2017, 10:55 AM
(BøɍƝ ťọ̄ Ćọɗë)
(BøɍƝ ťọ̄ Ćọɗë) - avatar
+ 2
@lpang first of all, thank you for cleared this misunderstanding. anyway i will start learning javascript after css which is i am learning now😊 friends? we can be gud friends 😉😊
3rd Oct 2017, 2:00 PM
(BøɍƝ ťọ̄ Ćọɗë)
(BøɍƝ ťọ̄ Ćọɗë) - avatar
+ 2
@lpang cool😊 ill try not to be naughty hehehe just kidding
3rd Oct 2017, 2:16 PM
(BøɍƝ ťọ̄ Ćọɗë)
(BøɍƝ ťọ̄ Ćọɗë) - avatar
+ 1
keep a note of this. Id selectors in CSS begins with a # (followed by the name). Whereas, class selectors begin with a .(followed by the name).
3rd Oct 2017, 10:42 AM
Sergio Salvatore
Sergio Salvatore - avatar
+ 1
hey!
9th Oct 2017, 1:08 AM
(BøɍƝ ťọ̄ Ćọɗë)
(BøɍƝ ťọ̄ Ćọɗë) - avatar
0
hi
8th Oct 2017, 9:43 PM
Albani Bashir
Albani Bashir - avatar