css代码制作表格
基础表格结构
使用HTML的<table>标签配合CSS可以创建表格。以下是一个简单示例:
<table>
<tr>
<th>表头1</th>
<th>表头2</th>
</tr>
<tr>
<td>内容1</td>
<td>内容2</td>
</tr>
</table>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
表格样式优化
通过CSS可以调整表格的视觉效果:
table {
font-family: Arial, sans-serif;
box-shadow: 0 2px 3px rgba(0,0,0,0.1);
}
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f1f1f1;
}
响应式表格设计
对于移动设备,可以通过媒体查询实现响应式表格:

@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 dotted #ccc;
}
table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
高级表格特性
实现固定表头和可滚动内容区域:
<div class="table-container">
<table>
<!-- 表格内容 -->
</table>
</div>
.table-container {
max-height: 400px;
overflow-y: auto;
}
table thead th {
position: sticky;
top: 0;
background: white;
z-index: 10;
}
表格边框样式
自定义表格边框样式:

table {
border: 2px solid #3498db;
border-radius: 5px;
}
th {
border-bottom: 2px solid #3498db;
}
td {
border-bottom: 1px solid #ddd;
}
表格间距控制
调整表格单元格的内外边距:
table {
border-spacing: 0;
}
td, th {
padding: 12px 15px;
}
表格颜色方案
为表格添加配色方案:
table {
background-color: white;
}
th {
background-color: #3498db;
color: white;
}
tr:nth-child(even) {
background-color: #f8f9fa;
}
以上代码示例展示了如何使用CSS创建和美化表格,从基础结构到高级响应式设计,可根据实际需求组合使用这些样式。






