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 Basics (2014) Basic Selectors Descendant Selectors

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Why does Guil uses so many class selectors? In some cases it seems better to use an ID selector.

I see a lot of class selectors used in this video (ex: primary-content, secondary content, title). From what I understand, the class selector is better used when it needs to be repeated trough out the code, so should we assume that these classes will be used in the next videos? For me, an ID selector would be more appropriate since we'll only have one primary content and one secondary content.

2 Answers

Ian Evans
PLUS
Ian Evans
Courses Plus Student 978 Points

Many developers have learned through experience that using IDs as selectors in CSS can cause problems that are hard to debug as stylesheets become large and complex (mainly because IDs have such a high degree of specificity in the Cascade). Here's a decent overview on the topic:

https://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/

In general, most professional web developers rarely use ID selectors, and many don't use them ever. Indeed, many popular CSS linting tools forbid the use of IDs:

https://github.com/CSSLint/csslint/wiki/disallow-ids-in-selectors

In other words, use of classes instead of IDs in most cases is part of modern, idiomatic CSS.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

Oh, wow. I didn't have any idea about this. Thank you for clarifying this :).

Raffael Dettling
Raffael Dettling
32,998 Points

You can even assign both an id and a class to an element if it´s not enough :D It´s a bit intependent from your coding style by example i use classes more often then id´s.