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

When to use a class, ID and other

In the videos I've seen Guil use classes for pretty much everything, being very specific. Even for things which don't really require such specificity, for example,

header h1 { } could be the same as .heading { }

What's best to use? Does it matter? Also, when should I use ID's instead of classes?

1 Answer

Steven Parker
Steven Parker
243,656 Points

For reasons of both efficiency and clarity in both the HTML and CSS, classes are generally preferred.

But, that said, there's plenty of times when it makes sense to use a combined selector as in your example, or an ID.

A good time to use an ID is when you know your target is, and will always be (in future versions), just one element, and the specific styles you applying won't be used anywhere else in the document.

Ah I see. Thank you for clearing that up! :)