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

HTML

Arturo Espinoza
Arturo Espinoza
9,181 Points

When should I use an Id element and when should I use a classes element.

When should I use an Id element and when should I use a classes element.

4 Answers

Stephen Kiers
Stephen Kiers
6,958 Points

It depends on how you are using them. IDs and Classes have different functions depending on if you are using them for CSS or javascript. In practice, CSS doesn't care; javascript does. As Adam said, you can only have one ID per page, but you can reuse the same classes over and over. That is the simplest explanation, but there is more to it than that. I would go into detail, but css-tricks does a great job here : http://css-tricks.com/the-difference-between-id-and-class/

ID's are unique and should only be used once per HTML page. Each element can have only one ID.

Classes are NOT unique. You can use the same class on multiple elements and you can use multiple classes on the same element.

George Elias
George Elias
3,015 Points

ID elements are unique and are use to target specific elements in CSS or Javascript. On the other hand, CLASSES can be repeated and should be used to apply the same styles to multiple elements.

Arturo Espinoza
Arturo Espinoza
9,181 Points

Thank you for your replies. The link helped a lot with the examples.