css制作套表格
使用CSS制作表格样式
通过CSS可以灵活地定制表格的外观,包括边框、背景色、字体等。以下是一些常见的表格样式设置方法。
基础表格样式

<table class="styled-table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</tbody>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}
.styled-table thead tr {
background-color: #009879;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
.styled-table tbody tr:nth-of-type(even) {
background-color: #f3f3f3;
}
.styled-table tbody tr:last-of-type {
border-bottom: 2px solid #009879;
}
.styled-table tbody tr:hover {
background-color: #f1f1f1;
}
响应式表格设计
对于移动设备,可以通过媒体查询调整表格布局:

@media screen and (max-width: 600px) {
.styled-table {
border: 0;
}
.styled-table thead {
display: none;
}
.styled-table tr {
margin-bottom: 10px;
display: block;
border-bottom: 2px solid #ddd;
}
.styled-table td {
display: block;
text-align: right;
padding-left: 50%;
position: relative;
}
.styled-table td::before {
content: attr(data-label);
position: absolute;
left: 0;
width: 50%;
padding-left: 15px;
font-weight: bold;
text-align: left;
}
}
高级表格效果
添加斑马纹、悬停效果和圆角边框:
.advanced-table {
border-collapse: separate;
border-spacing: 0;
border-radius: 10px;
overflow: hidden;
}
.advanced-table th {
position: sticky;
top: 0;
background: linear-gradient(to bottom, #4a6fdc, #3a5bc7);
color: white;
text-shadow: 1px 1px 1px rgba(0,0,0,0.2);
}
.advanced-table tr:nth-child(even) {
background-color: rgba(74, 111, 220, 0.1);
}
.advanced-table tr:hover {
background-color: rgba(74, 111, 220, 0.2);
transform: scale(1.01);
transition: all 0.2s ease;
}
这些CSS技巧可以帮助创建美观且功能丰富的表格,适应各种网页设计需求。根据具体项目要求,可以调整颜色、间距和交互效果。






