使用 CSS 实现更好的样式
在这一步中,我们将使用 CSS 为上面创建的表格应用样式。
将以下 CSS 代码添加到你的 HTML 文件中:
<!doctype html>
<html>
<head>
<title>Colored Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #4169e1;
color: white;
}
</style>
</head>
<body>
<table border="1">
<caption>
Colored Table
</caption>
<colgroup>
<col style="background-color: gray;" />
<col style="background-color: lightblue;" />
<col style="background-color: lightgreen;" />
</colgroup>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Column 1</td>
<td>Row 1, Column 2</td>
<td>Row 1, Column 3</td>
</tr>
<tr>
<td>Row 2, Column 1</td>
<td>Row 2, Column 2</td>
<td>Row 2, Column 3</td>
</tr>
</tbody>
</table>
</body>
</html>
在你的 index.html
文件中查看完成的代码。你应该能够看到一个三列的表格,每列都有不同的颜色。