网页表格制作css代码
基础表格样式
以下是一个简单的CSS代码,用于美化HTML表格的边框、间距和字体样式:
table {
width: 100%;
border-collapse: collapse;
font-family: Arial, sans-serif;
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;
}
响应式表格
当屏幕尺寸较小时,表格可以水平滚动:
@media screen and (max-width: 600px) {
.responsive-table {
overflow-x: auto;
display: block;
}
}
斑马条纹效果
交替行颜色可以提高可读性:
.striped-table tr:nth-child(odd) {
background-color: #f8f8f8;
}
.striped-table tr:nth-child(even) {
background-color: #ffffff;
}
悬停高亮效果
鼠标悬停时行高亮显示:

.hover-table tr:hover {
background-color: #e6f7ff;
transition: background-color 0.3s ease;
}
圆角边框表格
为表格添加圆角边框:
.rounded-table {
border-radius: 5px;
overflow: hidden;
}
.rounded-table th:first-child {
border-top-left-radius: 5px;
}
.rounded-table th:last-child {
border-top-right-radius: 5px;
}
固定表头
滚动时表头保持固定:
.fixed-header {
position: sticky;
top: 0;
z-index: 10;
box-shadow: 0 2px 2px -1px rgba(0,0,0,0.1);
}
紧凑型表格
减少单元格内边距:

.compact-table th,
.compact-table td {
padding: 4px 8px;
font-size: 0.9em;
}
彩色表头
为表头添加背景色:
.colored-header th {
background-color: #4CAF50;
color: white;
}
单元格对齐方式
控制单元格内容对齐:
.text-center td {
text-align: center;
}
.text-right td {
text-align: right;
}
.vertical-middle td {
vertical-align: middle;
}
边框样式变体
不同风格的边框:
.dotted-border td {
border: 1px dotted #ccc;
}
.no-border td {
border: none;
border-bottom: 1px solid #eee;
}
这些CSS代码可以单独使用或组合使用,根据具体需求调整参数值如颜色、间距等。使用时只需将相应的class添加到HTML表格元素上即可应用对应的样式效果。






