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

Stein Christensen
Stein Christensen
3,592 Points

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

James Barnett
James Barnett
39,199 Points

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/

Stein Christensen
Stein Christensen
3,592 Points

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

a .class { }

and

a [class] { }

James Barnett
James Barnett
39,199 Points

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.