【发布时间】:2018-05-17 14:33:46
【问题描述】:
我有一个<img>,其右侧有一个面板,其中包含一个控件标题和一长串项目。所以我有一个嵌套的弹性盒情况:
.container (flex)
-> .child img
-> .child controls panel (flex)
-> .controls
-> .content-wrapper scrolling list
在非常基本的层面上,两个并排的面板很容易,通过align-items: stretch .. https://codepen.io/neekfenwick/pen/MOxYxz 进行简单的弯曲
在那个基本级别上,这是对How do I keep two divs that are side by side the same height? 的重复问题。
一旦右侧面板通过垂直滚动列表变得更加复杂,我无法看到如何使其父级的高度,即第二个.child,与第一个.child 的高度匹配。 https://codepen.io/neekfenwick/pen/XzGJGw
+-------------+---------------+
| | Controls |
| Left child | |
| +---------------+
| | A|
| | Vertically |+
| | scrolling ||
| | items ||
| | |+
| | V|
+-------------+---------------+
我已阅读Flexbox item with scrollable content,但这解决了水平滚动问题,而不是像这样的垂直布局。同样Nested flexbox with scrolling area 处理垂直堆叠的嵌套弹性盒,我认为row 和column 方向的组合让我感到困惑。
作为代码sn-p:
.container {
display: flex;
align-items: stretch;
}
.child {
border: 1px solid grey;
background-color: lightgrey;
flex: 1 1 auto;
}
.controls-panel {
display: flex;
flex-direction: column;
}
.controls {
flex: 0 0 auto;
}
.content-wrapper {
flex: 1 1 auto;
width: 400px;
overflow-y: auto;
}
.content-item {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
<div class="container">
<div class="child">
<p>In real life I am an inline img.</p>
<p>I am some content whoop de doo.</p>
<p>I am some content whoop de doo.</p>
<p>I am some content whoop de doo.</p>
<p>I want my sibling to equal my height.</p>
</div>
<div class="child controls-panel">
<div class="controls">
<p>Small controls area. Panel below should scroll vertically.</p>
</div>
<div class="content-wrapper">
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
<div class="content-item"></div>
</div>
</div>
</div>
我不想在这里修复任何元素的高度。碰巧,我知道 img 的高度,所以我可以使用 CSS 来强制右侧元素的高度,但我想知道是否有一种“真正的”flexbox 方法来解决这个问题。
【问题讨论】:
-
我能做些什么来让我的答案被接受吗?
-
我真的很抱歉耽搁了。您的回答非常好,我现在刚刚被项目的其他部分所淹没。我在您发布它时阅读了它,但是您使用了我以前从未见过的技巧并且需要时间来试验。我会评论答案。
-
没问题 :) ... 只是在跟进我的回答