h5实现表格样式
实现H5表格样式的方法
使用HTML和CSS创建基础表格
在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th>(表头)标签构建结构。CSS用于调整边框、背景色等样式:
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<tr>
<td>张三</td>
<td>25</td>
</tr>
</table>
table {
width: 100%;
border-collapse: collapse; /* 合并边框 */
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
响应式表格设计

针对移动端,可通过横向滚动或堆叠布局适配小屏幕。使用媒体查询和overflow-x: auto实现横向滚动:
@media screen and (max-width: 600px) {
table {
display: block;
overflow-x: auto;
}
}
使用CSS框架简化开发

Bootstrap等框架提供预定义表格样式。例如Bootstrap的table类:
<table class="table table-striped">
<tr>
<th>标题</th>
</tr>
</table>
高级样式技巧
- 斑马纹效果:通过
:nth-child(even)设置交替行背景色 - 悬停高亮:
:hover伪类实现鼠标悬停效果 - 圆角边框:结合
border-radius美化表格
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #e9e9e9;
}
注意事项
- 避免过度复杂的嵌套表格
- 确保表格语义化,使用
<caption>添加描述 - 测试不同设备的显示效果






