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.

JavaScript Treehouse Club - MASH MASH - HTML Forms/Divs/Input

Johanne Grenier
Johanne Grenier
2,111 Points

Div id VS class

What is the main difference between the id attribute and the class attribute in div elements?

I understand that the id names it, basically we are identifying it and naming it. I'm not sure what class stands for. I understand that it allows us to call them up in CSS. But wouldn't we be able to call them up using their id as well? And if so - when should we use which?

Thanks for asking this Johanne, I was wondering the same thing!

4 Answers

Callum King
seal-mask
.a{fill-rule:evenodd;}techdegree
Callum King
Front End Web Development Techdegree Student 4,934 Points

In a HTML Document, you can only use the same ID on one element, but with classes you can give the same class to multiple elements.

Mandaar Dahale
Mandaar Dahale
2,527 Points

The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one. Like class div can be used to group elements together so that they can be easily targeted in css.

ID are more specific than class selector and it will override the element selector and class selector assigned to an element