css制作表格例子
基础表格结构
使用HTML的<table>标签配合CSS可以创建结构清晰的表格。以下是一个简单示例:
<table class="basic-table">
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<tr>
<td>张三</td>
<td>25</td>
</tr>
</table>
.basic-table {
width: 100%;
border-collapse: collapse;
}
.basic-table th, .basic-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.basic-table th {
background-color: #f2f2f2;
}
斑马条纹效果
通过CSS的:nth-child()选择器实现交替行颜色:
.striped-table tr:nth-child(even) {
background-color: #f9f9f9;
}
悬停高亮
为表格行添加鼠标悬停效果:
.hover-table tr:hover {
background-color: #e6f7ff;
}
响应式表格
当屏幕宽度不足时,通过CSS使表格可横向滚动:

<div class="table-responsive">
<table>...</table>
</div>
.table-responsive {
overflow-x: auto;
}
边框样式定制
创建不同风格的边框效果:
.custom-border-table {
border: 2px solid #333;
}
.custom-border-table th {
border-bottom: 3px double #333;
}
.custom-border-table td {
border-right: 1px dashed #999;
}
固定表头
实现内容滚动时表头固定的效果:
.fixed-header-table thead th {
position: sticky;
top: 0;
background: white;
box-shadow: 0 2px 2px -1px rgba(0,0,0,0.1);
}
圆角表格
为表格添加现代感的圆角:

.rounded-table {
border-radius: 10px;
overflow: hidden;
}
单元格合并样式
处理跨行或跨列的单元格:
.merged-cells-table td[colspan] {
background-color: #e1f5fe;
text-align: center;
}
表格动画效果
为表格添加过渡动画:
.animated-table tr {
transition: all 0.3s ease;
}
.animated-table tr:hover {
transform: translateX(5px);
}
打印优化
确保表格打印时显示完整:
@media print {
.print-table {
width: 100%;
page-break-inside: avoid;
}
}






