【问题标题】:Stop flex element grow over max-height:max-content停止 flex 元素增长超过 max-height:max-content
【发布时间】:2020-08-06 22:58:34
【问题描述】:

我用height:0 flex-grow:1 制作了两个部分,并将它们设置为max-height: max-content,这样它们就不会增长太多,当它们包含大量元素时,每个部分将占用50% 的垂直空间。这是一个例子:https://jsfiddle.net/3wogy5q4/17/ 问题是 max-height: max-content 在 Chrome 和 Fireforx 上被忽略,但仅在某些计算机上。你们知道为什么有些电脑会忽略max-content 以及如何替换它吗?

.main-screen {
  height: 60vh;
  background-color: yellow;
}

.col {
  background-color: red;
  height: 100%;
  width: 100px;
  display: flex;
  flex-direction: column;
}

.first {
  background-color: blue;
  width: 95%;
  overflow: auto;
  height: 0;
  flex-grow: 1;
  max-height: max-content;
}

.second {
  background-color: green;
  width: 95%;
  overflow: auto;
  height: 0;
  flex-grow: 1;
  max-height: max-content;
}
<div class="main-screen">
  <div class="col">
    <div class="first">
      <div>First</div>
      <div>First</div>
      <div>First</div>
      <div>First</div>
      <div>First</div>
      <div>First</div>
      <div>First</div>
      <div>First</div>
    </div>
    <div class="second">
      <div>Second</div>
      <div>Second</div>
      <div>Second</div>
      <div>Second</div>
    </div>
  </div>
</div>

【问题讨论】:

  • 我没有在我的计算机上看到问题,但您似乎想要max-height: min-content; 而不是max-height: max-content,这意味着最大值应该是他们需要的最小空间量。
  • max-content === min-content === auto 表示高度。
  • @JacobP min-content 也可以正常工作,但不能解决问题。 “幸运的是”这个问题也出现在我的电脑上,我运行的是最新的 Chrome 84.0.4147.105

标签: css flexbox


【解决方案1】:

你们知道为什么有些电脑会忽略max-content 以及如何替换它吗?

如果您谈论的是计算机,而不是浏览器,那么这些计算机可能运行的是过时的浏览器。

检查浏览器版本。更新浏览器。

http://browsehappy.com/

【讨论】:

    【解决方案2】:

    .first.second 中删除flex-grow,并将.first.second 上的max-height 更改为height

      height: 60vh;
      background-color: yellow;
    }
    
    .col {
      background-color: red;
      height: 100%;
      width: 100px;
      display: flex;
      flex-direction: column;
    }
    
    .first {
      background-color: blue;
      width: 95%;
      overflow: auto;
      height: max-content;
    }
    
    .second {
      background-color: green;
      width: 95%;
      overflow: auto;
      height: max-content;
    }
    

    【讨论】:

    • 当元素很多时,此解决方案不会在部分之间保持 50/50 的垂直分割。如果第一部分的元素数量是第二部分的两倍,则第一部分占用垂直空间的 2/3,而第二部分占用 1/3。
    • @Lad 在你上面的图片中说明它应该如何工作,它并没有保持 50/50。第一个更大,所以这与它应该如何工作的图像相匹配。
    猜你喜欢
    • 2021-05-27
    • 2021-08-24
    • 2012-05-05
    • 2018-05-19
    • 1970-01-01
    • 2016-08-26
    • 2015-10-04
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多