有没有一种方法可以根据父级的类别重新定义LESS变量?

时间:2019-12-31 04:57:50

标签: css less

所以我有这个html标记

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

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

这是我到目前为止的内容,但是它不起作用:

textClass

如果这不可能,那么我该如何减少使用呢?

感谢您能提供帮助的人。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以这样命名变量,使其内部没有-字符,如下所示:

@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;
}

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

相关问题