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 trialKitty Zee
1,432 PointsWhy was a class used instead of an id tag # for the Contact Details list items?
I thought they both do the same thing (call on an element in your HTML) except class is used when you have multiple instances and id tags are only used for one instance.
3 Answers
Simon Woodard
6,545 PointsThey do indeed do the same thing in that they identify the object to be styled. However in CSS, an id is given a higher priority than a class.
So you can declare an object to have a class and an id, if you were to assign conflicting styles, the id would win out.
Aina R.
Courses Plus Student 1,527 PointsYou completely right here : "I thought they both do the same thing (call on an element in your HTML) except class is used when you have multiple instances and id tags are only used for one instance."
About why a class was used instead of an id, was just about choice.
Kitty Zee
1,432 PointsThanks for confirming, Aina. I was afraid I was missing something there :)
Chris Malcolm
2,909 PointsYes agree with aina, its up to the designer to choose what's best. I often like to use classes for more heavily styled elements even if there is only one. And if it is more of something that is for identifiing elements/holding data for access via javascript selector, I will more often use an Id.
Kitty Zee
1,432 PointsThanks for the advice, Chris. I haven't gotten to javascript or any complicated coding yet, but that's something for me to look forward to once I am making decisions about classes and ids to write better code :)
Aina R.
Courses Plus Student 1,527 PointsAina R.
Courses Plus Student 1,527 PointsThanks for this.
Kitty Zee
1,432 PointsKitty Zee
1,432 PointsThanks for replying. I had no idea there was any type of prioritization between the two. I'll have to play around with that and see how it works. Thanks Simon!