簡體   English   中英

您可以設置有序列表編號的樣式嗎?

[英]Can you style ordered list numbers?

我正在嘗試為有序列表中的數字設置樣式,我想添加背景顏色、邊框半徑和顏色,以便它們與我正在使用的設計相匹配:

在此處輸入圖片說明

我想這是不可能的,我必須為每個數字使用不同的圖像,即

ol li:first-child {list-style-image:url('1.gif')};
ol li:nth-child(2) {list-style-image:url('2.gif');} 
etc...

有沒有更簡單的解決方案?

您可以使用CSS counters結合:before偽元素來完成此操作:

 ol { list-style: none; counter-reset: item; } li { counter-increment: item; margin-bottom: 5px; } li:before { margin-right: 10px; content: counter(item); background: lightblue; border-radius: 100%; color: white; width: 1.2em; text-align: center; display: inline-block; }
 <ol> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ol>

我正在尋找不同的東西,並在 CodePen 上找到了這個例子;

試試這個: http : //codepen.io/sawmac/pen/txBhK

 body { font-size: 1.2em; font-family: "Helvetica Neue", Helvetica, sans-serif; margin: 50px; } .custom-counter { margin: 0; padding: 0; list-style-type: none; } .custom-counter li { counter-increment: step-counter; margin-bottom: 5px; } .custom-counter li::before { content: counter(step-counter); margin-right: 20px; font-size: 80%; background-color: rgb(180, 180, 180); color: white; font-weight: bold; padding: 3px 8px; border-radius: 11px; }
 <ol class="custom-counter"> <li>This is the first item</li> <li>This is the second item</li> <li>This is the third item</li> <li>This is the fourth item</li> <li>This is the fifth item</li> <li>This is the sixth item</li> </ol>

2021 更新,您可以在最近的瀏覽器中使用::marker偽元素選擇器: https : //developer.mozilla.org/en-US/docs/Web/CSS/ ::marker

 li::marker { color: red; } li { color: blue; }
 <ol> <li>item</li> <li>item</li> <li>item</li> <li>item</li> </ol>

暫無
暫無

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

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