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

General Discussion

ID'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

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.

http://css-tricks.com/the-difference-between-id-and-class/

Thanks Bryan H, I understand the difference, but I was wondering why some would choose to use classes where ID's are more appropriate.

For 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.

I only use IDs to wrap content, for example, a header, or footer. Easier to manipulate in CSS

I think it really just comes down to a choice of programming style, for each person. Would be my best guess.

While 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