css表格制作考试
基础表格结构
使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签。例如:
<table>
<tr>
<th>姓名</th>
<th>分数</th>
</tr>
<tr>
<td>张三</td>
<td>90</td>
</tr>
</table>
边框与间距控制
通过CSS设置border属性定义边框样式,border-collapse控制单元格边框合并:
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
}
表格样式优化
使用background-color设置表头背景色,text-align调整文本对齐方式:
th {
background-color: #f2f2f2;
text-align: left;
}
tr:hover {
background-color: #e9e9e9;
}
响应式表格设计
添加overflow-x: auto实现小屏幕横向滚动:
div.table-container {
overflow-x: auto;
}
@media screen and (max-width: 600px) {
table { font-size: 14px; }
}
斑马线效果
通过:nth-child(even)选择器实现交替行颜色:
tr:nth-child(even) {
background-color: #f9f9f9;
}
固定表头与滚动体
结合position: sticky实现滚动时表头固定:

thead th {
position: sticky;
top: 0;
background: white;
}
tbody {
height: 200px;
overflow-y: auto;
display: block;
}






