假设我有以下设置,
.block
.block__header
.block__content
.block__footer
现在我想显示这个块的活动状态。假设块本身有一个绿色背景,元素 2 和 3 应该有粗体文本。据我了解 BEM 的理念,不应该使用子选择器来保持尽可能低的特异性。
那么这真的是这样做的方法吗?
.block.block--active
.block__header
.block__content.block__content--active
.block__footer.block__footer--active
更新:我将如何在 SASS 中编写该解决方案(对它来说非常新)?到目前为止,这是我的设置......如果我可以使用嵌套选择器,这里的最佳做法是什么?
.block {
&--active {
}
&__header {
}
&__content {
// active modifier of content
&--active {
font-weight: bold;
}
// would be the same as
.block--active & {
font-weight: bold;
}
// but can i reference the active block somehow else in sass?
// & is a parent selector but i would need the parent of the parent here...
}
&__footer {
&--active {
}
}
}