怎样制作css表格
使用HTML和CSS创建表格
HTML中的<table>元素用于创建表格,结合CSS可以自定义样式。以下是一个基础示例:
<table class="custom-table">
<thead>
<tr>
<th>标题1</th>
<th>标题2</th>
</tr>
</thead>
<tbody>
<tr>
<td>数据1</td>
<td>数据2</td>
</tr>
</tbody>
</table>
.custom-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
.custom-table th,
.custom-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
.custom-table th {
background-color: #f2f2f2;
}
.custom-table tr:nth-child(even) {
background-color: #f9f9f9;
}
响应式表格设计
对于移动设备,可以通过媒体查询调整表格显示方式:
@media screen and (max-width: 600px) {
.custom-table {
display: block;
}
.custom-table thead {
display: none;
}
.custom-table tr {
margin-bottom: 10px;
display: block;
border: 1px solid #ddd;
}
.custom-table td {
display: block;
text-align: right;
}
.custom-table td::before {
content: attr(data-label);
float: left;
font-weight: bold;
}
}
添加悬停效果
增强用户体验的悬停效果:
.custom-table tr:hover {
background-color: #e6e6e6;
transition: background-color 0.3s ease;
}
表格边框样式自定义
创建不同风格的边框:
.custom-table {
border: 2px solid #333;
border-radius: 5px;
overflow: hidden;
}
.custom-table th {
border-bottom: 2px solid #333;
}
使用CSS Grid布局表格
替代传统表格布局的方法:

.grid-table {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 1px;
background-color: #ddd;
}
.grid-table > div {
background-color: white;
padding: 8px;
}
.grid-table .header {
font-weight: bold;
background-color: #f2f2f2;
}






