CSS响应式表格是指使用CSS技术来创建一个表格,该表格能够根据不同的屏幕尺寸和设备类型自动调整其布局和样式,以提供更好的用户体验。响应式设计的核心思想是确保网页内容在任何设备上都能以最佳的方式呈现。
以下是一个简单的CSS响应式表格示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Table</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
@media (max-width: 600px) {
table, thead, tbody, th, td, tr {
display: block;
}
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
tr { border: 1px solid #ccc; }
td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
td:before {
position: absolute;
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
content: attr(data-label);
}
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Name">John Doe</td>
<td data-label="Age">30</td>
<td data-label="Email">john.doe@example.com</td>
</tr>
<tr>
<td data-label="Name">Jane Smith</td>
<td data-label="Age">25</td>
<td data-label="Email">jane.smith@example.com</td>
</tr>
</tbody>
</table>
</body>
</html>
通过上述示例代码,你可以看到如何使用CSS媒体查询来实现一个简单的响应式表格。在小屏幕设备上,表格的列会被垂直堆叠显示,并且每一行会显示一个单元格,同时使用data-label
属性来显示列标题。
领取专属 10元无门槛券
手把手带您无忧上云