CSS定位最后一个不是最后一个孩子的类类型

CSS定位最后一个不是最后一个孩子的类类型

问题描述:

JS小提琴

不改变html 是否有任何方法使用CSS定位最后的 .red 类?

Without altering the html is there any way to target the last .red class using CSS?

<div class="row">
    <div class="red">Red</div>
    <div class="red">Red</div>
    <div class="red">Target Me With CSS???</div>
    <div class="blue">Blue</div>
    <div class="blue">Blue</div>
    <div class="blue">Blue</div>
</div>

这里是我试过的:

.row > div{
    display:inline-block;
    padding:10px;
}

.row .red{
    background-color:#770000;    
    color:#fff;
}

.row .red:first-child{
    background-color:#440000;    
    color:#fff;
}

/*have tried :last-of-type too*/
.row .red:last-child{
    background-color:#FF0000;    
}

.row div:last-child{
    background-color:#0000BB;    
}


是不使用JS的方法。

你可以得到的最接近的是第三个项目:

The closest you can get is to target the 3rd item with:

.row div:nth-child(3) {
    background: chucknorris;
}

您可以包含一个限定符,像这样:

You can include a qualifier to only target the third child if it is .red like so:

.red:nth-child(3) {  
   background: chucknorris; 
}

http://jsfiddle.net/s76J3/3/