文章

6 表格标签

6 表格标签

表格(table)以行(row)和列(column)的形式展示数据。

table,caption

<table> 是一个块级容器标签,所有表格内容都要放在这个标签里面。

1
2
3
<table>
  ... ...
</table>

<caption> 总是 <table> 里面的第一个子元素,表示表格的标题。该元素是可选的。

1
2
3
<table>
  <caption>示例表格</caption>
</table>

thead、tbody、tfoot

<thead>、<tbody>、<tfoot> 都是块级容器元素,且都是 <table> 的一级子元素,分别表示表头、表体和表尾。

1
2
3
4
5
<table>
  <thead>... ...</thead>
  <tbody>... ...</tbody>
  <tfoot>... ...</tfoot>
</table>

这三个元素都是可选的。如果使用了 <thead>,那么 <tbody>和<tfoot> 一定在 <thead> 的后面。如果使用了 <tbody>,那么 <tfoot> 一定在 <tbody> 后面。

大型表格内部可以使用多个 <tbody>,表示连续的多个部分。

colgroup,col

<colgroup>是<table> 的一级子元素,用来包含一组列的定义。<col><colgroup> 的子元素,用来定义表格的一列。

1
2
3
4
5
6
7
<table>
  <colgroup>
    <col>
    <col>
    <col>
  </colgroup>
</table>

tr

<tr> 标签表示表格的一行(table row)。如果表格有 <thead>、<tbody>、<tfoot>,那么 <tr> 就放在这些容器元素之中,否则直接放在 <table> 的下一级。

1
2
3
4
5
<table>
  <tr>...</tr>
  <tr>...</tr>
  <tr>...</tr>
</table>

th,td

<th>和<td> 都用来定义表格的单元格。其中,<th> 是标题单元格,<td> 是数据单元格。

1
2
3
4
5
6
7
8
9
10
11
<table>
  <tr>
    <th>学号</th><th>姓名</th>
  </tr>
  <tr>
    <td>001</td><td>张三</td>
  </tr>
  <tr>
    <td>002</td><td>李四</td>
  </tr>
</table>
本文由作者按照 CC BY 4.0 进行授权