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 trial

CSS

Silvana y Erika :) Aponte
Silvana y Erika :) Aponte
4,733 Points

Class selector or ID attribute

Hi! Why should I use the class selector for the phone, mail and twitter elements? It works the same with an id attribute?

5 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

Don't exactly know what lesson you are working on put it's best practice to use an id if your element is unique such as having one slider on a website and giving it the id of "slider". You use a class when you have multiple elements that can use the same styling.

Silvana y Erika :) Aponte
Silvana y Erika :) Aponte
4,733 Points

I'm working in this lesson (https://teamtreehouse.com/library/how-to-make-a-website/adding-pages-to-a-website/add-iconography) The teacher uses class selector for an unique element. That's why I'm wondering if it can change for an id attribute.

Rene Alonzo
Rene Alonzo
2,513 Points

Hello! in CSS Basics / Basic Selectors - you will learn more about this. The id's have to be unique. Use class instead if you want to select multiple elements to apply an style for example.

Austin Whipple
Austin Whipple
29,725 Points

Always a good idea to only use IDs when you know you'll need them and when there isn't a chance of duplicating the same element elsewhere on the same page. A lot of developers will simply default to using a class selector rather than an ID selector for this reason. You never know when it might be a good idea to reuse the styles you've already written!

Check out this page for a bit more information.

Julie Myers
Julie Myers
7,627 Points

In general use the class selector if two or more elements, on a single page, will need to use the same rule. For example: <div class="sameStyles">Hi</div> <div class="sameStyles">Hi</div>

Use the id selector when there will be only one element, on the page with that id attribute value, that will need to use the rule. For example: <div id="first">Hello</div> --> The value of "first" for the id attribute can only be used once on the page. <div id="second">Bye</div> --> The value of "second" for the id attribute can only be used once on the page.

When you get into the DOM the getElementById() is more browser backwards compatible then getElementsByClassName().

jason chan
jason chan
31,009 Points

You want to use class if your planning to reuse it.

ID is you know it's the only element your changing or selecting.