CSS Counters: doesn't reset if there is a wrapper outside OL

170 views Asked by At

I tried to create a custom Ordered List (OL) using "CSS Counters" taking as example this Mozilla article.

I need to slightly modify it, wrapping last OL in a DIV containter named .foo, as shown in this jsFiddle.

<div id='foo'>
    <ol>
      <li>item</li>          <!-- 1     -->
      <li>item</li>          <!-- 2     -->
    </ol>
</div>

ol {
  counter-reset: section;
  list-style-type: none;
}
li::before {
  counter-increment: section;
  content: counters(section,".") " ";
}

Adding wrapper, counter does not reset anymore and numbers continue from 4.1 and 4.2. Why? How to reset new counter even if is wrapped inside a container? Thanks

1

There are 1 answers

1
gopalraju On

Add counter reset to the div and assign a new value to the wrapped ol. Fiddle: http://jsfiddle.net/mbmg52sz/3/

ol{
  counter-reset: sectionA;
  list-style-type: none;
}
li::before {
  counter-increment: sectionA;
  content: counters(sectionA,".") " ";
}

div{
    counter-reset: sectionA;
}

div > ol{
    counter-reset: sectionB;
}

div > ol > li{
    counter-increment: sectionB;
    content: counters(sectionB,".") " ";
}