1 回答

TA貢獻1853條經驗 獲得超6個贊
我添加了一些演示 CSS 來展示如何解決這個引用問題。單擊“運行”按鈕查看其運行情況。
盡管您可以將類添加到每個標記上,但指定所需<td>的子標記會更容易。tr為此,您可以使用偽類first-child、nth-child和last-child。我在您的示例中添加了一些隨機格式 - 我將把具體的格式定義留給您。
您當然可以用于nth-child所有這些。通常,編程中的枚舉從零開始(“零索引”),但此類是一索引的。因此,first-child我可以用fornth-child(1)來代替。以類似的方式,last-child可以在這里寫為nth-child(3),但在某些情況下可能不太容易維護,因為如果你想插入一列,你也必須調整最右邊一列的 CSS。
<!DOCTYPE html>
<html>
<head>
<style>
table {
? border-collapse: collapse;
? width: 100%;
}
th, td {
? padding: 8px;
? text-align: left;
? border-bottom: 1px solid #ddd;
}
table tr td:first-child {
? ? font-weight: bold;
? ? color: red;
}
table tr td:nth-child(2) {
? ? font-size: 0.8em;
? ? color: purple;
}
table tr td:last-child {
? ? font-style: italic;
? ? color: green;
}
</style>
</head>
<body>
<h2>Bordered Table Dividers</h2>
<p>Add the border-bottom property to th and td for horizontal dividers:</p>
<table>
? <tr>
? ? <th>Firstname</th>
? ? <th>Lastname</th>
? <th>Savings</th>
? </tr>
? <tr>
? ? <td>Peter</td>
? ? <td>Griffin</td>
? ? <td>$100</td>
? </tr>
? <tr>
? ? <td>Lois</td>
? ? <td>Griffin</td>
? ? <td>$150</td>
? </tr>
? <tr>
? ? <td>Joe</td>
? ? <td>Swanson</td>
? ? <td>$300</td>
? </tr>
? <tr>
? ? <td>Cleveland</td>
? ? <td>Brown</td>
? ? <td>$250</td>
? </tr>
</table>
</body>
</html>
- 1 回答
- 0 關注
- 153 瀏覽
添加回答
舉報