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 trial
Colby Work
20,422 PointsID's or Classes?
Peace, fam!
I see a lot of people using classes over ID's, and have wondered for a long time, what the best practice is for choosing one over the other? I know an ID is for an element that appears once on a page, so why would people do things like:
h1 class="site-title" (when site title only is on each page once)
or even regions like class="main", when main is a section that only appears one time. Is it preference? Because to me, it doesn't make sense to ONLY use classes, especially knowing that classes are for elements that appear more than once.
I wish I had more specific examples, but this is just something I've noticed for a while now and haven't found a decent explanation yet.
5 Answers
notf0und
11,940 PointsID'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.
Colby Work
20,422 PointsThanks Bryan H, I understand the difference, but I was wondering why some would choose to use classes where ID's are more appropriate.
notf0und
11,940 PointsFor the possibility that they may use the class again, and don't want to use an ID which they may have to go back and change . So essentially: personal preference based on the limitations of IDs.
Danny Carrillo
819 PointsI only use IDs to wrap content, for example, a header, or footer. Easier to manipulate in CSS
Don Brubaker
2,518 PointsI think it really just comes down to a choice of programming style, for each person. Would be my best guess.
Adam Fichman
9,502 PointsWhile I'm not saying I essentially agree with everything in the article, here is a great article on the thought process behind using classes over ideas from css wizardy