css制作表格例子
基础表格样式
使用CSS创建一个简单的表格,包含边框和基本样式:
<table class="basic-table">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</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;
}
斑马条纹表格
添加交替行颜色提高可读性:
.striped-table tr:nth-child(even) {
background-color: #f9f9f9;
}
悬停高亮效果
为表格行添加悬停效果:

.hover-table tr:hover {
background-color: #e6e6e6;
}
响应式表格
在小屏幕上使表格可横向滚动:
<div class="table-responsive">
<table>
<!-- 表格内容 -->
</table>
</div>
.table-responsive {
overflow-x: auto;
}
固定表头表格
创建滚动时表头固定的表格:

<div class="fixed-header-container">
<table>
<thead>
<tr>
<th>固定表头1</th>
<th>固定表头2</th>
</tr>
</thead>
<tbody>
<!-- 表格内容 -->
</tbody>
</table>
</div>
.fixed-header-container {
height: 300px;
overflow-y: auto;
}
.fixed-header-container thead th {
position: sticky;
top: 0;
background-color: #f8f8f8;
z-index: 10;
}
圆角边框表格
为表格添加现代圆角设计:
.rounded-table {
border-radius: 8px;
overflow: hidden;
}
.rounded-table th:first-child {
border-top-left-radius: 8px;
}
.rounded-table th:last-child {
border-top-right-radius: 8px;
}
单元格对齐控制
精确控制单元格内容对齐方式:
.align-table td {
vertical-align: middle;
text-align: center;
}
.align-table th {
text-align: right;
}






