css supports counter variable 1) that can be used in a property value (for instance in a content property to add numbering to:
For instance:
counter-set is not yet supported by Apple Safari
ol { counter-set: level1 }
ol > ol { counter-set: level2 }
li { display: block }
ol li::before { content: counter(level1) ". "; counter-increment: level1 }
ol ol li::before { content: counter(level1) "." counter(level2, upper-roman) " "; counter-increment: level2 }
<ol>
<li> first item </li>
<li> second item </li>
<ol>
<li> second level, first item </li>
<li> second level, first item </li>
</ol>
<li> third item </li>
<li> fourth item </li>
</ol>
This example shows a way to number chapters and sections with “Chapter 1”, “1.1”, “1.2”, etc.
body {
counter-set: chapter 0 section 0; /* scope is on the body */
}
h1::before {
content: "Chapter " counter(chapter) ". ";
counter-increment: chapter 1; /* Add 1 to chapter */
counter-set: section 0;
}
h2::before {
content: counter(chapter) "." counter(section) " ";
counter-increment: section;
}
<h1>Chapter</h1>
<h2>Section</h2>
<h2>Section</h2>
<h2>Section</h2>
<h1>Chapter</h1>
<h2>Section</h2>
<h2>Section</h2>
<h2>Section</h2>
Nested counter 4) are pretty wild if you don't understand their internal.
Counter:
For instance:
body > ol { counter-reset: item }
body > ol > ol > ol { counter-reset: item }
li { display: block }
li::before { content: counter(item) ". "; counter-increment: item }
<ol> <!-- item0 is created, set to 0 -->
<li> item0 is incremented to 1 </li>
<li> item0 is incremented to 2 </li>
<ol>
<li> item0 is incremented to 3 </li>
<li> item0 is incremented to 4 </li>
<ol> <!-- item1 is created, set to 0, nested in item0 -->
<li> item1 is incremented to 1 </li>
<li> item1 is incremented to 2 </li>
</ol>
<li> item1 is incremented to 3 and not item0 ! because their was no call to counter-set or reset</li>
</ol>
<li> item0 is incremented to 5 </li>
<li> item0 is incremented to 6 </li>
</ol>
<ol> <!-- item2 is created, set to 0 -->
<li> item2 is incremented to 1 </li>
<li> item2 is incremented to 2 </li>
</ol>