【发布时间】:2017-10-18 18:39:11
【问题描述】:
我有一些看起来像这样的 sass
.activities {
border: 1px solid green;
.activity {
background-color: red;
&:first-child {
background-color: yellow;
}
}
}
我希望作为第一个子元素的 .activity 元素只有在具有 .activities 类的父元素也没有类 .events 时才为黄色。
在下面的示例中,第一个 .activity 元素应该有 background-color: red
<div class="activities events">
<div class="activity">one</div>
<div class="activity">two</div>
<div class="activity">three</div>
</div>
当具有.activities 的元素也具有.events 时,是否有一种DRY 方法来否定这种第一个子样式?
或者这是最简单的方法:
.events .activity:first-child {
background-color: red;
}
【问题讨论】:
-
我认为最底层的方式是最简单/最干的。您也可以使用
:not选择器,但这会更复杂。