【问题标题】:Compass - include one value for another property指南针 - 包括另一个属性的一个值
【发布时间】:2017-07-07 09:06:49
【问题描述】:

我想包含另一个类的值 - 并将其用于不同的东西:

我有这门课:

.sourceClass {
  color: red;
}

我有这门课:

.destinationClass {
  border-color: ###should be the color from .sourceClass => red
}

这可能吗?我该怎么做?

【问题讨论】:

    标签: css sass compass


    【解决方案1】:

    您用“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 来获取该颜色并将其用作边框颜色。

    希望这会有所帮助!

    【讨论】:

    • 谢谢 - 问题是,我无法更改 .sourceClass - 所以无法使用变量。
    • @JörgWrase 你好,半年后!那么唯一的解决方案就是我的消息的最后一部分——如果.destinationClass.sourceClass 的孩子,你可以使用currentColor
    猜你喜欢
    • 2014-01-18
    • 2021-02-07
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多