Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML How to Make a Website Customizing Colors and Fonts Use Classes in CSS

what is the main difference between id and class css selector??

what is the main difference between id and class css selector??

4 Answers

Evgeniia Mas
Evgeniia Mas
4,452 Points

Hello! Id is unique and you can use only one with that name on your page. Class you can assign to different elements and have two and more elements with same class on your page; id is also more specific when you style elements because of it's priority. So if you use same styles (font-size, for example) to the same element with id (20px) and class (10px) the font-size of id will be used.

If you validate your html code this mistake will be shown!!!! It works, but it is not correct! And it is obvious when you use javascript or jquery. You won't be able to select elements and make with them something if two or more elements which have same Ids.

I'll leave this with Evgeniia as HTML & CSS are not my strong points. This answer covers your question well, I think. :+1:

Steve.

Raihan Khan
Raihan Khan
2,168 Points

Which one is better to user ID or Class. Why should I use ID over Class

Neither is better but they are used for different things. Depending on what you are trying to achieve, one may have greater benefit than the other but that's case-specific and not a generally-applicable rule. I think a class has the benefit of being capable of being applied in more than one place, so you can select it in multiple places; an id does not have this capability. Sometimes, though, that perceived restriction is a benefit.

Steve.

Hi there,

An id is unique and can only be used once on a page. A class defines a style that can be re-used however many times you want.

I hope that helps,

Steve.

but why html and css did not restricted me when i apply same id to multiple elements.?

Not sure - won't selecting it only apply the style to the first occurrence of the id? I didn't think the style would be applied to multiple id's of the same name, just the first?

Jonathan Rhodes
Jonathan Rhodes
8,086 Points

An id is unique and can only be used once, where as a class can be used multiple times and is used to denote a category of elements. Ids are used when you want to select only one specific element, while classes can select all items given that class name.

great answer evgenlia mas..

Evgeniia Mas
Evgeniia Mas
4,452 Points

Thanks for your feedback!