jsFiddle - 我试图只在包装器中有超过4个容器的情况下才获取最后一个元素。是否可以使用css代替JS
来完成此操作<div class="wrapper">
<div class="container">Container #1</div>
<div class="container">Container #2</div>
<div class="container">Container #3</div>
<div class="container">Container #4</div>
<div class="container">Container #5</div>
</div>
.wrapper div:nth-child(n+4):last-child() {
background-color: gold;
}
答案 0 :(得分:9)
是。你可以做到这一点。你几乎只有一些小的修正。
小提琴:http://jsfiddle.net/kiranvarthi/q5parpxy/4/
.wrapper div:nth-child(n+5):last-child {
background-color: gold;
}
答案 1 :(得分:0)
这是正确的语法
.wrapper div:nth-child(n+4):last-child {
background-color: gold;
}
<div class="wrapper">
<div class="container">Container #1</div>
<div class="container">Container #2</div>
<div class="container">Container #3</div>
<div class="container">Container #4</div>
<div class="container">Container #5</div>
</div>
last-child
应该没有大括号
答案 2 :(得分:0)
你在这里
.wrapper div:last-child:nth-child(n+5) {
background-color: gold;
}
<div class="wrapper">
<div class="container">Container #1</div>
<div class="container">Container #2</div>
<div class="container">Container #3</div>
<div class="container">Container #4</div>
</div>
<div class="wrapper">
<div class="container">Container #1</div>
<div class="container">Container #2</div>
<div class="container">Container #3</div>
<div class="container">Container #4</div>
<div class="container">Container #5</div>
</div>
<div class="wrapper">
<div class="container">Container #1</div>
<div class="container">Container #2</div>
<div class="container">Container #3</div>
<div class="container">Container #4</div>
<div class="container">Container #5</div>
<div class="container">Container #6</div>
</div>