制作css表格
基础CSS表格结构
使用HTML的<table>标签创建表格骨架,搭配CSS控制样式。以下是一个基础示例:
<table class="styled-table">
<thead>
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
</thead>
<tbody>
<tr>
<td>内容A</td>
<td>内容B</td>
</tr>
</tbody>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
font-size: 0.9em;
font-family: sans-serif;
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;
}
响应式表格设计
当屏幕宽度不足时,通过媒体查询将表格转换为卡片布局:

@media screen and (max-width: 600px) {
.styled-table {
border: 0;
}
.styled-table thead {
display: none;
}
.styled-table tr {
margin-bottom: 15px;
display: block;
border: 1px 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: 15px;
width: 45%;
padding-right: 10px;
font-weight: bold;
text-align: left;
}
}
悬停效果增强
为表格行添加悬停交互效果:
.styled-table tbody tr:hover {
background-color: #e8f4fc;
cursor: pointer;
transform: scale(1.01);
transition: all 0.3s ease;
}
斑马纹配色方案
使用CSS变量实现可定制的斑马纹效果:

:root {
--primary-bg: #3498db;
--even-row: #f8f9fa;
--odd-row: #ffffff;
}
.styled-table tbody tr:nth-child(odd) {
background-color: var(--odd-row);
}
.styled-table tbody tr:nth-child(even) {
background-color: var(--even-row);
}
边框样式定制
创建双线边框或圆角边框:
.styled-table {
border: 1px double #333;
}
.styled-table th:first-child {
border-top-left-radius: 10px;
}
.styled-table th:last-child {
border-top-right-radius: 10px;
}
固定表头滚动表格
实现固定表头、可滚动内容区域的表格:
.scrollable-table {
height: 300px;
overflow-y: auto;
display: block;
}
.scrollable-table thead {
position: sticky;
top: 0;
z-index: 1;
}






