抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

一个典型的HTML表格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<table>
<caption>
A summary of the UK's most famous punk bands
</caption>
<thead>
<tr>
<th scope="col">Band</th>
<th scope="col">Year formed</th>
<th scope="col">No. of Albums</th>
<th scope="col">Most famous song</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Buzzcocks</th>
<td>1976</td>
<td>9</td>
<td>Ever fallen in love (with someone you shouldn't've)</td>
</tr>
<tr>
<th scope="row">The Clash</th>
<td>1976</td>
<td>6</td>
<td>London Calling</td>
</tr>

<!-- several other great bands -->

<tr>
<th scope="row">The Stranglers</th>
<td>1974</td>
<td>17</td>
<td>No More Heroes</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row" colspan="2">Total albums</th>
<td colspan="2">77</td>
</tr>
</tfoot>
</table>

为表格添加样式

间距和布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 间距 */

table {
table-layout: fixed;
width: 100%;
border-collapse: collapse;
border: 3px solid purple;
}

thead th:nth-child(1) {
width: 30%;
}

thead th:nth-child(2) {
width: 20%;
}

thead th:nth-child(3) {
width: 15%;
}

thead th:nth-child(4) {
width: 35%;
}

th,
td {
padding: 20px;
}
  • table-layout属性设置为flxed,使表格的行为在默认情况下更可预测。thead th:nth-child(n)(:nth-child)选择器选择了thead元素中th的第n个子元素,选择了四个不同的标题,并设置百分比宽度

  • border-collapse属性设置为collapse值,他们之间有间隔

评论