用css制作表格
CSS 制作表格的方法
使用 HTML 表格标签 + CSS 样式
通过 HTML 的 <table>、<tr>、<td> 等标签创建表格结构,再用 CSS 美化样式。
<table class="styled-table">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
}
.styled-table th, .styled-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.styled-table th {
background-color: #f2f2f2;
}
.styled-table tr:nth-child(even) {
background-color: #f9f9f9;
}
使用 DIV 模拟表格
通过 CSS 的 display: table 系列属性用 DIV 实现表格布局。
<div class="table">
<div class="row header">
<div class="cell">Header 1</div>
<div class="cell">Header 2</div>
</div>
<div class="row">
<div class="cell">Data 1</div>
<div class="cell">Data 2</div>
</div>
</div>
.table {
display: table;
width: 100%;
border-collapse: collapse;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 1px solid #ddd;
padding: 8px;
}
.header .cell {
background-color: #f2f2f2;
font-weight: bold;
}
响应式表格设计
针对移动设备优化表格显示,添加水平滚动或改变布局。
@media screen and (max-width: 600px) {
.responsive-table {
display: block;
overflow-x: auto;
}
/* 或者转换为卡片式布局 */
.card-table .row {
display: block;
margin-bottom: 10px;
}
.card-table .cell {
display: block;
border: none;
}
}
高级样式技巧
为表格添加悬停效果、斑马纹等增强视觉效果。
/* 悬停高亮 */
.styled-table tr:hover {
background-color: #e6f7ff;
}
/* 斑马纹 */
.styled-table tr:nth-child(odd) {
background-color: #ffffff;
}
.styled-table tr:nth-child(even) {
background-color: #f5f5f5;
}
/* 圆角边框 */
.styled-table {
border-radius: 5px;
overflow: hidden;
}
边框样式控制
精细控制表格边框样式,实现更专业的外观。
.styled-table {
border: 1px solid #ddd;
}
.styled-table th {
border-bottom: 2px solid #aaa;
}
.styled-table td {
border-bottom: 1px solid #eee;
}
每种方法适用于不同场景,传统表格标签适合数据展示,DIV 模拟方式更灵活,响应式设计确保移动设备可用性。







