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 trialAaron Machado
598 PointsConventions 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?
3 Answers
Philip Cox
14,818 PointsHey 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
10,458 PointsAaron 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
28,149 PointsCSS 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.
Aaron Machado
598 PointsAaron Machado
598 PointsThanks everybody for your answers.