The difference is this:
.parentClass {
&.childClass {}
}
will actually generate the selector .parentClass.childClass
, meaning that you are selecting for an element that has both classes, e.g. <div class="parentClass childClass">
. This is likely not the case you want. Meanwhile, this:
.parentClass {
& .childClass {}
}
will compile to:
.parentClass .childClass {}
...which will select an element with the class childClass
that is a child of an element with the class parentClass
, e.g.:
<div class="parentClass">
<div class="childClass"></div>
</div>
As @deceze has pointed out, in your simplified example the &
is not necessary, if all you want is to imply a hierarchical relationship, because it is syntactically identical to:
.parentClass {
.childClass {}
}
...which also gives you .parentClass .childClass {}
.