表格制作css
基础表格样式
使用CSS为表格添加边框、间距和背景色,提升可读性:
table {
width: 100%;
border-collapse: collapse; /* 合并边框 */
margin: 20px 0;
}
th, td {
border: 1px solid #ddd; /* 单元格边框 */
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2; /* 表头背景色 */
}
斑马纹效果
通过:nth-child()实现交替行颜色,增强视觉层次:
tr:nth-child(even) {
background-color: #f9f9f9;
}
悬停高亮
为表格行添加悬停效果,提升交互体验:
tr:hover {
background-color: #e6f7ff;
}
响应式表格
通过媒体查询和横向滚动处理小屏幕适配:
@media (max-width: 600px) {
table {
display: block;
overflow-x: auto;
}
}
单元格对齐控制
精确控制内容对齐方式:
td.center {
text-align: center;
}
td.right {
text-align: right;
}
边框样式进阶
自定义边框颜色和样式:
table {
border: 2px solid #333;
}
th {
border-bottom: 2px solid #444;
}
表头固定
滚动时保持表头可见(需配合HTML结构):

thead {
position: sticky;
top: 0;
z-index: 10;
}






