tags:

views:

125

answers:

4

What is the difference between:

.classA.classB {
  border: 1px solid; }

.classA .classB {
  border: 1px solid; }

?

A: 

No difference intended. But there should be space between them.

User
In certain situations a space is *required* (such as `div span`, i.e. all spans contained in divs). Thus, it is most consistent to always include a space.
Noldorin
-1 one for wrongness.
Williham Totland
This is wrong..a.b targets all elements with class="a b".a .b targets all elements with class="b" whose parents have class="a".
Sam DeFabbia-Kane
+23  A: 

.classA.classB refers to an element that has both classes A and B (class="classA classB"); whereas .classA .classB refers to an element with class="classB" descended from an element with class="classA".

Edit: Spec for reference: Attribute Selectors (See section 5.8.3 Class Selectors)

Williham Totland
Thats what I had suspected. Do you know if IE6 handles .classA .classB (with space) correctly?
David
@retrohound Just tried it - IE6 handles both cases (with space and without) correctly
jimyi
I'm glad you nailed this. This is often misunderstood. They have 2 totally different meanings, as you mentioned.
Justin Lucente
@retrohound: Yes, IE6 should handle this correctly. I don't have a box to test this on at the moment, though.
David
-1 for being rude.
User
@jimyi I've been told that IE6 cannot handle .classA.classB correctly (http://www.quirksmode.org/css/multipleclasses.html). Thanks for testing it tho.
A: 

Williham's answer is the only correct one. The specs can be found here:

http://www.w3.org/TR/CSS21/selector.html#class-html

Philippe Leybaert
+2  A: 

A style like this is far more common, and would target any type of element of class "classB" that is nested inside any type of element of class "classA".

.classA .classB {
  border: 1px solid; }

It would work, for example, on:

<div class="classA">
  <p class="classB">asdf</p>
</div>

This one, however, targets any type of element that is both class "classA", as well as class "classB". This type of style is less frequently seen, but still useful in some circumstances.

.classA.classB {
  border: 1px solid; }

This would apply to this example:

<p class="classA classB">asdf</p>

However, it would have no effect on the following:

<p class="classA">fail</p>
<p class="classB">fail</p>

(Note that when an HTML element has multiple classes, they are separated by spaces.)