我有一组不同主题的调色板,如下所示:
$primary: red;
$secondary: blue;
.theme-2 { $primary: green; $secondary: yellow; }
//..
//some random scss file that has typography class names
.cool-text { color: $primary; }有没有可能做到这一点,无论应用于容器的类名使用为其定义的变量调色板颜色?
例如:
<div class="theme-2">
<p class="cool-text"> cool </p> // should be green
</div>
<p class="cool-text"> cool </p> //should be red发布于 2016-11-23 02:12:36
因为Sass变量是在运行时之前编译的,所以它们不能是上下文敏感的,因此您提供的示例是不可能的。
这里有一些有帮助的阅读和替代示例:https://webdesign.tutsplus.com/articles/understanding-variable-scope-in-sass--cms-23498
我读入了原生CSS变量,它可以做你想做的事情,并且在除IE/Edge:https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables之外的大多数浏览器中都获得了支持
发布于 2016-11-23 02:13:02
你可以使用!default在这里用你的html codepen检查这个运行的代码片段。
$primary: red !default;
$secondary:blue !default;
.cool-text { color: $primary; }
.theme-2 {
$primary: green;
$secondary: yellow;
.cool-text{
color: $primary;
}
}如果颜色没有设置为任何值,它将采用默认的红色,而不是您设置的任何颜色。
希望能有所帮助
https://stackoverflow.com/questions/40748622
复制相似问题