html table添加序号
时间: 2023-07-01 11:14:30 浏览: 566
在 HTML 表格中添加序号可以使用 CSS 的 `counter-reset` 和 `counter-increment` 属性。具步骤如下:
1. 在 CSS 中设置一个计数器,例如 `table-counter`:
```css
table {
counter-reset: table-counter;
}
```
2. 在表格中的每一个行添加一个伪元素 `::before`,并使用 `counter-increment` 计数器来设置序号:
```css
tr::before {
content: counter(table-counter);
counter-increment: table-counter;
}
```
3. 如果需要对序号进行样式设置,可以在 CSS 中对 `::before` 进行样式设置。
下面是一个完整的例子:
```html
<style>
table {
counter-reset: table-counter;
}
tr::before {
content: counter(table-counter);
counter-increment: table-counter;
margin-right: 10px;
font-weight: bold;
}
</style>
<table>
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>张三</td>
<td>20</td>
</tr>
<tr>
<td></td>
<td>李四</td>
<td>25</td>
</tr>
<tr>
<td></td>
<td>王五</td>
<td>30</td>
</tr>
</tbody>
</table>
```
阅读全文
相关推荐

















