html - 如何仅使用类 "box special"而不仅仅是 "box"开始对第 n 个子选择器进行计数?

标签 html css css-selectors

我想让一些盒子根据它们是组中的奇数还是偶数盒子而具有不同的属性,但仅适用于多个类的选择器:但它包括原始 box 类的对象,尽管它不在第 n 个 child 的 CSS 选择器中:

http://jsfiddle.net/wLx67r83/

<div class="box">1</div>
<div class="box special">1</div>
<div class="box special">1</div>

.box
{
    width: 100px;
    height: 50px;
    background-color: #e3e3e3;
}

.box.special:nth-child(odd)
{
    background-color: red;
}

.box.special:nth-child(even)
{
    background-color: blue;
}

第三个框应该是蓝色的,但它是红色的!第二个应该是红色的,但它是蓝色的!

最佳答案

您必须更改 css 中的偶数/奇数。

http://jsfiddle.net/wLx67r83/6/

<div class="box">1</div>
<div class="box special">1</div>
<div class="box special">1</div>

.box
{
    width: 100px;
    height: 50px;
    background-color: #e3e3e3;
}

.special:nth-child(even)
{
    background-color: red;
}

.box.special:nth-child(odd)
{
    background-color: blue;
}

编辑:或者您可以选择公式选项 http://jsfiddle.net/wLx67r83/28/

.special:nth-child(2n+0)
{
    background-color: red !important;
}
.special:nth-child(2n+3)
{
    background-color: blue !important;
}

关于html - 如何仅使用类 "box special"而不仅仅是 "box"开始对第 n 个子选择器进行计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808746/

相关文章:

jQuery addClass 只要至少一个后代有类

html - CSS:将边框应用于选项卡焦点图像

html - CSS line-clamp 在 Safari 中对内部 block 级元素不起作用

html - 悬停一个框并突出显示另一个框

javascript - 如何使用 jQuery 检查选​​择/单击的元素是否是 "n"的倍数?

html - 加载 css 文件失败

jquery - jquery slider 的CSS定位问题

html - 带有 %-height 和 100% 内容部分的粘性标题

html - 如何在任何第一个 child 悬停时突出显示所有第一个 child 等等?

c# - Selenium WebDriver : how to count number of DIV elements in a list