【问题标题】:flexbox overflowing container in firefox [duplicate]Firefox中的flexbox溢出容器[重复]
【发布时间】:2017-08-25 02:56:45
【问题描述】:

我正在处理 flexbox 布局,但我遇到了一个问题,即 flex 项只会在 firefox 中溢出容器。这是我能想到的最精简的示例(抱歉,它不是超级小,我不确定是什么导致了问题,所以很难知道我可以删除什么):

<div class="css-190tu11">
  <h1 class="css-1qqir8k">Blah</h1>
  <div class="css-131b9cy">
    <div class="css-1suni8j">
      <a class="css-yq2t95" href="some-url">
        <span class="icon icon-register css-nwymqb" aria-hidden="true"></span>
        <h2 class="css-1892n22">Title</h2>
        <div class="css-kgza95">Description</div>
      </a>
      <div class="css-kujdaf">
        Some special thing
      </div>
    </div>
    <div class="css-1suni8j">
      <a class="css-yq2t95" href="some-url">
        <span class="icon icon-register css-nwymqb" aria-hidden="true"></span>
        <h2 class="css-1892n22">Title</h2>
        <div class="css-kgza95">Description</div>
      </a>
      <div class="css-dkez8"></div>
    </div>
    <div class="css-1suni8j">
      <a class="css-yq2t95" href="some-url">
        <span class="icon icon-register css-nwymqb" aria-hidden="true"></span>
        <h2 class="css-1892n22">Title</h2>
        <div class="css-kgza95">Description</div>
      </a>
      <div class="css-dkez8"></div>
    </div>
    <div class="css-1suni8j">
      <a class="css-yq2t95" href="some-url">
        <span class="icon icon-register css-nwymqb" aria-hidden="true"></span>
        <h2 class="css-1892n22">Title</h2>
        <div class="css-kgza95">Description</div>
      </a>
      <div class="css-dkez8"></div
    </div>
  </div>
</div>

还有 CSS:

