



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;
}
| 姓名 |
年龄 |
城市 |
| 张三 |
30 |
北京 |
| 李四 |
25 |
上海 |
/* 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;
}
}