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


Using IDs instead of Classes

In this video, he uses IDs instead of Classes. In every other html/css video I've watched on Treehouse, classes are used instead of IDs for the same kinds of things he did.

I'm just wondering why he's using IDs instead of classes. I have been under the impression you should save using IDs for JavaScript based functionality, and used classes for styling purposes.

3 Answers

Kevin Korte
Kevin Korte
28,148 Points

You're assumptions are correct, not sure why IDs were used. It's best to use classes for styling and ids to target unique dom elements.

jason chan
jason chan
31,009 Points

IDs are easier selectors to use. Use a Class if you want to use it elsewhere, most css frameworks are written classes. If not creating a css rule selector would be way longer. Just think ID is unique selector

Example below: https://developer.mozilla.org/en-US/docs/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

IDs as selectors is a 'violation' of CSS Lint and general best practices. However, there are exceptions to every rule, and these are guidelines, not laws.

So, when in doubt, go with a class. However, if you truly 100% know that your element is 'one-of-a-kind' and will never be used on another page in your site, cut through the chatter and just use IDs.

Usually on our projects, if we 'break' a rule (e.g. linting), it's okay as long as we put a 'designer's note' comment stating why we are doing so.

That way, it is not construed as ignorance or laziness, but is deliberate.