I'm guessing this isn't possible, but wondering if anybody has attempted to produce ordinal numbers (1st, 2nd, 3rd etc) using CSS Counters with any success?
Obviously this would be trivial in JavaScript, but was hoping to find a style-only solution.
To use a counter it must first be initialized to a value with the counter-reset property. The counter's value can then be increased or decreased using counter-increment property. The current value of a counter is displayed using the counter() or counters() function, typically within a pseudo-element content property.
The counter-increment property increases or decreases the value of one or more CSS counters. The counter-increment property is usually used together with the counter-reset property and the content property. Default value: none.
It will be easy if you have to add th
on every number. But in this case you will need to change the 1st, 2nd, 3rd, 21st, 22nd, 23rd, 31st, 32nd, etc...
So you will need to use nth child concept here. Use :nth-child
to target the element.
You will also need to use :not
selector to not change the 11th, 12th, 13th element
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>
Like this?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With