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

Why is there no space after the element when using an attribute selector ("a[class]" instead of "a [class]")?

This kind of inconsistency makes CSS rules super confusing and easy to mess up!

7 Answers

Cosmin Cioaclă
Cosmin Cioaclă
21,339 Points

So:

a[width="33"] will select all anchor (a) tags that have the property of width="33".

a [width="33"] will select all elements that are inside an anchor (a) tag that have the property of width="33".

The space is there to go down a level in the CSSOM tree. If there is no space, the selector targets the current level.

Hope that clears it up.

Awesome answer, Cosmin! I need to brush up on my CSS some more I suppose! :)

That's just the way CSS was written. The way I imagine it is that you are putting a space between all individual elements/pseudo-classes/IDs/Classes etc. If it is something that belongs inside the element's guts, then it's close to the element, not spaced away from it. Maybe I just have an overactive imagination or something, though! haha

That's probably a good mnemonic if it's consistent! I'm just trying to draw logical connections between the various formatting rules (an equals sign instead of a colon, no space instead of a space, no commas instead of a pile of commas, etc.) so I can really understand the "why"s and therefore hopefully remember the rules better and debug less!

The equals sign is used for comparisons in CSS. The colon is for selecting pseudo-classes.

I understand that you are frustrated, but once you understand the rules of CSS, they will frustrate you no longer.

Spacing out elements in a selector statement targets child elements within those elements listed. For example, if I want to target any span that occurs within a paragraph, I can write:

p span {
//CSS here
}

That space says for CSS to find only spans that are child elements of paragraph elements.

That is completely different from targeting all paragraphs and spans, which would be:

p, span {
//CSS here
}

See the comma there? Commas are always used to separate individual things, and in CSS, it is no different. This code would now target all paragraphs and span elements on the page.

CSS just takes some getting used to. You'll get it! :)

Cosmin Cioaclă
Cosmin Cioaclă
21,339 Points

Hi sawtoothwave,

An attribute selector, just like a class selector, can be used on its own or combined with other selectors.

Therefor, a[href="tree.house"] will select all the anchor tags with that exact href attribute. a [href="tree.house"] will only look for that href in the child elements of that anchor. Conversely, a.tree-house will target all the anchor tags with a class of "tree-house", whereas a .tree-house will only look for a class of "tree-house" among the child elements of that anchor.

Hope that helps, Cosmin

Thanks for the examples, both! I'm not too frustrated actually, just trying to understand.

I think the part that's confusing me is that if "p span" targets all spans that exist within a p tag, and a[class] targets all classes that exist within an a tag, why does the syntax demand a space between "p" and "span" but no space between "a" and "[class]"? I understand that the brackets are to signify that "class" is an attribute selector, and not an html element named "class" but I still don't quite get why it isn't written as "a [class]" instead of "a[class]".

YES that's the stuff. Thank you!