网页表格制作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-responsive {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
@media screen and (max-width: 600px) {
table {
display: block;
width: 100%;
}
}
斑马条纹效果
为表格行添加交替颜色,提高可读性:
tr:nth-child(odd) {
background-color: #ffffff;
}
tr:nth-child(even) {
background-color: #f5f5f5;
}
固定表头
当表格内容较长时,固定表头位置:

thead {
position: sticky;
top: 0;
z-index: 10;
}
th {
box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.1);
}
圆角边框表格
为表格添加现代感的圆角边框:
table {
border-radius: 5px;
overflow: hidden;
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;
}
表格单元格对齐
控制不同列的对齐方式:
.align-left {
text-align: left;
}
.align-center {
text-align: center;
}
.align-right {
text-align: right;
}
悬停高亮效果
增强用户交互体验的悬停效果:

td {
transition: background-color 0.2s ease;
}
td:hover {
background-color: #e3f2fd;
cursor: pointer;
}
紧凑型表格
减少表格内边距,创建更紧凑的布局:
.compact-table th,
.compact-table td {
padding: 4px 8px;
font-size: 0.9em;
}
表格边框样式
自定义表格边框样式:
.custom-border {
border: 2px solid #3498db;
}
.custom-border th,
.custom-border td {
border: 1px solid #bdc3c7;
}
表格标题样式
增强表格标题的视觉效果:
caption {
caption-side: top;
font-size: 1.2em;
font-weight: bold;
margin-bottom: 10px;
color: #333;
text-align: center;
}






