Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Brad WardFront End Web Development Techdegree Student 351 Points
IDs over Classes
I just wanted to find out why you would ever use an ID over a class. In everything I've read and learned, I've basically been told to never use an ID, just use a class. Any insight on the differences, pros, and cons would be appreciated!
Jennifer NordellTreehouse Teacher
I'm no expert, but I'd say you'd use it for something that would always be unique. Let's say for instance that you have a site that sells a multitude of items. But only one item is ever going to be the "top seller" for that week. Now, it may be the same item 2 weeks in a row or more. But only one is ever going to be number 1. In that case, it might be nice to implement an ID to make sure that you don't accidentally mark two as the top seller. This, however, is the only concrete example I've thought up so far. Hope this helps!
Luke Towers15,328 Points
In terms of building HTML just to style it with CSS there really isn't a super strong reason to use an ID over a class, except for it to be a visual reminder to yourself that this particular element is unique as you're developing your CSS. It comes in handy though when you have to use an ID on an element for other purpose and you wish to target that element with CSS.