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

HTML How to Make a Website Debugging HTML and CSS Problems How to Fix Problems with Code

Class and ID

How do you know when to use class as opposed to id and vic versa?

3 Answers

Generally, you should use a class when you are referring to an item type, where as ids should only be used when referring to a single and specific entity.

A class can be referred to in your html several times, where as an id can only be used once, so you should use classes for items that you know you're going to use a lot. An example would be if you wanted to give all the paragraphs on your webpage the same styling, you would use classes. If you then decided that, out of all these paragraphs, you want to give the very first paragraph a special style, you would use an id on just the first paragraph.

So id for one (or a few), and class for many.

IDs should only be used once. Classes can be used many times :)

Gard Mikael Fjeldavli
Gard Mikael Fjeldavli
19,416 Points

That's really up to you. BUT; an ID can only be assigned to one element, while you can give many elements the same class. So if you are designing your website and want to give (for example) all your submit buttons the same look, you might give them all the same class, so the same styles will be applied to all your buttons. But if you want to target only one of them, you might give it an ID, and the styles you bind to this ID will only apply to that ID. =)