HTML表格添加背景颜色

82次阅读
没有评论

要为HTML表格添加背景颜色,可以使用CSS样式表中的 background-color 属性。可以将此属性应用于表格、表格行、表头单元格或表格单元格。

下面是一个示例HTML页面,其中显示了如何为表格添加背景颜色:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML Table Background Color Example</title>
    <style>
        table {
            background-color: #f2f2f2;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        td {
            background-color: #f5f5f5;
        }
    </style>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Age</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>John</td>
        <td>Doe</td>
        <td>35</td>
    </tr>
    <tr>
        <td>Jane</td>
        <td>Doe</td>
        <td>28</td>
    </tr>
    </tbody>
</table>
</body>
</html>

在这个例子中,<style> 标签用于添加CSS样式表。table、th 和 td 选择器用于选择表格、表头单元格和表格单元格,并将 background-color 属性设置为不同的颜色值。

请注意,可以使用CSS样式表中的其他属性来自定义表格的外观,例如边框、字体和填充。

正文完
 
评论(没有评论)