【发布时间】:2016-04-08 20:13:22
【问题描述】:
HTML:
<ul>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<ul>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
<li>ho</li>
</ul>
CSS:
ul {
margin-left: 15px;
}
li {
background-color: blue;
position: relative;
height: 20px;
}
li:before {
content: "";
position: absolute;
display: block;
top: 0;
left: -1000px;
background: blue;
width: 1000px;
height: inherit;
}
li:nth-child(odd), li:nth-child(odd):before {
background-color: green;
}
li:nth-child(odd)::before {
background-color: green;
}
li:nth-child(even) {
color:white;
}
这是一个fiddle,所以你可以看到它的样子。如您所见,我想要一个交替列表,但也希望 child-ul 永远不要重复相同的颜色。如您所见,有两条绿线紧随其后。我怎样才能防止这种情况发生?
有什么想法或技巧可以实现吗?
【问题讨论】:
-
仅供参考,您不应该在这样的 UL 中嵌套 UL
-
CSS 本身不可能(尽管您可以手动破解它)。你需要 JS - 类似 - stackoverflow.com/questions/36487297/…
-
是的,您的嵌套
ul必须在您的li之一内。 -
你的意思是这样的:jsfiddle.net/K3TuN/1306