Less allows one to select the parent selector (http://lesscss.org/features/#parent-selectors-feature)
How does one get the immediate parent selector, not the root parent selector?
Less allows one to select the parent selector (http://lesscss.org/features/#parent-selectors-feature)
How does one get the immediate parent selector, not the root parent selector?
A Base Example
It partly depends upon how you structure your LESS code. There is currently no way to do this with a normal nested structure. However, take the following example, where the
.grandchild
is our final target in all cases (it must be the outermost level--I called this "end target grouping" in a previous answer before LESS added documentation about using&
as a parent selector):LESS
CSS Output
As you can see, any code nested in the first level only has the end target of
.grandchild
in its selector string. Each level one goes "down" in the nest, one is actually going "up" in selector specificity. So to target just the "immediate parent" for the selector string, place it in the.child
of this example.Hovers Still Work
LESS
This will add to the above css these two outputs:
Skip Generations
You can code it to skip some generations, like so:
LESS
CSS Output
Abstracted
There are various ways this could be abstracted out, such that the code does not give the appearance of a nested look (which could confuse a user). Something like this is one way (output similar to that given in first and second examples above):
Final Comments
As seven-phases-max linked to in his comment, there is talk of adding generational precision within a normal nested context. The solution given here requires one to think "opposite" of nesting, but rather think only about the element being targeted. So the way to add a
.grandchild
into another selector would not be this mixin:LESS (expecting to add a parent by normal nesting)
CSS Output
It would be best to add it into the original code according to the proper place, but if that is not possible, then some repetition is needed (unless you set up some way in the original code to "insert" parents through creative mixin calling--I have no time to devout to that here).
Whether such opposite coding can be useful or not all depends upon one's goals and desires in organizing the LESS code.