0
<div class="grandParent active"> /*active class added dynamically*/ <div class="parent1"> <div class="childOfParent1"> Some Text </div> </div> <div class="parent2"> <div class="childOfParent2"> Some Text </div> </div> </div> 

Using Less how can i apply the child text color depending on grandParent class

.grandParent { .parent1 { .childOfParent1 { color : red; .grandParent:not(active) .parent1 .childOfParent1 { color : green; } } } } 

The above is possible using the below code but i do not want to repeat the code

.grandParent { .parent1 { .childOfParent1 { color : red; } } &:not(active) .parent1 .childOfParent1 { color : green; } } 

1 Answer 1

2

The most maintainable code would be something like this I guess:

.parent { .child { color: green; .grandParent.active & {color: red} } } 

Or less DRY (if you really want to get use of :not):

.parent { .child { .grandParent & {color: red} .grandParent:not(.active) & {color: green} } } 

See Changing selector order for more details on such & usage.


Following the comments below here's another variant:

.grandParent { .parent { .child { color: green; .active& {color: red} } } } 

[Well, slightly offtopic] Though if really takes into getting it maintainable, there're a few remarks:

  1. Avoid too specific selectors (so for this example I think either .grandParent or .parent are redundant actually).
  2. Never nest for the sake of nesting. Sometimes nesting is good but sometimes it's extremely ugly (see this nice brief answer for just a few of many reasons for never doing it blindly).
  3. DRY != maintainable. Often repeating one or two classes for "flat" styles are much better than writing a bloated and cluttered class hierarchy (Usually for such cases mixins and/or selector interpolation works better than plain nesting). E.g. instead of making it more easy modifiable (usually this is the only reason for being too heavy on DRY) think if you could write it the way you'll never want it to be modified at all.
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, Thank you for your answer, but my .parent1 selector is enclosed by .grandParent selector. The above solution will only work if the .parent1 selector is outside of the .grandParent selector. Is there any way i can apply condition on .grandParent from within the .grandParent selector?
@seenimurugan: No mate, since the & is added at the end, the compiled CSS selector would be like .grandParent:not(.active) .parent1 .childOfParent1. So effectively the structure is the same. Just the order of nesting the selectors in Less is modified to suit the needs.
Here is a sample for you using the code in the answer.
Hi Harry, thanks a lot for your time, But as i said earlier when the .parent1 is inside the .grandParent, the compiled output is .grandParent .grandParent .parent1 .childOfParent1 { color: #ff0000; } .grandParent:not(.active) .grandParent .parent1 .childOfParent1 { color: #008000; } . So the 'some Text' will not be colored as i expected.
With this approach the .parent1 should not be inside .grandParent in the actual Less file mate. The nesting should start with .parent1. But if for some reason you are not in favor of this approach then you may have to try something like this but this in a way is repetitive coding too.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.