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

CSS CSS Basics (2014) Basic Selectors Class Selectors

Why use ID instead of Class?

Looking at the parameters for both these selectors, I can't see the benefit of using ID when Class can be used to apply the same changes to every instance in the document. Is there a marked reason to use ID instead?

5 Answers

Steven Parker
Steven Parker
231,248 Points

ID is a better choice when there is only one item to be targeted, since every element ID on the document must be unique.

Fran ADP
Fran ADP
6,304 Points

It is a better choice.

Hi there, ID is a better choice when unique element to be targeted.

What's wrong with using only one element that has a class that is only used once? Why not just stick to just class selectors?

Steven Parker
Steven Parker
231,248 Points

Technically, a class will do the job just fine; but using an ID makes it clear at first glance at the CSS that the rule targets one unique element.

Hi there, an id can be used to identify one element, whereas class can be used to identify more than one. id is unique. So, id is a better choice when unique element to be targeted.