



td::before {
content: attr(data-label) ": "; /* 显示data-label的值 */
font-weight: bold;
display: inline-block; /* 或 block,根据需要 */
min-width: 80px; /* 确保标签有足够空间 */
margin-right: 5px;
}td {
position: relative; /* 如果需要绝对定位伪元素 */
padding-left: 100px; /* 为伪元素留出空间 */
}
td::before {
content: attr(data-label);
position: absolute;
left: 0;
width: 90px; /* 固定宽度 */
font-weight: bold;
text-align: right;
padding-right: 10px;
}td {
display: flex; /* 让单元格内部的标签和内容并排 */
justify-content: space-between; /* 或者 flex-start */
align-items: center;
padding: 8px 10px;
border-bottom: 1px solid #eee;
}
td::before {
content: attr(data-label);
font-weight: bold;
flex-shrink: 0; /* 防止标签被压缩 */
margin-right: 10px;
}<!-- HTML 示例 -->
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="姓名">张三</td>
<td data-label="年龄">30</td>
<td data-label="城市">北京</td>
</tr>
<tr>
<td data-label="姓名">李四</td>
<td data-label="年龄">25</td>
<td data-label="城市">上海</td>
</tr>
</tbody>
</table>/* CSS 示例 */
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
/* 桌面端样式 */
@media screen and (min-width: 769px) {
thead {
display: table-header-group; /* 确保桌面端显示 */
}
td::before {
content: none; /* 桌面端不显示伪元素 */
}
}
/* 移动端样式 */
@media screen and (max-width: 768px) {
table, thead, tbody, th, td, tr {
display: block;
}
thead {
display: none; /* 隐藏表头 */
}
tr {
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden;
}
td {
border: none; /* 移除单元格边框 */
position: relative;
padding-left: 100px; /* 为伪元素留出空间 */
text-align: right; /* 数据右对齐 */
}
td::before {
content: attr(data-label); /* 显示data-label的值 */
position: absolute;
left: 0;
width: 90px; /* 固定宽度 */
font-weight: bold;
text-align: left; /* 标签左对齐 */
padding-right: 10px;
box-sizing: border-box;
}
}以上就是CSS中如何处理数据表格响应式—display切换方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号