是否有一种方法可以根据父类来重新定义LESS变量?

问题描述 投票:0回答:1

所以我有这个html标记

<div class="text-white">
    <div class="textClass"></div>
</div>

textClass类有一个较少定义的变量,它将标题颜色变为黑色,但是我想要的是,如果textClass元素具有text-white父对象,则textClass标题颜色随后将变为白色。这可能吗?

这是我到目前为止所拥有的,但是没有用:

//I defined the color of the heading
@heading-color: black;

//My attempt to redefine the heading-color base on class
.text-white > .textClass {
    @heading-color: white;
}

如果这不可能,那么我怎样才能少用一点呢?

谢谢您的帮助。

css less
1个回答
0
投票

在这种情况下,您可以在其中不带-字符的情况下命名变量,例如:

@headingColor: black;

.text-white > .textClass {
    @headingColor: white;
    background-color: @headingColor;
}
.some-heading {
    background-color: @headingColor;
}

输出:

.text-white > .textClass {
    background-color: #ffffff;
}

.some-heading {
    background-color: #000000;
}

注意:覆盖它时,可以在合并范围内使用它。

© www.soinside.com 2019 - 2024. All rights reserved.