html - 使用 CSS 将表格行显示为列
问题描述
我希望标签和值使用表格根据最长的标签大小正确对齐。对于大屏幕尺寸,我想将 2 个表格行的集合显示为列(如图 B 所示)。是否有唯一的 CSS 方法来实现这种响应式显示更改?
<div class="table">
<div class="row">
<div class="cell">label 1</div>
<div class="cell">value 1</div>
</div>
<div class="row">
<div class="cell"> looooooooooong label 2</div>
<div class="cell">value 2</div>
</div>
<div class="row">
<div class="cell">label 3</div>
<div class="cell">value 3</div>
</div>
<div class="row">
<div class="cell">label 4</div>
<div class="cell">value 4</div>
</div>
</div>
解决方案
这是对@Fakt309 开始的解决方案的改进。我使用flex-order
(订单)重新排列大屏幕的项目。
@media only screen
and (min-width: 801px) {
.row:nth-of-type(1) {
order: 1;
}
.row:nth-of-type(2) {
order: 3;
}
.row:nth-of-type(3) {
order: 2;
}
.row:nth-of-type(4) {
order: 4;
}
}
我还添加flex-direction: column;
了小屏幕以使这些项目在彼此下方对齐。
.table {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
flex-wrap: wrap;
}
.row {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
width: 40%;
}
.cell {
padding: 20px;
width: 40%;
word-break: break-all;
}
@media only screen
and (max-width: 800px) {
.table {
flex-direction: column;
}
.row {
width: 90%;
}
}
@media only screen
and (min-width: 801px) {
.row:nth-of-type(1) {
order: 1;
}
.row:nth-of-type(2) {
order: 3;
}
.row:nth-of-type(3) {
order: 2;
}
.row:nth-of-type(4) {
order: 4;
}
}
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<div class="table">
<div class="row">
<div class="cell">label 1</div>
<div class="cell">value 1</div>
</div>
<div class="row">
<div class="cell"> looooooooooong label 2</div>
<div class="cell">value 2</div>
</div>
<div class="row">
<div class="cell">label 3</div>
<div class="cell">value 3</div>
</div>
<div class="row">
<div class="cell">label 4</div>
<div class="cell">value 4</div>
</div>
</div>
推荐阅读
- python - Python re.findall 避免急切
- c++ - 使用文字字符串作为参数为类模块创建运算符重载时遇到困难
- c++ - 将分配器用于不同类型的结构字段
- macos - Elastic Search Immediately Dies on Mac
- tensorflow - ML.PREDICT 的多列输入,用于 BigQuery ML 中的 TensorFlow 模型
- javascript - 我可以合并多个基于 React 子组件回调设置状态的函数吗?
- java - 在 Java 中运行线程会引发异常
- java - 保存整个 Web 请求,Tomcat 9.0
- powershell - 唯一会话 ID
- r - R中的绘图显示我的X轴与“M”代表百万 - 我如何显示实数