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 Foundations Advanced Selectors Pseudo-Classes: :nth-of-type and :only-of-type

Dylan Merritt
Dylan Merritt
15,682 Points

What is the difference between using a descendant selector and using the :only-of-type selector?

If there is only one of a type in a class, it seems to me that using a descendant selector and using the :only-of-type selector would accomplish the same thing, and that it would be easier to use a descendant selector. Is there any real difference between the two?

1 Answer

Steven Parker
Steven Parker
229,785 Points

Given the simple case described, there would be no functional difference. The differences would be in specificity (higher for :only-of-type) and browser compatibility (universal for descendant).

But more important would be what behavior would you intend for when another element of that type is added? At that point the :only-of-type would no longer apply at all, but the descendant would apply to both elements.