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 trialJohanne Grenier
3,032 PointsDiv 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?
4 Answers
Callum King
Front End Web Development Techdegree Student 4,934 PointsIn 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
3,032 PointsAha! Thank you, that makes sense then :) Potentially, I could call up an element by its id name in CSS as well, right?
Callum King
Front End Web Development Techdegree Student 4,934 PointsYes you can call up an ID in CSS.
Mandaar Dahale
2,527 PointsThe 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.
terminator genesys
1,969 PointsID are more specific than class selector and it will override the element selector and class selector assigned to an element
Johanne Grenier
3,032 PointsGreat, thx :)
A X
12,842 PointsA X
12,842 PointsThanks for asking this Johanne, I was wondering the same thing!