Multiple CSS counters not working as expected -
i trying create multiple levels of counters in html table, not working expected. first counter works ok, next counters not work. somehow counters not incrementing or reset wrongly?
the code:
<html> <head> <title>counter demo</title> <style> table.tasksteps { counter-reset: tasksteps; } td.taskstep { counter-reset: risks; counter-increment: tasksteps; } td.risk { counter-reset: measures; counter-increment: risks; } td.measure { counter-increment: measures; } td.taskstep:before { content: counter(tasksteps) '. '; } td.risk:before { content: counter(tasksteps) '.' counter(risks) '. '; } td.measure:before { content: counter(tasksteps) '.' counter(risks) '.' counter(measures) '. '; } </style> </head> <body> <table class="tasksteps"> <thead> <tr> <td>taakstep</td> <td>risk</td> <td>measure</td> </tr> </thead> <tbody> <tr> <td class="taskstep t1">step 1</td> <td></td> <td></td> </tr> <tr> <td class="taskstep t2">step 2</td> <td class="risk">risk 1</td> <td></td> </tr> <tr> <td class="t3"></td> <td class="risk">risk 2</td> <td></td> </tr> <tr> <td class="taskstep t2">step 3</td> <td class="risk">risk 3</td> <td></td> </tr> <tr> <td class="taskstep t2">step 4</td> <td class="risk">risk 4</td> <td></td> </tr> <tr> <td class="t4"></td> <td class="risk">risk 5</td> <td class="measure">measure 1</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 2</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 3</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 4</td> </tr> <tr> <td class="t3"></td> <td class="risk">risk 6</td> <td></td> </tr> <tr> <td class="t4"></td> <td class="risk">risk 7</td> <td class="measure">measure 5</td> </tr> </tbody> </table> </body> </html>
this gives output:
there 2 problems here:
the direct parent of first counted element should contain other counted elements. in case, first counted element
td
in row, parenttr
but other counted elements have own parents oftr
(which siblings of parent of first counted element). fix think have set class ontr
, count there.the
counter-reset
,counter-increment
can overridden, means if @ 1tr
, need usecounter-reset
,counter-increment
more 1 counter variable, need put them on same declaration (space-separated)counter-reset
,counter-increment
.
from 2 points above, here code should be:
html:
<table class="tasksteps"> <thead> <tr> <td>taakstep</td> <td>risk</td> <td>measure</td> </tr> </thead> <tbody> <tr class="taskstep"> <td class="taskstep t1">step 1</td> <td></td> <td></td> </tr> <tr class="taskstep risk"> <td class="taskstep t2">step 2</td> <td class="risk">risk 1</td> <td></td> </tr> <tr class="risk"> <td class="t3"></td> <td class="risk">risk 2</td> <td></td> </tr> <tr class="taskstep risk"> <td class="taskstep t2">step 3</td> <td class="risk">risk 3</td> <td></td> </tr> <tr class="taskstep risk"> <td class="taskstep t2">step 4</td> <td class="risk">risk 4</td> <td></td> </tr> <tr class="risk"> <td class="t4"></td> <td class="risk">risk 5</td> <td class="measure">measure 1</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 2</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 3</td> </tr> <tr> <td class="t5"></td> <td></td> <td class="measure">measure 4</td> </tr> <tr class="risk"> <td class="t3"></td> <td class="risk">risk 6</td> <td></td> </tr> <tr class="risk"> <td class="t4"></td> <td class="risk">risk 7</td> <td class="measure">measure 5</td> </tr> </tbody> </table>
css:
table.tasksteps { counter-reset: tasksteps; } tr.taskstep { counter-reset: risks; counter-increment: tasksteps; } tr.risk { counter-reset: measures; counter-increment: risks; } tr.taskstep.risk { counter-reset: risks measures; counter-increment: tasksteps risks; } td.measure { counter-increment: measures; } td.taskstep:before { content: counter(tasksteps) '. '; } td.risk:before { content: counter(tasksteps) '.' counter(risks) '. '; } td.measure:before { content: counter(tasksteps) '.' counter(risks) '.' counter(measures) '. '; }
Comments
Post a Comment