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

id or class css??

Hi everyone, I don't have clear when can I use id or a class in css, the video tutorials explain the concept, but for me class and Id are the same.

Thanks.

4 Answers

Something not mentioned here, is CSS specificity. An ID is worth 100 points, a class is only worth 10, when calculating css specificity. Too many ids used in css styling can create problems, because they will override styles you think they shouldn't.

I tend to use ids only on elements that do something. A form, a jquery tag, etc. I use classes for all other styling.

https://css-tricks.com/specifics-on-css-specificity/

for Id you can use just for one element in a page while class you can use for several element or use several classes for one element, I hope that answers your questions

Well they can seem the same thing but they aren't. In the early stages of learning won't make much sense but when you learn some advanced stuff like the advanced css selectors it will help, and it helps a lot with JavaScript/jQuery too. Don't worry about it, you will learn it how to use them more appropriate in different situations. Have a look on here for more information about it: https://css-tricks.com/the-difference-between-id-and-class/

All in all 'id' and a 'class' are the same well sort of...

They only really matter when you are working on a style sheet that's big and i'm talking about 10,000 lines of solid CSS.

You would use an 'id' when your making a object or something but don't want to ever use it again because 'id' you can only use one.

A class is kind of better because you can reuse them as many times as you need.

Like I said they ONLY reason you switch between the two is file size, because if everything was an id, you'd have a hell of a lot of id's haha

Hope this helped, -Shaun Moore