【发布时间】:2013-02-06 10:34:27
【问题描述】:
我正在尝试通过使用 SCSS 来清理我的 CSS。
标准 CSS:
.dark-hr,
.light-hr {
width: 100%;
height: 1px;
margin: 15px 0px;
}
.dark-hr {
background-color: #595959;
}
.light-hr {
background-color: #cccccc;
}
vs SCSS:
.generic-hr {
width: 100%;
height: 1px;
margin: 15px 0px;
}
.dark-hr {
@extend .generic-hr;
background-color: #595959;
}
.light-hr {
@extend .generic-hr;
background-color: #cccccc;
}
有什么方法可以避免创建不会使用的“generic-hr”类?我希望某种巢能很好地工作。 在这种情况下,CSS 绝对比 SCSS 更干净、更易读。
理想情况下,我需要它在 SCSS 中工作:
.## {
// base class that is not outputted
.dark-hr {
//attributes the extend the base class '.##'
}
.light-hr {
//attributes the extend the base class '.##'
}
}
输出:
.dark-hr, .light-hr {
//shared attributes defined by '.##'
}
.dark-hr {
// overrides
}
.light-hr {
// overrides
}
【问题讨论】:
-
你可以使用
[class*='-hr'],这基本上意味着任何有-hr的类,至少你不需要每次都@extend,如果你选择添加更多@987654328 @classes,你不需要编辑/扩展任何东西 -
@ashley 你可以这样做,但是如果你需要添加一个没有“-hr”后缀但扩展@987654329的类会发生什么@?您还必须记住扩展
[class*='-hr'],如果项目已经搁置几个月,很容易忘记。
标签: css sass oocss precompiler