具有两个等高 children 的 Flexbox 布局,其中一个包含带有滚动内容的嵌套 Flexbox
Flexbox layout with two equal height children, one containing nested flexbox with scrolling content
我有一个 <img>
,其右侧有一个面板,其中包含控件 header 和一长串项目。所以我有一个嵌套的 flexbox 情况:
.container (flex)
-> .child img
-> .child controls panel (flex)
-> .controls
-> .content-wrapper scrolling list
在最基本的层面上,两个并排的面板很简单,是一个简单的 flex align-items: stretch
.. https://codepen.io/neekfenwick/pen/MOxYxz
在那个基础水平上,这是 How do I keep two divs that are side by side the same height? 的重复问题。
一旦右侧的面板变得更加复杂并带有垂直滚动列表,我就看不出如何使其 parent、第二个 .child
的高度与第一个 .child
的高度匹配.child
。 https://codepen.io/neekfenwick/pen/XzGJGw
+-------------+---------------+
| | Controls |
| Left child | |
| +---------------+
| | A|
| | Vertically |+
| | scrolling ||
| | items ||
| | |+
| | V|
+-------------+---------------+
我读过 Flexbox item with scrollable content but that solves a horizontal scrolling problem, not a vertical layout like this. Also 处理垂直堆叠的嵌套 flexbox,我认为这种 row
和 column
方向的组合混淆了我的情况。
作为代码片段:
.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 来强制右侧元素的高度,但我想知道是否有 'true' flexbox 方法来解决这个问题问题。
一般来说,要使 overflow: scroll
(或 auto
和 hidden
)正常工作,需要以某种方式限制高度,否则元素通常会增长根据需要以适应他们的内容。
主要有 3 种方式,设置实际高度,如第一个示例,我将其添加到 container
。
堆栈片段 1
.container {
display: flex;
height: 100vh;
}
.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>
或者使用绝对定位来创建 高度约束,这是绝对元素所做的。
它只是通过一个额外的包装器 content-scroll
完成的,并将保持结构的其余部分完全动态。
堆栈片段 2
.container {
display: flex;
}
.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 {
position: relative;
flex: 1 1 auto;
width: 400px;
}
.content-scroll {
position: absolute;
top: 0; left: 0;
right: 0; bottom: 0;
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-scroll">
<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>
</div>
单独使用 Flexbox,认为它可能无法完全跨浏览器工作,尤其是对于旧版本,它仍然支持它但有很多错误。
原始代码中的简单修复是将 .content-wrapper
中的 flex: 1 1 auto;
更改为 flex: 1 1 0px;
(我的 IE 版本需要 0px
,Chrome/Firefox/Edge 可以使用 0
)
堆栈片段 3
.container {
display: flex;
}
.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 0px;
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>
</div>
</div>
代码段 2 和 3 也回答了您的问题
...with a vertically scrolling list, I cannot see how to make the
height of its parent, the second .child
, match the height of the
first .child
.
注意,如果右列的顶部元素可以比左列大,则底部元素需要一个最小高度,以防止它折叠成 0,例如
.content-wrapper {
...
min-height: 200px; /* e.g. like this */
}
我有一个 <img>
,其右侧有一个面板,其中包含控件 header 和一长串项目。所以我有一个嵌套的 flexbox 情况:
.container (flex)
-> .child img
-> .child controls panel (flex)
-> .controls
-> .content-wrapper scrolling list
在最基本的层面上,两个并排的面板很简单,是一个简单的 flex align-items: stretch
.. https://codepen.io/neekfenwick/pen/MOxYxz
在那个基础水平上,这是 How do I keep two divs that are side by side the same height? 的重复问题。
一旦右侧的面板变得更加复杂并带有垂直滚动列表,我就看不出如何使其 parent、第二个 .child
的高度与第一个 .child
的高度匹配.child
。 https://codepen.io/neekfenwick/pen/XzGJGw
+-------------+---------------+
| | Controls |
| Left child | |
| +---------------+
| | A|
| | Vertically |+
| | scrolling ||
| | items ||
| | |+
| | V|
+-------------+---------------+
我读过 Flexbox item with scrollable content but that solves a horizontal scrolling problem, not a vertical layout like this. Also row
和 column
方向的组合混淆了我的情况。
作为代码片段:
.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 来强制右侧元素的高度,但我想知道是否有 'true' flexbox 方法来解决这个问题问题。
一般来说,要使 overflow: scroll
(或 auto
和 hidden
)正常工作,需要以某种方式限制高度,否则元素通常会增长根据需要以适应他们的内容。
主要有 3 种方式,设置实际高度,如第一个示例,我将其添加到 container
。
堆栈片段 1
.container {
display: flex;
height: 100vh;
}
.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>
或者使用绝对定位来创建 高度约束,这是绝对元素所做的。
它只是通过一个额外的包装器 content-scroll
完成的,并将保持结构的其余部分完全动态。
堆栈片段 2
.container {
display: flex;
}
.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 {
position: relative;
flex: 1 1 auto;
width: 400px;
}
.content-scroll {
position: absolute;
top: 0; left: 0;
right: 0; bottom: 0;
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-scroll">
<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>
</div>
单独使用 Flexbox,认为它可能无法完全跨浏览器工作,尤其是对于旧版本,它仍然支持它但有很多错误。
原始代码中的简单修复是将 .content-wrapper
中的 flex: 1 1 auto;
更改为 flex: 1 1 0px;
(我的 IE 版本需要 0px
,Chrome/Firefox/Edge 可以使用 0
)
堆栈片段 3
.container {
display: flex;
}
.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 0px;
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>
</div>
</div>
代码段 2 和 3 也回答了您的问题
...with a vertically scrolling list, I cannot see how to make the height of its parent, the second
.child
, match the height of the first.child
.
注意,如果右列的顶部元素可以比左列大,则底部元素需要一个最小高度,以防止它折叠成 0,例如
.content-wrapper {
...
min-height: 200px; /* e.g. like this */
}