制作表格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 {
border: 0;
}
table thead {
display: none;
}
table tr {
margin-bottom: 10px;
display: block;
border: 1px solid #ddd;
}
table td {
display: block;
text-align: right;
border-bottom: 1px dotted #ccc;
}
table td:before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
高级样式选项
添加圆角边框和阴影效果:
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;
}
固定表头实现
当表格内容过长时固定表头:

.table-container {
height: 400px;
overflow-y: auto;
}
thead th {
position: sticky;
top: 0;
z-index: 10;
}
单元格对齐方式
控制不同列的对齐方式:
.align-left {
text-align: left;
}
.align-center {
text-align: center;
}
.align-right {
text-align: right;
}
.valign-top {
vertical-align: top;
}
.valign-middle {
vertical-align: middle;
}
.valign-bottom {
vertical-align: bottom;
}
表格边框样式
自定义边框样式和颜色:
table {
border: 2px solid #333;
}
th {
border-bottom: 3px solid #666;
}
td {
border-left: none;
border-right: none;
}
表格标题样式
为表格添加标题和说明:
caption {
caption-side: top;
font-size: 1.2em;
font-weight: bold;
padding: 10px;
text-align: center;
color: #333;
background-color: #f8f8f8;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}






