How to style markers in an ordered list that uses the start attribute?

How to add styles to the "start" attribute of HTML?

I cannot target number, even if I apply styles to the entire ordered list tag.

//CODE: <ol start="50"> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol> 



OUTPUT:

  1. Coffee
  2. Tea
  3. Milk
+5
source share
3 answers

You can use the counter-reset and counter-increment properties for this. You should use the :before pseudo-element in list items.

Here is an example.

First you need to run the counter. You can do this using the counter-reset property.

 ol { counter-reset: item 49; list-style: none; } 

The syntax for counter-reset is as follows

counter- reset: none | name number | initial | inherit;

Here we give the name and then the number that you want to start after. Since it starts with 0 by default, you want to select 49, not 50.

Finally, we can start styling our rooms so that they look beautiful. We do this with the :before pseudo-element. In the content property, we can include the value of our counter, which we determined using the counter-reset property above.

 li:before { margin-right: 10px; // Gives us breathing room after number padding: 3px; content: counter(item); background: lightblue; border-radius: 100%; // Gives circle shape color: white; width: 1.2em; text-align: center; display: inline-block; } 

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

It should also be noted that counter-reset and counter-increment only work on IE8 or higher.

https://developer.mozilla.org/en-US/docs/Web/CSS/counter-increment

+8
source

For anyone who sees this question in 2025:

::marker Pseudo-element

This specification defines a new type of pseudo-element, ::marker pseudo-element that is generated by list items to represent an item marker (bullet or number identifying each element).

This pseudo-element can be stylized as a real element and will satisfy the requirements in question. Unfortunately, it currently enjoys browser-free support.

Additional Information:

+4
source

I think you could use some kind of <span> element to separate the style of the number from the style of the list item, something like this:

 <style> ol > li { color: #00F; } ol > li > span { color: #000; } </style> <ol start="50"> <li><span>Coffee</span></li> <li><span>Tea</span></li> <li><span>Milk</span></li> </ol> 
+2
source

Source: https://habr.com/ru/post/1236930/


All Articles