css - Scss,第 n 个 child & :not() selector

标签 css sass

我对 scss 有点陌生,我的行容器中的行有这种基本样式。

.rows-container{
    border-bottom:2px ridge grey;
}
.row{
    height:30px;width:100%;

    &:hover div{
        background-color:lightgrey;
        }
    &:nth-child(odd){
        background: white;
        }
    &:nth-child(even){
        background: #e0e0e0;
        }
}

以下 html 非常简单:我遗漏了一些不重要的 html。

<div class="rows-container">
    <div class="row></div> //white
    <div class="row></div> //grey
    <div class="row></div> //white
    <div class="row></div> //grey etc...
</div>

但是现在我添加了子行

<div class="rows-container">
    {{each rows}}
        <div class="row"></div>           //parent row

        {{each childs}}
        <div class="subitem"></div>   //several rows from the same table which have a parent_id connected to the current row.
        {{#each}}

    {{#each}}
</div>

我打算在点击时切换子元素。但是当没有子项可见时(子项有自己的颜色),'.rows' 的奇数/偶数就乱七八糟了。现在我认为这是因为 nth-child 奇数/偶数是在容器中的所有行/子项上计算的,而不仅仅是 .row(s)。 有没有办法对 .rows 设置奇数/偶数样式,但从奇数/偶数旋转中排除 .subitems?我在想也许有一种方法可以在 scss 中使用 :not() ,但到目前为止我还没有成功。

最佳答案

使用 :nth-of-type

所以你的代码应该是:

.row{
    height:30px;width:100%;

    &:hover div{
        background-color:lightgrey;
        }
    &:nth-of-type(odd){
        background: white;
        }
    &:nth-of-type(even){
        background: #e0e0e0;
        }
}

REF: https://css-tricks.com/almanac/selectors/n/nth-of-type/

.subitem:nth-of-type(even) {
  background: lightslategrey;
}

.subitem:nth-of-type(odd) {
  background: red;
}
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>
<div class="subitem">subitem</div>
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>
<div class="subitem">subitem</div>
<div class="">subitem no class</div>
<div class="">subitem no class</div>

关于css - Scss,第 n 个 child & :not() selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186223/

相关文章:

javascript - 动画中的重复内容阅读更多/更少

html - 如何在宽度减小时增加高度?

javascript - 从 css 调整大小图像创建 Canvas ,在其上维护浏览器插值

html - 标签元素多个内部跨度文本截断

css - 我的类别页面中的对象未垂直对齐

javascript - 从 scss 访问伪类元素属性宽度

css - 用于在 Firefox 上滚动背景的条件 CSS

css - 如何反转 Bootstrap 4 中列的顺序?

compass-sass - 将列表作为参数传递给 SASS 中的 mixin?

javascript - 覆盖sass变量的麻烦