【发布时间】:2017-12-11 15:27:02
【问题描述】:
我有一个使用 flexbox 的非常简单的网格,我想在可滚动框中显示多行数据。我可能遗漏了一个微不足道的属性,但行高似乎无法正确调整到其内容的高度。
一个例子:https://plnkr.co/edit/rCXfvd4Vsh8RgoFja89A?p=preview
.grid {
display: flex;
flex-flow: column;
max-height: 400px;
overflow-y: auto;
}
.grid .header {
font-weight: 700;
margin-bottom: 6px;
border-bottom: 3px solid black;
}
.grid .row {
flex: 1;
display: flex;
flex-flow: row;
}
.grid .row:nth-child(even) {
background: #CCC;
}
.grid .col-1 {
flex: 0 0 60px;
}
.grid .col-2 {
flex: 1 0 200px;
white-space: pre;
}
<h1>Flexbox grid</h1>
<h3>Overflow example</h3>
<div class="grid">
<div class="row header">
<div class="col-1">Col 1</div>
<div class="col-2">Col 2</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">One Line</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">One Line</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
</div>
<h3>No overflow example</h3>
<div class="grid">
<div class="row header">
<div class="col-1">Col 1</div>
<div class="col-2">Col 2</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
</div>
为了保持一切简单,我的列是固定大小的,我想要显示的所有数据都加载到网格中(而不是使用虚拟网格)。有没有办法修复我的示例,以便行根据内容自行调整?
编辑:已解决!
正如用户@bhv 指出的那样,我应该通过应用flex: 1 0 auto 而不是flex+:1 (1 auto) 来禁用.row 的收缩。
.grid {
display: flex;
flex-flow: column;
max-height: 400px;
overflow-y: auto;
}
.grid .header {
font-weight: 700;
margin-bottom: 6px;
border-bottom: 3px solid black;
}
.grid .row {
flex: 1;
display: flex;
flex-flow: row;
flex-shrink: 0;
}
.grid .row:nth-child(even) {
background: #CCC;
}
.grid .col-1 {
flex: 0 0 60px;
}
.grid .col-2 {
flex: 1 0 200px;
white-space: pre;
}
<h1>Flexbox grid</h1>
<h3>Overflow example</h3>
<div class="grid">
<div class="row header">
<div class="col-1">Col 1</div>
<div class="col-2">Col 2</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">One Line</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">One Line</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
</div>
<h3>No overflow example</h3>
<div class="grid">
<div class="row header">
<div class="col-1">Col 1</div>
<div class="col-2">Col 2</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
<div class="row body">
<div class="col-1">DATA</div>
<div class="col-2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled</div>
</div>
</div>
【问题讨论】:
-
哦哇..我只需要禁用“收缩”属性?我完全忽略了这一点。谢谢!