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 trialRex Irvin Carpen
3,314 PointsWhy use class rather than id, when naming a selector?
I usually use id when naming a selector. I wanna know is there difference when using class instead of ID?
2 Answers
Raja Kannan
6,590 PointsHi,
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.
Ari Misha
19,323 PointsHiya there! The convention is you can only use id once per element whereas you can class multiple times for multiple selectors or for/within the same selector as well. I mostly use id for my main container divs and maybe few inputs. But i use classes for the selectors that might repeat itself on the same page or in the same container div.
~ Ari
Rex Irvin Carpen
3,314 Pointstnx Ari so class is more efficient in repeated the same selectors.
Ari Misha
19,323 PointsBoth are efficient ad you can implement 'em in your own way or convention. Use class for selectors which will repeat itself a lot. Moreover, you'll end up using classes more than ids anyway.
Rex Irvin Carpen
3,314 PointsRex Irvin Carpen
3,314 PointsHello Raja . Thanks for the answer, follow up question. Can you use ID and class at the same time?