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 CSS Selectors Selectors - Beyond the Basics Child, Adjacent, and General Sibling Combinators

Dylan Carter
Dylan Carter
4,780 Points

So, its best to use only class selectors?

so when we write css now we should be using classes that can apply to multiple things, so it wont have to be repeated a lot in the css, then just give the class names to the html element that you want it to effect, more than one if necessary. So while doing this, we basically do not need to ever use a actual element selector, or should that done maybe only if one element only needs a certian change? what would be the advantages and disadvantages of this? or just kept all by classes and maybe id's?

2 Answers

Mark Pryce
Mark Pryce
8,804 Points

It all depends on what you want to do with the elements.

ids are unique, so if you had a button, the only one of its kind on the page you would give it an id to target that specific element because it's 'unique' also ids can be used as anchors for js.

class names serve a similar purpose as ids but it can be used multiple times, imagine you had 3 columns of content, consisting of - basic info-1, main info-1, basic info-2. If the basic info columns was to be styled the same way you would add a class to these elements because it would save you writing css for each.

You would still use element selectors also, let's take an unordered list for example you would not give a class to each list item so you would simply target li. if you had multiple lists on your page you and this particular list is styled different you could use a class of this unordered list and use that to target list items .special li {}

I'm no expert but I hope this makes some sense and I hope it helps you.

Happy coding!

Evan Maroge
Evan Maroge
5,277 Points

I am still learning about this myself but the advantage of having such selectors that use an id instead of a class, is that it lets the page load faster. if you have a class which many tags belong, the browser will take longer time to load, especially when applying javascript to the elements. I think choosing a class or Id depends on what you're working on. That's all i know now. :)