2

I've seen that you can use various types of counters, as described in the accepted answer to this question: CSS Pseudo Element Counters: can you increment an alphabet letter "a", "b", "c", etc instead of a number?

However, is it possible to increment by a float, e.g. 0.5? This would give a list of, say, 3.0, 3.5, 4.0, 4.5, etc.

Here is what I've tried to do, to no avail:

.values li:before {
  content: counter(step);
  counter-increment: step 0.5;
}

The value after step is not recognized if it's a float (only if integer).

If this is not possible, is there another method I could use to programmatically set the <li> elements as desired?

Thank you.

3 Answers3

3

You can fake it with multiple rules and nth-child etc

ul {
  counter-reset: numbers;
  list-style-type: none;
}

li:before {
  padding-right: 1em;
}

li:nth-of-type(2n+1)::before {
  counter-increment: numbers;
}

li:nth-child(odd)::before {
  content: counters(numbers, "") ".0";
}

li:nth-child(even)::before {
  content: counters(numbers, "") ".5";
}
<ul>
  <li>Lorem</li>
  <li>Lorem</li>
  <li>Lorem</li>
  <li>Lorem</li>
  <li>Lorem</li>
  <li>Lorem</li>
</ul>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

CSS probably isn't the best thing you can use for this. Try using JavaScript:

var children = document.getElementById("values").children;
var value = 1;
for(var i = 0; i < children.length; i++) {
  var child = children[i];
  child.innerHTML = value + " " + child.innerHTML;
  value += 0.5;
}
<ul id="values">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
Okx
  • 353
  • 3
  • 23
  • This is a good way to do it. For reference, the way I ended up doing this (I'm using React) is this: ``` {values.map(value => { return (
  • {value}
  • ) })} ``` Where `values` is an array containing the steps (created with a for loop starting at the min value and ending at the max value, adding the step value each time). – Jackson Holiday Wheeler Jun 20 '17 at 10:59