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.

HTML

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,109 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,008 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.