I am trying to create an ordered list using CSS and the counter increment. However I only want the first five items to show the counter:
Here is the code I currently have:
var myUnit_1 = 1;
$(".demo1 li").css("counter-increment", "list " + myUnit_1);
.demo1 {
counter-reset: list;
}
.demo1 li:before {
content: counter(list);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="demo1">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li> Do not wish a number</li>
<li> Do not wish a number</li>
<li> Do not wish a number</li>
<li> Do not wish a number</li>
</ul>
I need the list to be kept as one single list and not split in two parts.
2
Answers
You can use
:nth-child()
to apply a selector to a range of elements:Yes – and you don’t need javascript for this.
Start with
<ol>
– it’s the ordered list element.You can use a combination of
li:nth-of-type(n + 6)
and the CSS propertylist-style-type
.Working Example:
Further Reading:
<ol>
– https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol:nth-of-type()
– https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-typelist-style-type
– https://developer.mozilla.org/en-US/docs/Web/CSS/list-style-type