What does a space mean in a CSS selector? i.e. What is the difference between .classA.classB and .classA .classB?

CssCss Selectors

Css Problem Overview


What is the difference between these two selectors?

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

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

Css Solutions


Solution 1 - Css

.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)

Solution 2 - Css

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.)

Solution 3 - Css

.classA.classB it means that the elements with both classes name will be selected whereas .classA .classB means that the element with class name classB inside the classA will only be selected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionretrohoundView Question on Stackoverflow
Solution 1 - CssWilliham TotlandView Answer on Stackoverflow
Solution 2 - CssT EView Answer on Stackoverflow
Solution 3 - CssAmarnath VishwakarmaView Answer on Stackoverflow