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 ID Selectors

whats the difference between class and id?

whats the difference between class and id?

3 Answers

Max Weir
Max Weir
14,963 Points

I plain terms in general with css, an ID has more specificity over a class.

e.g <div id="foo" class="bar">, styles specific to #foo will override .bar:

#foo { color: red; }
.bar { color: green; }

In general, it's best practice not to apply any styles to an ID and leave this for such things as javascript. Also, this can cause issues later on as ID's in some cases may need to be overridden using !important which can then bloat your css.

Gari Merrifield
Gari Merrifield
9,597 Points

As the names imply, an ID is for a specific item, and a class is for a group of items.

So, if you want a group of items to appear the same, use class to define that look.

If you want just one specific item to have that look, you can use an ID.

When you work with JavaScript later on, you will find other uses for the ID, such as checking input, or adding output.

And as Max Weir has pointed out, the specificity also is different, the #id will override the .class directives.

They mostly do the same thing, but ids are supposed to be unique (appear only one time)