首页 > 解决方案 > CSS 类在未设置的地方被拾取

问题描述

这个类创建了一个表格,其中行颜色成对交替(两个灰色,两个白色等):

.table-striped-two-rows tbody tr:nth-child(4n+1), tbody tr:nth-child(4n+2) {
    background-color: rgba(0, 0, 0, 0.05);
}

问题是它不会影响表格class="table-striped-two-rows"

我怎样才能将样式包含在类中的表中?

标签: css

解决方案


您还需要为4n + 2行指定祖先以限制选择器的范围

.table-striped-two-rows tbody tr:nth-child(4n+1), 
.table-striped-two-rows tbody tr:nth-child(4n+2) {
    background-color: rgba(0, 0, 0, 0.05);
}

推荐阅读