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 trialMajid Mokhtari
10,506 PointsDifference between Id and class
What is the major difference between id and class in writing html and css? and when should I use class instead of id? can somebody explain it to me please, I'm confused here!
4 Answers
Ben Cato
Courses Plus Student 1,096 PointsUsually you'll want to use class names. You can use the same class name as many times as you want. It's nice to use all naming conventions wisely, so you'll be able to address many elements at once (for example).
You can only use an id name one time. You can still have as many id's as you want, but you can't have more than one with the same name. So, if you are sure that you're only going to have a special case where you specifically need something unique on your page, you can assign it to an id name. But, you can also use a class instead, but only have that class name once in your code.
Aaron Arkie
5,345 PointsHello! Try checking out this site it explains all you need to know i hope this helps! http://css-tricks.com/the-difference-between-id-and-class/
Margo Rachkulik
16,343 PointsId attribute using for identification single element, but classes using for grouping elements with same styles or functions. 1 Id = 1 element, 1 class = multy element, any numbers of elements. I hope it help you.
James Barnett
39,199 PointsClasses and IDs serve the same purpose as styling hooks for your HTML. IDs have 2 important differences over classes.
- You can add a particular class name to various elements on a page, you can only use an id of a particular on a single element on a page
- IDs have a much higher specificity then classes
IDs are best used only when needing to raise the specificity of an element.