简体   繁体   English

CSS为什么最后一个li不被li:nth-​​child(odd)或li:nth-​​child(even)定位?

[英]CSS Why is the last li not being targeted by li:nth-child(odd) or li:nth-child(even)?

This is pretty straightforward. 这很简单。

I have the following HTML structure: 我有以下HTML结构:

<ul id="myContactList">
    <li>
        <ul>
            <li>item 1</li>
            <li>item 2</li>
            <li>item 3</li>
            <li>item 4</li>
        </ul>
    </li>
    ...
</ul>

and the trouble maker CSS: 和麻烦制造者的CSS:

ul#myContactList>li>ul>li {
    float:left; /* Trouble maker */
}

Here's the JSFiddle . 这是JSFiddle

Why isn't the last ul#myContactList>li being targeted by li:nth-child(odd)? 为什么最后一个ul#myContactList> li不是由li:nth-​​child(odd)作为目标?

Thanks in advance, cheers! 预先感谢,加油! :) :)

It is targeting it, but you have an issue with the floats not being cleared in the last list item. 它是针对它的,但是您有一个问题,未在最后一个列表项中清除浮点数。 See http://jsfiddle.net/ekXjy/4/ (specifically line 20 of the CSS, which causes a new float context for each list item). 请参阅http://jsfiddle.net/ekXjy/4/ (特别是CSS的第20行,它会为每个列表项带来新的float上下文)。

ul#myContactList>li>ul {
    list-style-type:none;
    padding:0;
    overflow: hidden; /* New style, to clear the floats contained within */
}

The clear:both you had for ul#myContactList>li>ul clears the floats for the list items preceding the last one, but nothing cleared the floats in the last item. clear:两者都具有ul#myContactList> li> ul可以清除最后一个项目之前的列表项的浮动,但是不会清除最后一个项目中的浮动。 Using overflow:hidden to give each list item its own block context fixes that. 使用overflow:hidden为每个列表项提供自己的块上下文可以解决此问题。

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

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