表格css制作
基础表格样式设置
使用CSS为HTML表格添加基础样式,如边框、间距和背景色:
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
斑马纹效果
为表格行添加交替颜色增强可读性:
tr:nth-child(even) {
background-color: #f9f9f9;
}
tr:hover {
background-color: #f1f1f1;
}
响应式表格设计
当屏幕宽度不足时让表格可横向滚动:
.table-container {
overflow-x: auto;
}
@media screen and (max-width: 600px) {
table {
display: block;
}
}
表头固定效果
实现滚动时表头固定的效果:
.table-scroll {
height: 300px;
overflow-y: auto;
display: block;
}
thead {
position: sticky;
top: 0;
}
单元格特殊样式
为特定单元格添加特殊样式:
td.highlight {
background-color: #ffeb3b;
font-weight: bold;
}
td.warning {
color: #d32f2f;
background-color: #ffcdd2;
}
表格边框样式进阶
自定义表格边框样式:
table.fancy {
border: 2px solid #6200ea;
}
table.fancy th {
border-bottom: 2px solid #6200ea;
}
table.fancy td {
border-left: none;
border-right: 1px solid #ddd;
}
表格间距调整
控制表格元素间距:
table.spacious {
border-spacing: 10px;
}
table.spacious td {
padding: 15px 20px;
}
圆角表格设计
为表格添加圆角效果:
table.rounded {
border-radius: 10px;
overflow: hidden;
}
table.rounded th:first-child {
border-top-left-radius: 10px;
}
table.rounded th:last-child {
border-top-right-radius: 10px;
}






