使用 css 计数器生成序数值?
Using css counters to generate ordinal values?
我猜这是不可能的,但想知道是否有人尝试使用 CSS Counters 生成序数(第 1、第 2、第 3 等)并取得成功?
显然这在 JavaScript 中是微不足道的,但希望找到一个仅样式解决方案。
像这样?
body {
counter-reset: section;
}
h3::before {
counter-increment: section;
content: counter(section);
}
h3:nth-child(1)::before {
content: counters(section, "") "st ";
}
h3:nth-child(2)::before {
content: counters(section, "") "nd ";
}
h3:nth-child(3)::before {
content: counters(section, "") "rd ";
}
h3:nth-child(n+4)::before {
content: counters(section, "") "th ";
}
h3:nth-child(10n+1)::before {
content: counters(section, "") "st "
}
h3:nth-child(10n+2)::before {
content: counters(section, "") "nd "
}
h3:nth-child(10n+3)::before {
content: counters(section, "") "rd "
}
h3:nth-child(11)::before {
content: counters(section, "") "th "
}
h3:nth-child(12)::before {
content: counters(section, "") "th "
}
h3:nth-child(13)::before {
content: counters(section, "") "th "
}
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
如果您必须在每个数字上添加 th
,这将很容易。但在这种情况下,您需要更改第 1、2、3、21、22、23、31、32 等...
所以你需要在这里使用第n个子概念。使用 :nth-child
定位元素。
您还需要使用 :not
选择器来不更改第 11、12、13 个元素
body {
margin: 0;
font: 13px Verdana;
}
ul {
margin: 0;
padding: 0;
list-style: none;
counter-reset: item;
}
ul li {
margin-bottom: 5px;
position: relative;
}
ul li:before {
counter-increment: item;
content: counter(item)"th. ";
color: red;
font-weight: bold;
}
ul li:nth-child(10n+1):not(:nth-child(11)):before {
content: counter(item)"st. ";
}
ul li:nth-child(10n+2):not(:nth-child(12)):before {
content: counter(item)"nd. ";
}
ul li:nth-child(10n+3):not(:nth-child(13)):before {
content: counter(item)"rd. ";
}
<ul>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
</ul>
我猜这是不可能的,但想知道是否有人尝试使用 CSS Counters 生成序数(第 1、第 2、第 3 等)并取得成功?
显然这在 JavaScript 中是微不足道的,但希望找到一个仅样式解决方案。
像这样?
body {
counter-reset: section;
}
h3::before {
counter-increment: section;
content: counter(section);
}
h3:nth-child(1)::before {
content: counters(section, "") "st ";
}
h3:nth-child(2)::before {
content: counters(section, "") "nd ";
}
h3:nth-child(3)::before {
content: counters(section, "") "rd ";
}
h3:nth-child(n+4)::before {
content: counters(section, "") "th ";
}
h3:nth-child(10n+1)::before {
content: counters(section, "") "st "
}
h3:nth-child(10n+2)::before {
content: counters(section, "") "nd "
}
h3:nth-child(10n+3)::before {
content: counters(section, "") "rd "
}
h3:nth-child(11)::before {
content: counters(section, "") "th "
}
h3:nth-child(12)::before {
content: counters(section, "") "th "
}
h3:nth-child(13)::before {
content: counters(section, "") "th "
}
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
<h3>Introduction</h3>
<h3>Body</h3>
<h3>Conclusion</h3>
如果您必须在每个数字上添加 th
,这将很容易。但在这种情况下,您需要更改第 1、2、3、21、22、23、31、32 等...
所以你需要在这里使用第n个子概念。使用 :nth-child
定位元素。
您还需要使用 :not
选择器来不更改第 11、12、13 个元素
body {
margin: 0;
font: 13px Verdana;
}
ul {
margin: 0;
padding: 0;
list-style: none;
counter-reset: item;
}
ul li {
margin-bottom: 5px;
position: relative;
}
ul li:before {
counter-increment: item;
content: counter(item)"th. ";
color: red;
font-weight: bold;
}
ul li:nth-child(10n+1):not(:nth-child(11)):before {
content: counter(item)"st. ";
}
ul li:nth-child(10n+2):not(:nth-child(12)):before {
content: counter(item)"nd. ";
}
ul li:nth-child(10n+3):not(:nth-child(13)):before {
content: counter(item)"rd. ";
}
<ul>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
<li>listItem</li>
</ul>