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

Attribute Selectors

Why do you not have to include the tag on class selection ([class] { }) but you have to include the tag on type selectors ( input[type=""] ) ?

2 Answers

You don't, with attribute selectors, if you include an element it specifies the scope of the selector.

Here are some examples to illustrate on it works:

  • [class] = select any element that has a class attribute
  • input[type="submit"] = select any <input> element of type submit
  • a[class] = select any <a> element that has a class attribute

http://www.htmldog.com/guides/css/advanced/attributeselectors/

That makes a lot more sense now thank you. Is there a difference between

a .class { }

and

a [class] { }

The former is only selecting for a class named class and the later is selecting for any class.

On the other hand, these are equivalent are according to select oracle

a .test

and

a [class=~"test"]


You can check out the select oracle and this section on attribute selectors.