[英]Flex: apply max-height to a child element while preventing it from taking the space it doesn't need
I have a flex
container with 2 child elements, sharing the same space ( flex-grow: 1
). 我有一个带有2个子元素的
flex
容器,它们共享相同的空间( flex-grow: 1
)。 Each of them has overflow: auto
set. 每个都有
overflow: auto
设置。 They look like this: 他们看起来像这样:
The idea is the following: 这个想法如下:
So, in the case shown above, I really want this: 因此,在上面显示的情况下,我真的想要这样:
I am aware of the flex-shrink
property in CSS, I've tried to use it but I must be doing it wrong. 我知道CSS中的
flex-shrink
属性,我尝试使用它,但是我一定做错了。 Any help? 有什么帮助吗?
The code: ( jsfiddle ) 代码: ( jsfiddle )
HTML HTML
<div id="container">
<div class="child one">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</div>
<div class="child two">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>
</div>
CSS CSS
#container {
width: 200px;
height: 300px;
display:flex;
flex-direction: column;
}
.child {
max-height: 50%;
height: 100%;
flex-grow: 1;
overflow: auto;
}
.one {
background-color: #6799d2;
max-height: 19%;
}
.two {
background-color: #67d375;
}
Set the maximum height of the children, don't allow them to grow but allow them to shrink when possible. 设置孩子的最大身高,不允许他们成长,但在可能的情况下让他们收缩。
#container { width: 200px; height: 300px; display:flex; flex-direction: column; } .one { background-color: #6799d2; max-height: 50%; flex-shrink: 1; } .two { background-color: #67d375; flex-grow: 1; overflow: auto; }
<div id="container"> <div class="child one">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</div> <div class="child two">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div> </div>
You can give the second one a max-height of 100%: 您可以将第二个最大高度设置为100%:
.two {
max-height: 100%
}
Is that what you are looking for? 那是您要找的东西吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.