css 表格制作
CSS 表格制作基础
使用 CSS 可以美化 HTML 表格,使其更具视觉吸引力。以下是一个基础示例:
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
响应式表格设计
对于移动设备,可以添加水平滚动:
.table-container {
overflow-x: auto;
}
@media screen and (max-width: 600px) {
table {
width: auto;
}
}
表格悬停效果
增加交互性,当鼠标悬停时改变行背景色:
tr:hover {
background-color: #e9e9e9;
}
斑马线表格样式
交替行颜色提高可读性:

tr:nth-child(odd) {
background-color: #ffffff;
}
tr:nth-child(even) {
background-color: #f5f5f5;
}
固定表头表格
长表格滚动时保持表头固定:
.table-scroll {
height: 300px;
overflow-y: auto;
}
thead {
position: sticky;
top: 0;
background-color: white;
z-index: 1;
}
表格边框样式
自定义表格边框样式:
table {
border: 2px solid #333;
}
th, td {
border: 1px solid #aaa;
border-style: dotted;
}
单元格对齐方式
控制单元格内容对齐:

td {
vertical-align: middle;
text-align: center;
}
表格标题样式
为表格添加标题并设置样式:
<caption>Monthly Sales</caption>
caption {
caption-side: bottom;
font-size: 0.9em;
padding: 5px;
color: #666;
}
合并单元格样式
处理跨行或跨列的单元格:
<td colspan="2">Merged Cell</td>
td[colspan] {
background-color: #e1f5fe;
text-align: center;
}
表格阴影效果
为表格添加深度感:
table {
box-shadow: 0 2px 3px rgba(0,0,0,0.1);
}