.css-190tu11,
[data-css-190tu11] {
  padding: 12px 48px 24px 48px;
  border: solid 1px #e0e0e0;
}
.css-1qqir8k,
[data-css-1qqir8k] {
  font-size: 24px;
  font-weight: 300;
  margin-bottom: 18px;
  margin-top: 0;
}
.css-131b9cy,
[data-css-131b9cy] {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
.css-131b9cy > *,
[data-css-131b9cy] > * {
  flex: 0 1 auto;
  margin-left: 0;
  margin-right: 0;
}
.css-131b9cy > *:not(:first-child),
[data-css-131b9cy] > *:not(:first-child) {
  margin-left: 10px;
}
.css-131b9cy > *:not(:last-child),
[data-css-131b9cy] > *:not(:last-child) {
  margin-right: 10px;
}

.css-1suni8j,
[data-css-1suni8j] {
  display: flex;
  background-color: white;
  flex-direction: column;
  justify-content: space-between;
  text-align: center;
  max-width: 186px;
  min-width: 120px;
  height: 240px;
}
.css-1suni8j > *,
[data-css-1suni8j] > * {
  margin-top: 0;
  margin-bottom: 0;
}
.css-1suni8j > *:not(:first-child),
[data-css-1suni8j] > *:not(:first-child) {
  margin-top: 10px;
}
.css-1suni8j > *:not(:last-child),
[data-css-1suni8j] > *:not(:last-child) {
  margin-bottom: 10px;
}

.css-yq2t95,
[data-css-yq2t95] {
  flex: 1;
  justify-content: space-between;
  flex-direction: column;
  display: flex;
}
.css-yq2t95 > *:last-child,
[data-css-yq2t95] > *:last-child {
  flex: 0 1 auto;
}
.css-yq2t95 > *,
[data-css-yq2t95] > * {
  margin-top: 0;
  margin-bottom: 0;
}
.css-yq2t95 > *:not(:first-child),
[data-css-yq2t95] > *:not(:first-child) {
  margin-top: 5px;
}
.css-yq2t95 > *:not(:last-child),
[data-css-yq2t95] > *:not(:last-child) {
  margin-bottom: 5px;
}

.css-nwymqb,
[data-css-nwymqb] {
  transition: all 0.5s ease;
  border-radius: 50%;
  display: block;
  margin-left: auto;
  border: 1px solid #0070ba;
  height: 60px;
  width: 60px;
  font-size: 30px;
  line-height: 54px;
  margin-right: auto;
}

.css-1892n22,
[data-css-1892n22] {
  font-weight: normal;
  flex: 0 1 auto;
  color: inherit;
  padding: 0;
  font-size: 16px;
  height: 80px;
}
.css-kgza95,
[data-css-kgza95] {
  font-weight: normal;
  font-size: 11px;
  color: #333333;
  height: 76px;
}
.css-kujdaf,
[data-css-kujdaf] {
  min-height: 36px;
  background-image: linear-gradient(93deg, #1446a0, #0070ba);
  width: 100%;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
}
.css-dkez8,
[data-css-dkez8] {
  min-height: 36px;
}

这是它在 Chrome 和 Safari 中的样子(这是我想要的样子):

这是它在 Firefox 中的样子:

即使是 Internet Explorer 10 也能以我想要的方式显示它! (无法在 codepen 中验证,因为我删除了供应商前缀,但它在我的应用程序中有效)。

这里是a codepen

感谢您的帮助!

【问题讨论】:

  • 你不能用 CSS 做一个更大的高度吗?
  • 我希望它们的高度正好是 240 像素。
  • 您可以使用特定于浏览器的@media 查询。也许@media all and (min--moz-device-pixel-ratio:0).

标签: html css firefox flexbox


【解决方案1】:

css-1suni8j 类的容器具有固定的高度(240 像素),其中的元素(h2、DIV)也具有固定的宽度,这使得最后一个带有按钮的 DIV 位于其边框下方。如果删除 240px 高度,它将保留在容器内。

由于您的列中有不同数量的元素并且justify-contentspace-between,因此无法将其他元素保持在相同的高度。

【讨论】:

  • 感谢您的回答。为什么在 Chrome/Safari/IE10 中可以正常使用?
  • 可能是因为他们解释弹性框参数的方式
  • 有没有办法让 firefox 以相同的方式解释它们,或者提供不同的选项?
  • 我正在考虑在这些容器上使用justify-content: flex-start,具有未定义的高度(即删除240px 的高度),但由于某种原因不能保持子元素的高度固定。
【解决方案2】:

Turns out,溢出的元素需要有一个min-height: 0。我不知道为什么,但是把它放在.css-yq2t95 上,一切都很好。感谢大家的参与!

【讨论】:

  • 解释:你已经为.css-yq2t95 的所有子元素指定了高度,而在 Firefox(和 Edge 14)中,它恰好采用它们的高度/边距/边框的总和(并推动其兄弟,“特殊事物”按钮),不在视野范围内。默认情况下,垂直弹性项目(如.css-yq2t95)可以缩小,但不能低于它们的最小内容高度——在这种情况下是指定高度的总和。但是,如果添加 min-height:0,则会抑制特殊的最小高度行为并允许其缩小。
  • 然后因为.css-yq2t95 它自己是一个弹性容器,它会提示它的孩子缩小,他们能够这样做是因为他们自己的最小内容高度更小比他们(太大)指定的高度。
  • 您应该考虑在此处将 Chrome/Safari 的行为报告为各自的错误跟踪器中的错误! (或至少在其中之一):)
  • 除了(或代替)您的min-height:0 修复程序外,您可能只想首先对.css-yq2t95 的子项使用较小的指定高度。 (例如,在您依赖的收缩发生后,您可以使用这些元素实际结束的高度。)(尽管取决于您期望这些元素具有多少内容,也许最好不要给它们太小的高度,否则它们的内容可能会溢出。)
  • @dholbert,宾果游戏,问题是内容将被翻译,所以我真的很难知道内容最终会有多大。
猜你喜欢
  • 2017-12-10
  • 2014-05-28
  • 2020-03-15
  • 2015-02-10
  • 2020-10-12
  • 2016-10-01
  • 2018-12-09
  • 2023-04-10
  • 2018-01-07
相关资源
最近更新 更多