css表格制作代码
CSS表格制作基础代码
使用CSS创建表格可以通过<table>标签结合CSS样式实现。以下是一个基础示例:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #e9e9e9;
}
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
<td>Row 1, Cell 3</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
<td>Row 2, Cell 3</td>
</tr>
</table>
</body>
</html>
响应式表格设计
对于移动设备友好的响应式表格,可以添加以下CSS:

@media screen and (max-width: 600px) {
table {
border: 0;
}
table thead {
display: none;
}
table tr {
margin-bottom: 10px;
display: block;
border: 1px solid #ddd;
}
table td {
display: block;
text-align: right;
border-bottom: 1px solid #ddd;
}
table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
高级表格样式
创建更复杂的表格样式,如固定表头、斑马纹和悬停效果:
.table-container {
height: 400px;
overflow-y: auto;
}
table.fixed-header {
position: relative;
}
table.fixed-header thead {
position: sticky;
top: 0;
z-index: 1;
}
.striped-rows tr:nth-child(odd) {
background-color: #f8f8f8;
}
.highlight-column td:hover {
background-color: #ffeb3b;
transition: background-color 0.3s ease;
}
边框样式自定义
创建不同边框样式的表格:

.custom-border {
border: 2px solid #333;
}
.custom-border th, .custom-border td {
border: 1px dashed #666;
}
.no-inner-borders td {
border: none;
border-bottom: 1px solid #eee;
}
表格布局控制
控制表格布局和单元格宽度:
.fixed-layout {
table-layout: fixed;
}
.col-width-30 {
width: 30%;
}
.col-width-20 {
width: 20%;
}
.vertical-align-middle td {
vertical-align: middle;
}
表格标题和页脚
添加表格标题和页脚的样式:
caption {
caption-side: bottom;
font-style: italic;
padding: 10px;
color: #666;
}
tfoot {
font-weight: bold;
background-color: #f0f0f0;
}
这些代码示例涵盖了从基础到高级的CSS表格样式实现,可根据具体需求组合使用或进一步修改。






