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
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.

Johanne Grenier
Johanne Grenier
2,111 Points

Aha! Thank you, that makes sense then :) Potentially, I could call up an element by its id name in CSS as well, right?

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