制作表格css
基础表格样式
使用CSS为表格添加基础样式,包括边框、间距和字体:
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
}
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;
}
响应式表格
针对小屏幕设备添加横向滚动功能:

@media screen and (max-width: 600px) {
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
}
表头固定
创建滚动时固定表头的效果:
thead {
position: sticky;
top: 0;
z-index: 10;
}
高级样式选项
添加圆角边框和阴影效果:

table {
border-radius: 5px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
th:first-child {
border-top-left-radius: 5px;
}
th:last-child {
border-top-right-radius: 5px;
}
单元格状态样式
为不同状态的单元格添加特殊样式:
td.success {
background-color: #dff0d8;
color: #3c763d;
}
td.warning {
background-color: #fcf8e3;
color: #8a6d3b;
}
td.danger {
background-color: #f2dede;
color: #a94442;
}
表格标题样式
为表格标题添加特殊格式:
caption {
font-size: 1.5em;
margin-bottom: 10px;
font-weight: bold;
text-align: center;
}






