简体   繁体   English

Flex:将max-height应用于子元素,同时防止其占用不需要的空间

[英]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: 这个想法如下:

  • if the 2 children have enough content, keep both child elements at the max-height of 50% of the parent 如果两个孩子的内容足够,请将两个孩子元素的最大高度保持在父对象的50%
  • if the first child has less content, though, shrink it to its content; 如果第一个孩子的内容较少,则将其缩小到其内容; basically make sure it doesn't use the 50% of the parent if it doesn't need to 基本上要确保它不需要使用父级的50%
  • the second child always occupies the rest of the parent's space 第二个孩子总是占据父母空间的其余部分

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM