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 How to Make a Website Adding Pages to a Website Build the Contact Page

Aaron Machado
Aaron Machado
598 Points

Conventions for the use of classes vs id's

Hi, I'm wondering about the conventions for the use of classes vs id's. For example in this video a class is used for the unordered list "Contact-Info" nevertheless I don't see any other possible use of this class in our webpage other than specifically "contact-info". So if this style is going to be used just one time in one particular specific element shouldn't we use an id instead of a class?

Aaron Machado
Aaron Machado
598 Points

Thanks everybody for your answers.

3 Answers

Philip Cox
Philip Cox
14,818 Points

Hey Aaron. A good reason to get into the habit of using classes & id's 'correctly' is, in the future once you start adding functionality to your projects with javascript or php/ruby, it is a good idea to use your id's for functionality and your classes for styling. This way you also future proof your code for writing clean and succinct CSS, applying similar styles. It's easy to add a class to an element to add some style, but of course you can only have an id name once in a document. Hope this helps. :)

Avery Kachline
Avery Kachline
10,458 Points

Aaron Machado, You make a good point. Id selector is used in CSS for a specific element; a class selector is used for multiple elements in a class. Both are used for styling, but like Philip Cox said, if you are incorporating functionality with JavaScript, PHP, or ruby you might want to focus you Id tags more on website functionality even more than simply style.

Kevin Korte
Kevin Korte
28,109 Points

CSS specificity becomes another issues. A single ID will have more specificity than numerous classes. This can cause issues. Classes in general are more extendable as well. You may not ever see the need to use a class on another element, but as a freebee you could later. You can also be more modular with your classes, have multiple, reusable classes make up and element. I don't use IDs that much, unless I want to be 100% sure I target that one and only element. And example as mentioned might be with jQuery, or maybe a form selection.