<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>
使用Less,我如何根据grandParent类应用子文本颜色
.grandParent {
.parent1 {
.childOfParent1 {
color : red;
.grandParent:not(active) .parent1 .childOfParent1 {
color : green;
}
}
}
}
以上可能使用以下代码,但我不想重复该代码
.grandParent {
.parent1 {
.childOfParent1 {
color : red;
}
}
&:not(active) .parent1 .childOfParent1 {
color : green;
}
}
我猜最容易维护的代码是这样的:
.parent {
.child {
color: green;
.grandParent.active & {color: red}
}
}
或更少的DRY(如果您真的想使用:not
):
.parent {
.child {
.grandParent & {color: red}
.grandParent:not(.active) & {color: green}
}
}
有关此类用法的更多详细信息,请参见更改选择器顺序&
。
在下面的评论之后,这是另一个变体:
.grandParent {
.parent {
.child {
color: green;
.active& {color: red}
}
}
}
[嗯,有点题外话]尽管确实要使其保持可维护性,但有一些注意事项:
.grandParent
或.parent
冗余实际上)。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句