css表格制作表格
CSS表格制作方法
使用CSS创建表格可以通过多种方式实现,以下是几种常见的方法:
使用HTML表格标签结合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;
}
.custom-table th, .custom-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.custom-table th {
background-color: #f2f2f2;
}
.custom-table tr:nth-child(even) {
background-color: #f9f9f9;
}
使用div模拟表格布局
<div class="table">
<div class="row header">
<div class="cell">标题1</div>
<div class="cell">标题2</div>
</div>
<div class="row">
<div class="cell">内容1</div>
<div class="cell">内容2</div>
</div>
</div>
.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 1px solid #ddd;
padding: 8px;
}
.header .cell {
background-color: #f2f2f2;
font-weight: bold;
}
使用CSS Grid布局
<div class="grid-table">
<div class="header">标题1</div>
<div class="header">标题2</div>
<div>内容1</div>
<div>内容2</div>
</div>
.grid-table {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1px;
}
.grid-table > div {
padding: 8px;
border: 1px solid #ddd;
}
.header {
background-color: #f2f2f2;
font-weight: bold;
}
响应式表格设计
@media screen and (max-width: 600px) {
.responsive-table {
display: block;
}
.responsive-table thead {
display: none;
}
.responsive-table tr {
margin-bottom: 10px;
display: block;
border: 1px solid #ddd;
}
.responsive-table td {
display: block;
text-align: right;
border-bottom: 1px solid #ddd;
}
.responsive-table td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
表格样式增强技巧
悬停效果
.table tr:hover {
background-color: #e6e6e6;
}
固定表头
.fixed-header {
position: sticky;
top: 0;
z-index: 10;
}
斑马条纹
.striped tr:nth-child(odd) {
background-color: #f9f9f9;
}
圆角边框

.rounded-table {
border-radius: 8px;
overflow: hidden;
}
这些方法可以根据具体需求选择使用,HTML表格标签适合传统表格数据展示,div模拟表格和CSS Grid更适合现代布局需求,响应式设计则能确保在不同设备上都有良好的显示效果。






