简体   繁体   English

如何使用:nth-​​child将背景应用于无序列表?

[英]How to apply a background to Unordered List using :nth-child?

I am creating an UL and trying to apply a background-color to every other row using :nth-child(2n+1) Each row is basically a sub-item to the main UL, yet I have been able to apply a style to each UL (perhaps the sub-items within the nested UL are interfering). 我正在创建一个UL,并尝试使用:nth-​​child(2n + 1)将背景色应用于其他所有行,基本上每一行都是主UL的一个子项,但是我已经能够将样式应用于每个UL(也许嵌套UL中的子项目会干扰)。 I've tried almost every combination imaginable, what am I missing? 我已经尝试了几乎所有可以想象的组合,但我缺少什么呢? Noy Hadar 诺伊·哈达尔(Noy Hadar)

<style>
#data {
    width: 100%;
    list-style: none;
}
#data li ul {
    list-style: none;
    float: left;
    width: 100%;
    background: #FFC;
}
#data li ul:nth-child(2n+1) {
    background: #3F6;
    font-weight: bold;
}
#data li ul li {
    display: block;
    float: left;
    width: 10%;
    line-height: 24px;
}
</style>


<ul id="data">
        <li>
            <ul>
                <li>Closed Date</li>
                <li>Price</li>
                <li>DOM</li>
                <li>Address</li>
                <li>Price Change</li>
            </ul>
        </li>
        <li>
            <ul>
                <li>Closed Date</li>
                <li>Price</li>
                <li>DOM</li>
                <li>Address</li>
                <li>Price Change</li>
            </ul>
        </li>
        <li>
            <ul>
                <li>Closed Date</li>
                <li>Price</li>
                <li>DOM</li>
                <li>Address</li>
                <li>Price Change</li>
            </ul>
        </li>
        <li>
            <ul>
                <li>Closed Date</li>
                <li>Price</li>
                <li>DOM</li>
                <li>Address</li>
                <li>Price Change</li>
            </ul>
        </li>
        <li>
            <ul>
                <li>Closed Date</li>
                <li>Price</li>
                <li>DOM</li>
                <li>Address</li>
                <li>Price Change</li>
            </ul>
        </li>
</ul>

You need to target the li elements not the ul so: 您需要定位li元素而不是ul这样:

#data li ul li:nth-child(2n+1) {
    background: #3F6;
    font-weight: bold;
}

Try this: 尝试这个:

table.stripedTable {
background-color: #222;
    color: #fff;
}
    .stripedTable tr:nth-child(2n) {
        background-color: #EEE;
        color: #222;
    }

And the HTML: 和HTML:

<table id="table" class="stripedTable">
        <tr id="Row 1">     
                    <td>Row 1</td>
            <td id="Cell 1">Hi
            </td>
        </tr>
        <tr id="Row 2">
                <td>Row 2</td>
                <td id="Cell 2">Cell 2</td>
            </tr>
            <tr id="Row 3">
                <td>Row 3</td>
                <td id="Cell 3">Cell 3</td>
            </tr>
            <tr id="Row 4">
                <td>Row 4</td>
                <td id="Cell 4">Cell 4</td>
            </tr>
</table>

I see the problem, here's a jsFiddle fixing it. 我看到了问题,这是一个jsFiddle修复它。 I'll explain. 我会解释。

#data li ul:nth-child(2n+1) {
    background: #3F6;
    font-weight: bold;
}

What this is doing is selecting the ul element that is the nth-child of the li , your structure only has one ul after the li so it's the only one being selected, which is why your entire table is green. 这是什么东西做的是选择ul元素是的第n个孩子li ,你的结构只有一个ulli因此它被选中的只有一个,这就是为什么你的整个桌子是绿色的。

here's what i changed it to: 这是我将其更改为:

#data li:nth-child(2n+1) ul {
    background: #3F6;
    font-weight: bold;
}

now we're selecting the li (that contains the ul ) that is the nth-child of the #data container, and then we're styling the ul that is in that li , so we're selecting every other row and then styling it. 现在我们选择#data容器的第n个子#data li (包含ul ),然后我们对liul进行样式设置,因此我们选择其他所有行,然后进行样式设置它。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM