经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
使用CSS计数器美化数字有序列表的实现方法_CSS教程_CSS
来源:jb51  时间:2021/3/8 11:19:15  对本文有异议

在web设计中,使用一种井井有条的方法来展示数据是十分重要的,这样用户就可以很清晰的理解网站所展示的数据结构和内容,使用有序列表就是实现数据有组织的展示的一种简单方法。

如果你需要更加深入地控制有序列表数字的样式,你可能会觉得必须通过增加更多的 html DOM 结构或者通过 JavaScript 才能做到。幸运的是,使用 CSS计数器 可以更加容易的解决这个问题。

在这篇教程中,我们将学习到什么是 CSS计数器 和一些使用案例。

有序列表的问题

当你写了一个如下的有序列表,浏览器会自动在列表项前面加上数字

 

  1. <ol>
  2. <li>My First Item</li>
  3. <li>My Second Item</li>
  4. <li>My Third Item</li>
  5. </ol>

这看起来很好,但是它不允许你对数字进行样式调整。假如,你需要把列表前的数字放进一个圆圈里来修饰列表,你该怎么做呢?

一种方法是完全删除列表,并自己手动添加数字。

  1. <div>
  2. <span>1</span> My First Item
  3. </div>
  4. <div>
  5. <span>2</span> My Second Item
  6. </div>
  7. <div>
  8. <span>3</span> My Third Item
  9. </div>
  1. div {
  2. margin-bottom:10px;
  3. }
  4. div span {
  5. display:inline-flex;
  6. align-items:center;
  7. justify-content:center;
  8. width:25px;
  9. height:25px;
  10. border-radius:50%;
  11. background-color:#000;
  12. color:#fff;
  13. }

这确实是我们想要做的效果,但是也有一些缺点。首先,手动添加数字是很麻烦的。如果你需要更改一个编号,你必须一个接一个地改变它们。面对这种情况,你可以使用 JavaScript 动态添加 <span> 标签来解决这些问题,但这会为 DOM 添加更多的节点,从而导致大量内存占用。

在大多数情况下,最好使用CSS计数器。让我们来看看原因。

CSS计数器简介

CSS计数器是网页范围变量,其值可以使用 CSS 规则更改。

首先,使用 counter-reset 属性设置计数器。list-number 是此处使用的变量名。

  1. div.list {
  2. counter-reset: list-number;
  3. }

接着,使用 counter-increment属性来增加计数器的值。

  1. div.list div {
  2. counter-increment: list-number;
  3. }

现在,每次出现 div.listdiv 元素时,list-number 变量都会增加一。

最后,使用含有设置 content属性和 counter()函数的 :before 伪元素来展示数字。

  1. div.list div:before {
  2. content: counter(list-number);
  3. }

这里是完整代码:

  1. <div class="list">
  2. <div>My first item</div>
  3. <div>My second item</div>
  4. <div>My third item</div>
  5. </div>
  1. div.list {
  2. counter-reset: list-number;
  3. }
  4. /** 可以在:before 为元素中使用 counter-increment **/
  5. div.list div:before {
  6. counter-increment: list-number;
  7. content: counter(list-number);
  8. }

现在我们还没有完全达到目标。让我们对 :before 伪元素进行样式设计,使其看起来更好。

  1. div.list div:before {
  2. counter-increment: list-number;
  3. content: counter(list-number);
  4. margin-right: 10px;
  5. margin-bottom:10px;
  6. width:35px;
  7. height:35px;
  8. display:inline-flex;
  9. align-items:center;
  10. justify-content: center;
  11. font-size:16px;
  12. background-color:#d7385e;
  13. border-radius:50%;
  14. color:#fff;
  15. }

修改起始数字

默认情况下,counter-reset 会将计数器设置为 0。当第一个 counter-increment 被调用后它的起始变为1 可以通过将一个整数作为 counter-reset 函数的第二个参数来设置初始值。

  1. div.list {
  2. counter-reset: list-number 1;
  3. }

如果你想从 0 开始,可以将初始值设置为 -1

  1. div.list {
  2. counter-reset: list-number -1;
  3. }

更改增量值

默认情况下,counter-increment 会使计数器的值增加一。就像 counter-reset 一样,你可以定义 counter-increment 属性的偏移值。

在此示例中,counter-resetlist-number 设置为 0。每次调用 counter-increment 时,list-number 数值都会增加 2,因此,你将会看到列表序为 246

  1. div.list {
  2. counter-reset: list-number;
  3. }
  4. div.list div:before {
  5. counter-increment: list-number 2;
  6. // other styles
  7. }

计数器格式

counter() 函数可以有两个参数:counter-namecounter-format。对于第二个参数,你可以使用任何有效的列表类型值,包括:

  • decimal (e.g., 1, 2, 3…)
  • lower-latin (e.g., a, b, c…)
  • lower-roman (e.g., i, ii, iii…)

默认值为数字。

例如,如果你像我一样科学,你可以使用 lower-greek 小写希腊字母作为编号的值。

  1. div.list div:before {
  2. counter-increment: list-number;
  3. content: counter(list-number, lower-greek);
  4. // ... other styles
  5. }

计数器嵌套

使用嵌套订单列表时,始终以这种格式显示编号:

如果您需要子列表项目的数字编号(例如,1.1),您可以使用具有 counters() 功能的 CSS计数器

  1. <ol>
  2. <li>
  3. My First Item
  4. <ol>
  5. <li>My Nested First Item</li>
  6. <li>My Nested Second Item</li>
  7. </ol>
  8. </li>
  9. <li>My Second Item</li>
  10. </ol>
  1. ol {
  2. list-style-type:none;
  3. counter-reset:list;
  4. }
  5. ol li:before {
  6. counter-increment:list;
  7. content: counters(list, ".") ". ";
  8. }

注意,我们使用的是 counters() 函数,而不是 counter() 函数。

counters() 函数的第二个参数是连接字符串。它还可以有第三个参数来设置格式(例如,希腊数字或罗马数字)。

带标题的嵌套计数器

元素,如 <h1><h2> 不嵌套在文档中。它们以不同的元素出现,但仍代表一种层次结构。下面介绍如何将嵌套数字设置到标题中:

  1. body {
  2. counter-reset:h1;
  3. }
  4. h1 {
  5. counter-reset:h2;
  6. }
  7. h1:before {
  8. counter-increment: h1;
  9. content: counter(h1) ". ";
  10. }
  11. h2:before {
  12. counter-increment:h2;
  13. content: counter(h1) "." counter(h2) ". ";
  14. }

每次找到<h1>时,<h2>计数器都会重置。<h2> 在文档中获得的编号和 <h1> 相关。

Browser support

值得庆幸的是,CSS 计数器自与 CSS2 一起推出以来,得到了浏览器的广泛支持。虽然在内容以外的属性中使用 counter() 函数仍然是实验性的,但你可以毫不犹豫地执行本教程中涵盖的所有例子。

一个简单挑战

您准备好迎接涉及CSS计数器的简单挑战了吗?

使用 CSS计数器10 行代码中显示 11000 及其罗马字符。

如果你被难倒了,下面是你如何做到这一点:

要创建 1000div 元素,可以使用以下内容。

  1. for (var i = 0; i < 1000; i++) {
  2. document.body.appendChild( document.createElement("div") );
  3. }

CSS计数器:

  1. body {
  2. counter-reset:number;
  3. }
  4. div:before {
  5. counter-increment:number;
  6. content: counter(number) " => " counter(number, lower-roman);
  7. }

结论

CSS 计数器在 CSS 中是一个鲜为人知的功能,但您会惊讶于它们派上用场的频率。在此教程中,我们讨论了如何以及何时使用 CSS 计数器,并展示了一些示例。

以下是我们使用的属性列表。  

属性 用法
counter-reset 重置(或创建)给定值计数器(默认0)
counter-increment 通过给定偏移增加给定计数器(默认值 1)
counter(counter-name, counter-format) 从给定格式获取计数器的价值
counters(counter-name, counter-string, counter-format) 从给定格式获取嵌套计数器的价值

 CSS计数器 虽然很酷。但有一件事需要明白的是,所有计数器都是全局性的。如果你在一个有很多 CSS 文件的大型项目中使用,你可能无法找到它们的创建、重置和增量位置。不要过度使用它们,一定要使用描述性名称的计数器,以避免冲突。

