【发布时间】:2017-07-07 09:06:49
【问题描述】:
我想包含另一个类的值 - 并将其用于不同的东西:
我有这门课:
.sourceClass {
color: red;
}
我有这门课:
.destinationClass {
border-color: ###should be the color from .sourceClass => red
}
这可能吗?我该怎么做?
【问题讨论】:
我想包含另一个类的值 - 并将其用于不同的东西:
我有这门课:
.sourceClass {
color: red;
}
我有这门课:
.destinationClass {
border-color: ###should be the color from .sourceClass => red
}
这可能吗?我该怎么做?
【问题讨论】:
您用“Sass”标记了您的帖子。你在使用 Sass/SCSS 预处理器吗?如果是这样,您将声明并使用这样的变量:
$myColor: red;
.sourceClass { color: $myColor; }
.destinationClass { border-color: $myColor; }
如果你不使用 Sass,你可以阅读 native CSS variables - 目前在 Firefox 和 Chrome 中工作,但 not IE/Edge。
最后,所有当前浏览器都支持一个可能的解决方案,这将取决于您的 DOM 层次结构:currentColor。
如果您的.destinationClass 是.sourceClass 的子代,因此继承color: red,您可以简单地使用border-color: currentColor 来获取该颜色并将其用作边框颜色。
希望这会有所帮助!
【讨论】:
.destinationClass 是.sourceClass 的孩子,你可以使用currentColor。