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 trialShmuel Zilberman
1,409 Pointswhats the difference between class and id?
whats the difference between class and id?
3 Answers
Max Weir
14,963 PointsI 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
9,598 PointsAs 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.
Adam N
70,280 PointsThey mostly do the same thing, but ids are supposed to be unique (appear only one time)