Reputation: 464
<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;
}
}
Upvotes: 0
Views: 1718
Reputation: 11820
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:
.grandParent
or .parent
are redundant actually).Upvotes: 2