网页制作css表格
CSS表格基础样式
使用CSS可以美化HTML表格,使其更符合现代网页设计风格。以下是一个基础表格样式示例:
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
font-family: Arial, sans-serif;
}
th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
tr:hover {
background-color: #f5f5f5;
}
响应式表格设计
针对移动设备,可以添加响应式设计:
@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;
}
}
斑马条纹效果
为表格添加交替行颜色增强可读性:
tr:nth-child(even) {
background-color: #f9f9f9;
}
表格边框样式
自定义表格边框样式:
table {
border: 1px solid #ddd;
}
th, td {
border-right: 1px solid #ddd;
}
th:last-child, td:last-child {
border-right: none;
}
表格标题样式
为表格添加标题和说明:
.caption {
caption-side: top;
text-align: center;
padding: 10px;
font-size: 1.2em;
font-weight: bold;
color: #333;
}
.description {
font-size: 0.9em;
color: #666;
margin-bottom: 15px;
}
固定表头表格
对于长表格,可以固定表头:
.table-container {
height: 400px;
overflow-y: auto;
}
thead th {
position: sticky;
top: 0;
z-index: 10;
background-color: #f2f2f2;
}
表格单元格对齐
控制单元格内容对齐方式:
.text-left {
text-align: left;
}
.text-center {
text-align: center;
}
.text-right {
text-align: right;
}
.vertical-middle {
vertical-align: middle;
}
表格阴影和悬停效果
添加视觉效果增强交互体验:
table {
box-shadow: 0 2px 3px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
table:hover {
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
这些CSS样式可以根据实际需求组合使用或单独应用,创建出符合网站整体风格的表格设计。







