工作时遇到这样一个问题:根据某元素所包含的子元素个数,分别设置不同的样式,这个用js可以解决,不过个人认为用css解决可能更简单一点。这也正好加深了我对css选择器的理解和运用。
demo如下:
效果图如下
完整代码如下:
css3
*{
box-sizing:border-box;
}
ul{
width:100%;
margin:0;
padding:0;
font-size: 0;
}
li{
margin:0;
padding:0;
display:inline-block;
vertical-align: top;
font-size: 13px;
border:1px solid red;
height:30px;
}
li:nth-last-child(1):first-child{
width:100%;
}
li:nth-last-child(2):first-child,
li:nth-last-child(2):first-child ~ li{
width:calc(100% / 2);
}
li:nth-last-child(3):first-child{
width:calc(100% / 3);
color:blue;
}
li:nth-last-child(3):first-child ~ li:nth-last-child(1){
width:calc(100% / 4);
color:red;
}
li:nth-last-child(3):first-child ~ li:nth-last-child(2){
width:calc(100% / 6);
color:yellow;
}
- 11111
- 11111
- 22222
- 11111
- 22222
- 33333
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。







