简体   繁体   English

创建具有增量列数的html表

[英]Create html table with increment number of column

I have been success to make increment no on column.我已经成功地在列上增加了 no。

Here my code这是我的代码

 table { counter-reset: rowNumber; } table tr { counter-increment: rowNumber; } table tr td:first-child::before { content: counter(rowNumber); min-width: 1em; margin-right: 0.5em; }
 <table border="1"> <tr> <th>No</th> <th>Name</th> </tr> <tr> <td></td> <td>jhon</td> </tr> <tr> <td></td> <td>lucy</td> </tr> <tr> <td></td> <td>joe</td> </tr> </table>

在此处输入图片说明

but i want to make table number like image on below但我想制作如下图所示的表号

在此处输入图片说明

You must reset your counter on the second <tr>您必须在第二个<tr>重置您的计数器

 table tr:nth-child(2){ counter-reset: rowNumber; } table tr { counter-increment: rowNumber; } table tr td:first-child::before { content: counter(rowNumber); min-width: 1em; margin-right: 0.5em; }
 <table border="1"> <tr> <th>#</th> <th>Name</th> </tr> <tr> <td></td> <td>jhon</td> </tr> <tr> <td></td> <td>lucy</td> </tr> <tr> <td></td> <td>joe</td> </tr> </table>

On the table element your counter is 1, on the first row it is incremented to 2. Hence starting at 2. You need to do the reset on the first tr .table元素上,您的计数器是 1,在第一行它增加到 2。因此从 2 开始。您需要在第一个tr上进行重置。

table tr {
    counter-increment: rowNumber;
}
table tr:first {
    counter-reset: rowNumber;
}

Reset the counter at the second tr.在第二个 tr 重置计数器。

 tr:nth-child(2) { counter-reset: rowNumber; } table tr { counter-increment: rowNumber; } table tr td:first-child::before { content: counter(rowNumber); min-width: 1em; margin-right: 0.5em; }
 <table border="1"> <tr> <th>No</th> <th>Name</th> </tr> <tr> <td></td> <td>jhon</td> </tr> <tr> <td></td> <td>lucy</td> </tr> <tr> <td></td> <td>joe</td> </tr> </table>

Here is the code to fix your issue.这是解决您的问题的代码。 In your code you are counting the row number which is row number 2 in you case.在您的代码中,您正在计算行号,在您的情况下为行号 2。 so it is starting from 2.所以它从2开始。

One of the solution to your problem is use thead and tbody tags and increment the row counter from tbody as done it in below solution.您的问题的解决方案之一是使用theadtbody标签并按照以下解决方案从tbody增加行计数器。

 table tbody { counter-reset: rowNumber; } table tbody tr { counter-increment: rowNumber; } table tr td:first-child::before { content: counter(rowNumber); min-width: 1em; margin-right: 0.5em; }
 <table border="1"> <thead> <tr> <th>No</th> <th>Name</th> </tr> </thead> <tbody> <tr> <td></td> <td>jhon</td> </tr> <tr> <td></td> <td>lucy</td> </tr> <tr> <td></td> <td>joe</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM