Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMIAN OSAMA ARSHAD
Courses Plus Student 75 Pointswhat is the main difference between id and class css selector??
what is the main difference between id and class css selector??
4 Answers
Evgeniia Mas
4,452 PointsHello! Id is unique and you can use only one with that name on your page. Class you can assign to different elements and have two and more elements with same class on your page; id is also more specific when you style elements because of it's priority. So if you use same styles (font-size, for example) to the same element with id (20px) and class (10px) the font-size of id will be used.
If you validate your html code this mistake will be shown!!!! It works, but it is not correct! And it is obvious when you use javascript or jquery. You won't be able to select elements and make with them something if two or more elements which have same Ids.
Steve Hunter
57,712 PointsHi there,
An id
is unique and can only be used once on a page. A class defines a style that can be re-used however many times you want.
I hope that helps,
Steve.
MIAN OSAMA ARSHAD
Courses Plus Student 75 Pointsbut why html and css did not restricted me when i apply same id to multiple elements.?
Steve Hunter
57,712 PointsNot sure - won't selecting it only apply the style to the first occurrence of the id
? I didn't think the style would be applied to multiple id
's of the same name, just the first?
Jonathan Rhodes
8,086 PointsAn id is unique and can only be used once, where as a class can be used multiple times and is used to denote a category of elements. Ids are used when you want to select only one specific element, while classes can select all items given that class name.
MIAN OSAMA ARSHAD
Courses Plus Student 75 Pointsgreat answer evgenlia mas..
Evgeniia Mas
4,452 PointsThanks for your feedback!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI'll leave this with Evgeniia as HTML & CSS are not my strong points. This answer covers your question well, I think.
Steve.
Raihan Khan
2,168 PointsRaihan Khan
2,168 PointsWhich one is better to user ID or Class. Why should I use ID over Class
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNeither is better but they are used for different things. Depending on what you are trying to achieve, one may have greater benefit than the other but that's case-specific and not a generally-applicable rule. I think a class has the benefit of being capable of being applied in more than one place, so you can select it in multiple places; an
id
does not have this capability. Sometimes, though, that perceived restriction is a benefit.Steve.