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 Attribute Selectors

Andrew Teece
Andrew Teece
4,885 Points

'form' & 'div' needed before [ ]?

Just to be clear, for example, is 'form' or 'div' needed before the [ ]? It seems to me that even without they will target the element. Thanks!

1 Answer

andren
andren
28,558 Points

They are only needed if you want to target form or div elements.

The attribute selector will select all elements that have a matching attribute reagardless of the element type if you use it on its own. When you use a selector like div[class="example"] you are actually combining a tag selector and a attribute selector. If you dropped div from the earlier example you would target all elements with an attribute of class set to example, not just div elements, which might or might not be what you want.

Andrew Teece
Andrew Teece
4,885 Points

Thank you. Just wanted to make sure I was clear on that.