Css 如何每3个li元素添加一条水平线?
编辑:真正的问题 假设我有一个固定宽度的UL项目列表Css 如何每3个li元素添加一条水平线?,css,Css,编辑:真正的问题 假设我有一个固定宽度的UL项目列表 <ul> <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> <li>item5</li> <li>item6</li> <li>item7</li
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
<li>item6</li>
<li>item7</li>
<li>item8</li>
<li>item9</li>
</ul>
如果我使用一个表,我可以对TR元素应用一个边框。但是在UL上,没有行的标记。有没有CSS技巧可以使用?为什么不为
li
使用一个简单的边框底部
(使用:nth-last-child()删除了最后一行的边框
如果要删除最后一个元素的边框,请使用
ul li:last-child,
ul li:nth-last-child(2),
ul li:nth-last-child(3) {
border-bottom: 0;
}
这里我不需要解释什么复杂的东西,只要ul:after
过去,我就用它来自清除元素,因为它包含浮动的li
元素
为了删除最后一行边框,我使用了:last child
和:nth-last-child()
pseudos,分别选择lastli
、second lastli
和third lastli
如果每个li
中的内容都是可变的,那么您可以参考由创建的演示(谢谢)
(来源:web tiki)
您只需添加
ul li:nth-child(3n+4){
clear:left;
}
对于代码,只需一个注释,删除li
元素上的水平填充
,否则使用框大小:边框框
属性更改框模型行为。您可以执行以下操作:
使用第n个选择器:
CSS:
*{margin:0;padding:0;list-style:none;}
ul li{float:left;padding:20px;border-bottom:1px solid #ddd;}
ul li:nth-child(3n+1){clear:both;}
下面是一个纯CSS解决方案:
ul {
width : 500px;
display: block;
flex-wrap: wrap;
display: flex;
}
li {
list-style-type: none;
width: 33%;
border-bottom: 1px solid red;
text-align: center;
}
我建议采用以下解决方案:
css
ul > li:nth-child(3n+4) {
clear:left;
}
ul > li:nth-child(3n+3) {
padding-right: 0;
}
li{
float:left;
list-style-type: none;
padding-right: 15px;
border-bottom: solid 1px;
padding-bottom: 15px;
padding-top: 15px;
}
为什么不使用多个ULT呢?如果您需要使用jQuery代码来处理+1,这是最好的解决方案,因为它可以处理任意内容。。()@GabyakaG.Petrioli如果内容是可变的,则始终可以使用display:table编码>和显示:表格单元格
或者更好地使用多个ul
元素,我当然可以像dystroy一样使用flex,但还是兼容性matters@Mr.Alien此解决方案依靠LI相互堆叠而没有余量。但我已经使用了填充和边距来创建垂直分隔符(请参见编辑)。对不起,我知道我在最初的问题中没有说明这一点。我忽略了使用边框底部的简单解决方案。
ul {
width : 500px;
display: block;
flex-wrap: wrap;
display: flex;
}
li {
list-style-type: none;
width: 33%;
border-bottom: 1px solid red;
text-align: center;
}
ul > li:nth-child(3n+4) {
clear:left;
}
ul > li:nth-child(3n+3) {
padding-right: 0;
}
li{
float:left;
list-style-type: none;
padding-right: 15px;
border-bottom: solid 1px;
padding-bottom: 15px;
padding-top: 15px;
}