我对@extend规则有一点问题,这就是我得到的(专注于h1):
.content-header {
// CSS properties
h1 {
// CSS properties
}
}
.box-header {
// CSS properties
h1 {
@extend .content-header h1; // My selector problem!
// And his own CSS properties
}
}
Run Code Online (Sandbox Code Playgroud)
所以它将是:
.content-header h1, .box-header h1 {
/* Happily sharing the same CSS properties */
}
Run Code Online (Sandbox Code Playgroud)
但似乎@extend不喜欢这样,是否有任何其他方式来写这个而不给出h1一个类?
一个显而易见的解决方案是对子级定义一个新的@mixin your-name为你的.content-header h1定义和@include your-name范围内.box-header h1。
但是,有一个更好的解决方案引用父选择器:&:
h1 {
.content-header &,
.box-header & {
// CSS properties
}
.box-header & {
// And his own CSS properties
}
}
Run Code Online (Sandbox Code Playgroud)
这并不明显,因为逻辑相反,但最好维护。您正在更改h1特定选择器的定义。