簡體   English   中英

具有全寬邊框的嵌套列表項?

[英]Nested list-items with full width border?

我有一個無序列表,其中有多個嵌套項目,最多可達5級。 為了分隔每個列表項,我添加了一個border-bottom如下:

  li { border-bottom: 1px solid red; } ul { list-style: none; padding-left: 1em; } 
 <ul> <li>0.1 comment</li> <li>0.2 comment</li> <li> <ul> <li>1.1 comment (reply on 0.2)</li> <li>1.2 comment (reply on 0.2)</li> <li> <ul> <li>2.1 comment (reply on 1.2)</li> </ul> </li> <li>1.2 comment (reply on 0.2)</li> </ul> <li>0.3 comment</li> </ul> 

是否有可能有一個bottom-border不繼承列表項的填充並拉伸整個寬度? 認為可以使用background-image來完成,但我更願意找到純CSS解決方案。

您可以使用絕對定位的偽元素:

 #root { position: relative; } li:after { content: '\\0200B'; /* Zero-width space to place the border below the text */ position: absolute; /* Absolutely positioned with respect to #root */ z-index: -1; /* Place it behind the li, eg to avoid preventing selection */ left: 0; right: 0; border-bottom: 1px solid red; } ul { list-style: none; padding-left: 1em; } 
 <ul id="root"> <li>0.1 comment</li> <li>0.2 comment<br />second line<br />third line</li> <li> <ul> <li>1.1 comment (reply on 0.2)</li> <li>1.2 comment (reply on 0.2)</li> <li> <ul> <li>2.1 comment (reply on 1.2)</li> </ul> </li> <li>1.2 comment (reply on 0.2)</li> </ul> <li>0.3 comment</li> </ul> 

您可以將填充應用於li並為ul提供負邊距。 它需要每個級別的新規則

 ul { list-style:none; padding-left: 0; } li { border-bottom: 1px solid red; } /*level 2*/ li li{padding-left: 1em;} li li ul{margin-left: -1em;} /*level 3*/ li li li{padding-left: 2em;} li li li ul{margin-left: 2em;} /*level 4*/ li li li li{padding-left: 3em;} li li li li ul{margin-left: -3em;} /*level 5*/ li li li li li{padding-left: 4em;} li li li li li ul{margin-left: -4em;} /*remove double borders*/ li li:last-child{border-bottom:0;} 
 <ul> <li>0.1 comment</li> <li>0.2 comment</li> <li> <ul> <li>1.1 comment (reply on 0.2)</li> <li>1.2 comment (reply on 0.2)</li> <li> <ul> <li>2.1 comment (reply on 1.2)</li> </ul> </li> <li>1.2 comment (reply on 0.2)</li> </ul> <li>0.3 comment</li> </ul> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM