作者:购物狂RZBZ_719 | 来源:互联网 | 2023-05-16 22:29
Withaflexcontainerwithafixedheight,Icanmakeaflexitemscrollablebysettingoverflow:au
With a flex container with a fixed height, I can make a flex item scrollable by setting overflow: auto
, like so:
有了一个固定高度的flex容器,我可以通过设置overflow: auto来实现可滚动项,如下所示:
page {
display: flex;
flex-flow: row wrap;
height: 200px;
}
left-panel {
flex: 1;
display: block;
background: lightblue;
}
//main {flex: 5; display: block;}
header {
background: turquoise;
display: block
}
//main-content-wrapper {flex: 4; display:flex; flex-direction: column}
right-panel {
flex: 1;
overflow: auto;
}
content-panel {
background: pink;
display: block;
}
content {
height: 1000px;
display: block;
}
left-panel
(static)
header
(static)
content
(scrolls)
fiddle
小提琴
But I would like to make a child of the flex item scroll instead.
但是我想要让一个flex项目滚动的孩子。
I would think that setting height: 100%
on the flex item and overflow: auto
on the child element I want to scroll would work, but it does not:
我认为在flex项目上设置高度为100%,而在我想滚动的子元素上设置overflow: auto就可以了,但它不行:
page {
display: flex;
flex-flow: row wrap;
height: 200px;
}
left-panel {
flex: 1;
display: block;
background: lightblue;
}
//main {flex: 5; display: block;}
header {
background: turquoise;
display: block
}
//main-content-wrapper {flex: 4; display:flex; flex-direction: column}
right-panel {
flex: 1;
height: 100%;
}
content-panel {
background: pink;
display: block;
overflow: auto;
}
content {
height: 1000px;
display: block;
}
left-panel
(static)
header
(static)
content
(scrolls)
fiddle
小提琴
How can I make this work?
我怎样才能让它工作呢?
And why doesn't the second fiddle work?
为什么第二小提琴不奏效呢?
Added OP comments:
OP的评论:
I can see there are several related questions, but the ones I found are either not directly applicable, or mention workarounds to bugs that may have been fixed since.
我可以看到有几个相关的问题,但是我发现的这些问题不是直接适用的,或者是对可能已经修复的bug提出解决办法。
I would like to avoid using explicit height calculations to reduce coupling of components.
我希望避免使用显式的高度计算来减少组件之间的耦合。
I could convert the right-panel into a nested flexbox with flex-direction: column
, but I was hoping to avoid that.
我可以将右面板转换为带有flex-direction: column的嵌套flexbox,但我希望避免这种情况。
In case a flat structure (like in the second fiddle) is not possible without an explicit height calculation, it would be nice with an explanation of why. Could it be considered a bug with flexbox or am I misunderstanding something fundamental about css?
如果一个平面结构(如第二小提琴)不可能没有显式的高度计算,它将很好地解释为什么。它会被认为是带有flexbox的bug吗?还是我误解了css的一些基本原理?
3 个解决方案