怎么制作css表格
使用HTML和CSS创建表格
HTML提供<table>标签用于创建表格结构,CSS用于控制样式。以下是一个基础示例:
<table class="styled-table">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Cell 1</td>
<td>Row 1 Cell 2</td>
</tr>
<tr>
<td>Row 2 Cell 1</td>
<td>Row 2 Cell 2</td>
</tr>
</tbody>
</table>
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 25px 0;
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;
}
响应式表格设计
当屏幕宽度不足时,可通过CSS将表格转换为卡片布局:

@media screen and (max-width: 600px) {
.styled-table thead {
display: none;
}
.styled-table tr {
display: block;
margin-bottom: 15px;
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;
}
.styled-table tbody tr.active-row {
font-weight: bold;
color: #009879;
}
高级样式技巧
使用CSS变量实现主题切换:
:root {
--table-primary: #009879;
--table-hover: #e8f4fc;
--table-border: #dddddd;
}
.styled-table thead tr {
background-color: var(--table-primary);
}
.styled-table tbody tr:hover {
background-color: var(--table-hover);
}
边框样式优化
实现双线边框或自定义边框样式:
.styled-table {
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
}
.styled-table td {
border-right: 1px dashed #ccc;
}
.styled-table td:last-child {
border-right: none;
}






