如下html结构,想要实现:当有两个item时,隐藏第二个
<div class="item">第1个</div>
<div class="item">第2个</div>
可以使用相邻选择器
.item + .item{
display: none;
}
但是要实现:当有两个item时,隐藏第一个,只有一个item时不隐藏第一个。
由于css没有前向选择器,所以需要用nth-last-type实现
.item:nth-of-type(1):nth-last-of-type(2){
display: none;
}
网友评论