Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I style numbering an ordered list that has a start attribute value?

Here's the problem:

I have an ol li ordered list with a start attribute like so:

    .custom {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    
    .custom li {
      counter-increment: step-counter;
      margin-bottom: 10px;
    }
    
    .custom li::before {
      content: counter(step-counter);
      margin-right: 5px;
      font-size: 80%;
      background-color: rgb(0,200,200);
      color: white;
      font-weight: bold;
      padding: 3px 8px;
      border-radius: 3px;
    }
    <ol start="6" class="custom">
      <li>This is the sixth item</li>
      <li>This is the seventh item</li>
      <li>This is the eighth item</li>
      <li>This is the ninth item</li>
      <li>This is the tenth item</li>
    </ol>

I get the following output on the browser:

List with 5 items; the first one has the number 1, but should have number 6

Is it possible to serialize the list-style numbering on an ordered list using the value in start attribute instead of 1? No JavaScript can be used for this though.

like image 795
Marvin Danig Avatar asked Mar 16 '19 20:03

Marvin Danig


People also ask

Can you change the style of numbers in an ordered list?

Answer: Type attribute allows us to change the style of numbers in an ordered list. Explanation: The < li > tag includes two attributes – type and value. The type attribute is used to modify the order numbering in the list item.

Which attribute is used to change the start of numbering in an ordered list?

The start attribute specifies the start value of the first list item in an ordered list. This value is always an integer, even when the numbering type is letters or romans. E.g., to start counting list items from the letter "c" or the roman number "iii", use start="3".

Which attribute can changes the numbering style of a list?

Answer: Type attribute allows us to change the style of numbers in an ordered list..

What HTML type values can be used for numbering ordered list?

<ol>: The Ordered List element. The <ol> HTML element represents an ordered list of items — typically rendered as a numbered list.


2 Answers

You can simulate this using CSS variable that you set instead of start and use it to reset the counter. For semantic purpose you can also keep start attribute.

.custom {
  margin: 0;
  padding: 0;
  list-style-type: none;
  counter-reset: step-counter calc(var(--start) - 1);
}

.custom li {
  counter-increment: step-counter;
  margin-bottom: 10px;
}

.custom li::before {
  content: counter(step-counter);
  margin-right: 5px;
  font-size: 80%;
  background-color: rgb(0, 200, 200);
  color: white;
  font-weight: bold;
  padding: 3px 8px;
  border-radius: 3px;
}
<ol style="--start:6" start="6" class="custom">
  <li>This is the sixth item</li>
  <li>This is the seventh item</li>
  <li>This is the eighth item</li>
  <li>This is the ninth item</li>
  <li>This is the tenth item</li>
</ol>
like image 72
Temani Afif Avatar answered Sep 19 '22 12:09

Temani Afif


li tag have no access to parent attribute.

This is the best way i saw, using content: attr()

    .custom {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    
    .custom li {
      counter-increment: step-counter;
      margin-bottom: 10px;
    }
    
    .custom li::before {
      content: attr(data-idx);
      margin-right: 5px;
      font-size: 80%;
      background-color: rgb(0,200,200);
      color: white;
      font-weight: bold;
      padding: 3px 8px;
      border-radius: 3px;
    }
    <ol class="custom">
      <li data-idx="6">This is the sixth item</li>
      <li data-idx="7">This is the seventh item</li>
      <li data-idx="8">This is the eighth item</li>
      <li data-idx="9">This is the ninth item</li>
      <li data-idx="10">This is the tenth item</li>
    </ol>
like image 37
Arthur Avatar answered Sep 18 '22 12:09

Arthur