css样式制作表格
使用CSS制作表格的基本方法
通过HTML的<table>标签结合CSS可以创建灵活且美观的表格。以下是一个基础示例:
<table class="custom-table">
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
<tr>
<td>内容1</td>
<td>内容2</td>
</tr>
</table>
.custom-table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
.custom-table th, .custom-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.custom-table th {
background-color: #f2f2f2;
color: #333;
}
.custom-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.custom-table tr:hover {
background-color: #eaeaea;
}
响应式表格设计
对于移动设备,可以通过媒体查询实现横向滚动:
@media screen and (max-width: 600px) {
.responsive-table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
}
使用CSS Grid创建表格布局
CSS Grid提供了更灵活的表格布局方式:
.grid-table {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1px;
background-color: #ddd;
}
.grid-cell {
background-color: white;
padding: 10px;
}
.grid-header {
font-weight: bold;
background-color: #f2f2f2;
}
表格样式增强技巧
添加圆角边框和阴影效果:
.enhanced-table {
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
overflow: hidden;
}
.enhanced-table th:first-child {
border-top-left-radius: 5px;
}
.enhanced-table th:last-child {
border-top-right-radius: 5px;
}
表格单元格对齐控制
精确控制单元格内容对齐方式:

.align-table td {
vertical-align: middle;
text-align: center;
}
.number-cell {
text-align: right;
font-family: monospace;
}
这些方法可以根据具体需求组合使用,创建出既美观又功能完善的表格样式。