一些实战例子

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>CSS计数器</title>
  7. <style>
  8. html {
  9. box-sizing: border-box;
  10. font-size: 62.5%;
  11. }
  12.  
  13. *,
  14. *::before,
  15. *:after {
  16. box-sizing: inherit;
  17. }
  18.  
  19. body {
  20. font-family: Rambla, sans-serif;
  21. font-size: 2rem;
  22. line-height: 1.5;
  23. color: #03c03c;
  24. }
  25.  
  26. h1 {
  27. text-align: center;
  28. }
  29.  
  30. .wrapper {
  31. margin: 0 auto;
  32. width: 85%;
  33. display: -webkit-box;
  34. display: -webkit-flex;
  35. display: -ms-flexbox;
  36. display: flex;
  37. -webkit-justify-content: space-around;
  38. -ms-flex-pack: distribute;
  39. justify-content: space-around;
  40. }
  41.  
  42. @media (max-width: 1100px) {
  43. .wrapper {
  44. -webkit-box-orient: vertical;
  45. -webkit-box-direction: normal;
  46. -webkit-flex-direction: column;
  47. -ms-flex-direction: column;
  48. flex-direction: column;
  49. -webkit-box-align: center;
  50. -webkit-align-items: center;
  51. -ms-flex-align: center;
  52. align-items: center;
  53. }
  54. }
  55.  
  56. ol {
  57. counter-reset: li;
  58. margin: 20px 0;
  59. padding-left: 0;
  60. }
  61.  
  62. ol>li {
  63. position: relative;
  64. margin: 0 0 25px 2em;
  65. padding: 4px 8px 4px 20px;
  66. list-style: none;
  67. }
  68.  
  69. ol>li::before {
  70. content: counter(li);
  71. counter-increment: li;
  72. position: absolute;
  73. top: -2px;
  74. left: -2em;
  75. width: 2em;
  76. margin-right: 8px;
  77. padding: 4px;
  78. font-weight: bold;
  79. text-align: center;
  80. }
  81.  
  82. li ol,
  83. li ul {
  84. margin-top: 6px;
  85. }
  86.  
  87. ol ol li:last-child {
  88. margin-bottom: 0;
  89. }
  90.  
  91. .disc>li::before {
  92. color: white;
  93. background-color: #03c03c;
  94. border-radius: 50%;
  95. }
  96.  
  97. .circle>li::before {
  98. color: #03c03c;
  99. border: solid 2px #03c03c;
  100. border-radius: 50%;
  101. }
  102.  
  103. .angle>li::before {
  104. color: #03c03c;
  105. border-right: solid 3px #03c03c;
  106. border-bottom: solid 3px #03c03c;
  107. }
  108.  
  109. .shadow>li::before {
  110. color: white;
  111. background: #03c03c;
  112. box-shadow: 5px 5px 0 0 greenyellow;
  113. }
  114.  
  115. .rombo>li {
  116. margin-bottom: 25px;
  117. }
  118.  
  119. .rombo>li::before {
  120. color: white;
  121. z-index: 2;
  122. }
  123.  
  124. .rombo>li::after {
  125. position: absolute;
  126. top: -2px;
  127. left: -2em;
  128. width: 2em;
  129. margin-right: 8px;
  130. padding: 4px;
  131. background-color: #03c03c;
  132. height: 2em;
  133. -webkit-transform: rotate(45deg);
  134. -ms-transform: rotate(45deg);
  135. transform: rotate(45deg);
  136. content: '';
  137. z-index: 1;
  138. }
  139.  
  140. .underline>li::before {
  141. border-bottom: solid 3px #03c03c;
  142. }
  143. </style>
  144. </head>
  145.  
  146. <body>
  147. <h1>Styling Ordered List Numbers</h1>
  148. <div class="wrapper">
  149. <ol class="disc">
  150. <li>Tomato</li>
  151. <li>Cucumber</li>
  152. <li>Onion</li>
  153. <li>Pepper</li>
  154. </ol>
  155. <ol class="circle">
  156. <li>Tomato</li>
  157. <li>Cucumber</li>
  158. <li>Onion</li>
  159. <li>Pepper</li>
  160. </ol>
  161. <ol class="angle">
  162. <li>Tomato</li>
  163. <li>Cucumber</li>
  164. <li>Onion</li>
  165. <li>Pepper</li>
  166. </ol>
  167. <ol class="shadow">
  168. <li>Tomato</li>
  169. <li>Cucumber</li>
  170. <li>Onion</li>
  171. <li>Pepper</li>
  172. </ol>
  173. <ol class="rombo">
  174. <li>Tomato</li>
  175. <li>Cucumber</li>
  176. <li>Onion</li>
  177. <li>Pepper</li>
  178. </ol>
  179. <ol class="underline">
  180. <li>Tomato</li>
  181. <li>Cucumber</li>
  182. <li>Onion</li>
  183. <li>Pepper</li>
  184. </ol>
  185. </div>
  186. <a href="https://css-tricks.com/custom-list-number-styling/">更多例子</a>
  187. </body>
  188. </html>

更多优秀案例

https://css-tricks.com/custom-list-number-styling/

到此这篇关于使用CSS计数器美化数字有序列表的实现方法的文章就介绍到这了,更多相关CSS计数器数字有序列表内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

到此这篇关于使用CSS计数器美化数字有序列表的实现方法的文章就介绍到这了,更多相关CSS计数器数字有序列表内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号