【问题标题】:Flex item won't shrink with overflow - it still remains as high as contentFlex 项目不会因溢出而缩小 - 它仍然与内容一样高
【发布时间】:2019-08-08 18:52:05
【问题描述】:

我希望display: flex; 的其中一个孩子将高度调整为其他孩子。如果这个孩子的内容比其他孩子的大,那么overflow: scroll;应该对这个内容做。

换句话说: 我希望以下代码中的中间 div(黄色背景)将其高度调整为其他 div 的高度,溢出为.tabl

我知道这个问题和许多其他问题一样。我已经阅读了很多线程,但我仍然没有设法解决问题。我为这个帖子道歉,但我没有力气解决它。

.parent {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: baseline;
}

.child1 {
  background-color: red;
  align-self: stretch;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 {
  background-color: yellow;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  margin: 0 15px;
  width: calc(50% - 30px);
  max-width: calc(50% - 30px);
  display: flex;
  flex-direction: column;
  flex-shrink: 1;
}

.child3 {
  background-color: green;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 .tabl {
  flex-shrink: 1;
  min-height: 0;
  overflow: scroll;
}
<div class="parent">
  <div class="child1">
    <p>Some element one</p>
    <p>Some element two</p>
    <p>Some element three</p>
    <p>Some element four</p>
  </div>
  
  <div class="child2">
    <div class="info">
      <h2>Element</h2>
    </div>
    <div class="tabl">
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
    </div>
    <div class="footer">
      <button class="btn">Some button</button>
    </div>
  </div>
  
  <div class="child3">
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
  </div>
</div>

【问题讨论】:

标签: html css flexbox alignment


【解决方案1】:

我想要一个显示的孩子:flex;调整高度以适应其他人。

假设您希望 绿色部分 (child3) 具有最大高度:

  • 确保你有align-items: stretch(它是默认值,所以不要覆盖它) - 这可以确保每个row flex child 拉伸到最大高度的flex child。
  • 将其他部分(child1child2)的内容包装到一个绝对定位元素中。
  • 将此包装器元素设为列弹性框,以便在黄色部分正确溢出。

请看下面的演示:

.parent {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  /*align-items: baseline;*/
}

.child1 {
  background-color: red;
  align-self: stretch;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 {
  background-color: yellow;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  margin: 0 15px;
  width: calc(50% - 30px);
  max-width: calc(50% - 30px);
  display: flex;
  flex-direction: column;
  flex-shrink: 1;
}

.child3 {
  background-color: green;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 .tabl {
  flex-shrink: 1;
  min-height: 0;
  overflow: auto; /* changed to auto */
}

.child1, .child2, .child3 { /* ADDED */
  position: relative;
}

.child-wrap { /* ADDED */
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
<div class="parent">
  <div class="child1">
  <div class="child-wrap">
    <p>Some element one</p>
    <p>Some element two</p>
    <p>Some element three</p>
    <p>Some element four</p></div>
  </div>
  
  <div class="child2">
  <div class="child-wrap">
    <div class="info">
      <h2>Element</h2>
    </div>
    <div class="tabl">
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
    </div>
    <div class="footer">
      <button class="btn">Some button</button>
    </div>
    </div>
  </div>
  
  <div class="child3">
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
      <p>Some element one</p>
      <p>Some element two</p>
      <p>Some element three</p>
      <p>Some element four</p>
  </div>
</div>

假设您希望左侧或右侧部分中的 任一 具有最大高度 - 在这种情况下,仅对 中间黄色元素使用上述 绝对定位 - 请参阅下面的演示,其中 left 或 right 元素中的最高元素定义了整个 parent wrapper 的高度:

.parent {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  /*align-items: baseline;*/
}

.child1 {
  background-color: red;
  align-self: stretch;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 {
  background-color: yellow;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  margin: 0 15px;
  width: calc(50% - 30px);
  max-width: calc(50% - 30px);
  display: flex;
  flex-direction: column;
  flex-shrink: 1;
}

.child3 {
  background-color: green;
  width: 25%;
  min-width: 25%;
  max-width: 25%;
}

.child2 .tabl {
  flex-shrink: 1;
  min-height: 0;
  overflow: auto; /* changed to auto */
}

.child1, .child2, .child3 { /* ADDED */
  position: relative;
}

.child-wrap { /* ADDED */
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
<div class="parent">
  <div class="child1">
    <p>Some element one</p>
    <p>Some element two</p>
    <p>Some element three</p>
    <p>Some element four</p>
    <p>Some element two</p>
    <p>Some element three</p>
    <p>Some element four</p>
  </div>

  <div class="child2">
    <div class="child-wrap">
      <div class="info">
        <h2>Element</h2>
      </div>
      <div class="tabl">
        <p>Some element one</p>
        <p>Some element two</p>
        <p>Some element three</p>
        <p>Some element four</p>
        <p>Some element one</p>
        <p>Some element two</p>
        <p>Some element three</p>
        <p>Some element four</p>
        <p>Some element one</p>
        <p>Some element two</p>
        <p>Some element three</p>
        <p>Some element four</p>
        <p>Some element one</p>
        <p>Some element two</p>
        <p>Some element three</p>
        <p>Some element four</p>
      </div>
      <div class="footer">
        <button class="btn">Some button</button>
      </div>
    </div>
  </div>

  <div class="child3">
    <p>Some element one</p>
    <p>Some element two</p>
    <p>Some element three</p>

  </div>
</div>

【讨论】:

  • 不幸的是,最后一个 div(绿色)不会是最高的(不一定是最高的)。有时第一个 div(红色)可能是最高的 - 然后中间(黄色)高度也应该调整到最高。
  • 所以 only 使用绝对定位 yellow middle element - 我想在这种情况下会奏效......试试看 - 见更新的答案...
  • 好的,这是一个非常好的主意 :) 我希望只使用 flexbox 来达到这个目的,但是这个解决方案非常有效。
【解决方案2】:

例如,您将 .parent 设置为 最大高度:150px;

【讨论】:

    【解决方案3】:

    如果您提前知道您的 flex 子项的所需高度,则使用 max-height 属性是 Flexbox 原生提供的最简洁的解决方案,正如 @Lewis 建议的那样。

    如果您需要更细粒度的控制,请考虑使用 CSS Grid 布局模块。如果您使用下面的 sn-p 替换现有代码并将其应用于您的标记,您可以看到您尝试实现的布局的极简再现。

    .parent {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      grid-template-rows: repeat(4, 100px);
    }
    
    .parent > * {
      border: 2px solid black; // added for clarity
      grid-row-start: 1;
      grid-row-end: 4;
    }
    
    .child2 {
      overflow-y: scroll;
    }
    

    【讨论】:

      【解决方案4】:
      在这种情况下,

      溢出意味着如果没有定义一个高度,在此之后内容应该溢出。 flex 的问题(或好处,在大多数情况下)是盒子会增长以匹配“最大”的内容,或者更确切地说是 flex(因此得名)。为此,您可能需要 Javascript,或者您需要为子级定义最大高度。

      以下是您的代码示例,其中max-height 应用于儿童;

      .parent {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: baseline;
      }
      
      .child1 {
        background-color: red;
        align-self: stretch;
        width: 25%;
        min-width: 25%;
        max-width: 25%;
      }
      
      .child2 {
        background-color: yellow;
        min-height: 0;
        min-width: 0;
        overflow: auto;
        margin: 0 15px;
        width: calc(50% - 30px);
        max-width: calc(50% - 30px);
        display: flex;
        flex-direction: column;
        flex-shrink: 1;
      }
      
      .child3 {
        background-color: green;
        width: 25%;
        min-width: 25%;
        max-width: 25%;
      }
      
      .child2 .tabl {
        flex-shrink: 1;
        min-height: 0;
        overflow: scroll;
      }
      
      .child1, .child2, .child3{
        max-height: 250px;
        overflow: auto;
      }
      <div class="parent">
        <div class="child1">
          <p>Some element one</p>
          <p>Some element two</p>
          <p>Some element three</p>
          <p>Some element four</p>
        </div>
        
        <div class="child2">
          <div class="info">
            <h2>Element</h2>
          </div>
          <div class="tabl">
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
          </div>
          <div class="footer">
            <button class="btn">Some button</button>
          </div>
        </div>
        
        <div class="child3">
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
            <p>Some element one</p>
            <p>Some element two</p>
            <p>Some element three</p>
            <p>Some element four</p>
        </div>
      </div>

      【讨论】:

      • 视图必须...灵活。 :) 我无法在任何地方设置最大高度。有时,左侧或右侧的元素可能高于中间的元素。
      猜你喜欢
      • 2019-01-09
      • 2013-09-24
      • 1970-01-01
      • 2021-06-18
      • 2014-06-21
      • 1970-01-01
      • 2018-05-07
      • 2017-09-07
      • 2018-02-22
      相关资源
      最近更新 更多