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

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

ID selectors and Class selectors in CSS

I know that an element can have only 1 ID but can have many Classes however the thing that i do not get is I can use the same ID for different elements same as Classes I tried it and it worked so i cant see what is the difference between ID and Class selectors except that an element can have only 1 ID but can have many Classes

Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points

That is exactly the main difference between id's and classes!

Nour El-din El-helw
Nour El-din El-helw
8,241 Points

so I can use the same ID for different elements same as Classes right?

1 Answer

Mathew Tran
PLUS
Mathew Tran
Courses Plus Student 10,205 Points

Yep! as you said before though, elements can only have one id, whereas that same element can have any number of classes.

<div id="articlePreview" class="focused animate">
    <p> This is a preview to an article that I am focusing on </p>
</div>
<div id="articlePreview" class="no-animate">
    <p> This is a preview to an article that I am not focusing on </p>
</div>