Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What does space mean in CSS selector?

A space in a CSS selector has very special meaning. It means that the part of the selector that occurs right of the space is within (a child of) the part of the selector to the left.

What is the difference between and space in CSS?

vs. Space. Here, the (space) selects all the all the <span> elements inside <div> element even if they are nested inside more than one element. The > selects all the children of <div> element but if they are not inside another element.

What is difference between .FOO and #foo in CSS?

The id selector “#” is used to represent id=”id_name” in HTML element. Each elements can contain more that one “.” selector means that elements is containing more than one class which is separated by space, the will be selected by multiple dots like .

What are the 3 different kinds of selectors in CSS?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)


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


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


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