css - Chrome 忽略列布局中的 flex-basis

标签 css google-chrome webkit flexbox

<分区>

我无法让 Chrome 在 flex-direction: column 布局中关注 flex: 1 1 25% 的 flex-basis 部分。它在 row 布局中运行良好。

下面的代码片段演示了这个问题:黄色、蓝色和粉红色条是 flex-basis 50px、25% 和 75%,显示在列和行的 flex 方向上。

如果您在 Firefox(或 IE11 或 Edge)中运行它,列和行都会按预期划分区域:

Firefox screenshot

但是如果您在 Chrome (47) 或 Safari (9.0.3) 中运行它,左侧的列布局似乎完全忽略了 flex-basis —— 条形的高度似乎与 flex 无关-基础:

enter image description here

左右之间唯一的区别是flex-direction

.container {
  width: 400px;
  height: 200px;
  display: flex;
  background: #666;
  position: relative;
}

.layout {
  flex: 1 1 100%; /* within .container */
  margin: 10px;
  display: flex;
}

.row {
  flex-direction: row;
}
.column {
  flex-direction: column;
}

.exact {
  flex: 1 1 50px;
  background: #ffc;
}
.small {
  flex: 1 1 25%;
  background: #cff;
}
.large {
  flex: 1 1 75%;
  background: #fcf;
}
<div class="container">
  <div class="layout column">
    <div class="exact">50px</div>
    <div class="small">25%</div>
    <div class="large">75%</div>
  </div>
  <div class="layout row">
    <div class="exact">50px</div>
    <div class="small">25%</div>
    <div class="large">75%</div>
  </div>
</div>

我尝试将 height: 100% 添加到 .column,这让 Chrome 注意到了 flex-basis,但导致了另一个问题——flex 变大了比它的容器:

.column {
  flex-direction: column;
  height: 100%;
}

Chrome screenshot with height: 100%

我猜这是一个 long-standing webkit bug .有什么办法可以解决吗? (我正在尝试创建一些通用布局组件,因此对特定数量的子项或特定像素高度进行硬编码是行不通的。)

[编辑] 这是一个显示一般问题的附加示例(并避免了上例中的边距和总计大于 100% 的问题):

Firefox vs Chrome nested flexboxes

.container {
  width: 300px;
  height: 300px;
  display: flex;
}

.layout {
  flex: 1 1 100%; /* within its flex parent */
  display: flex;
  background: #ffc;
}

.row {
  flex-direction: row;
}
.column {
  flex-direction: column;
  height: 100%; /* attempted workaround for webkit */
}

.small {
  flex: 1 1 30%;
  background: #cff;
}
.large {
  flex: 1 1 70%;
  background: #fcf;
}

div {
  /* border/padding just makes divs easier to see --
     you can remove all of this without changing the problem */
  box-sizing: border-box;
  border: 1px solid #999;
  padding: 10px;
}
<div class="container">
    <div class="layout row">
        <div class="small">row: 30%</div>
        <div class="large layout column">
          <div class="small">row: 70%; col: 30%</div>
          <div class="large">row: 70%; col: 70%</div>
        </div>
    </div>
</div>

最佳答案

需要考虑的三个元素:

  • 所有高度之和大于 100%

    在您的 .column 布局中,您有三个 flex 元素。它们的高度是 75% + 25% + 50px。这本身就超过了您应用的 height: 100%。这不会导致溢出,因为您将 flex-shrink 设置为 1

  • 边距空间

    您已为两种布局指定了 margin: 10px。所以从顶部和底部边缘有额外的 20px 高度。在 .column 布局中,这确实确实会导致 Chrome 溢出。

    调整那些额外的 20px,溢出消失了:

    .column {
      flex-direction: column;
      height: calc(100% - 20px);   /* new */
    }
    

    .container {
      width: 400px;
      height: 200px;
      display: flex;
      background: #666;
      position: relative;
    }
    
    .layout {
      flex: 1 1 100%; /* within .container */
      margin: 10px;
      display: flex;
    }
    
    .row {
      flex-direction: row;
    }
    .column {
      flex-direction: column;
      height: calc(100% - 20px);   /* NEW */
    }
    
    .exact {
      flex: 1 1 50px;
      background: #ffc;
    }
    .small {
      flex: 1 1 25%;
      background: #cff;
    }
    .large {
      flex: 1 1 75%;
      background: #fcf;
    }
    <div class="container">
      <div class="layout column">
        <div class="exact">50px</div>
        <div class="small">25%</div>
        <div class="large">75%</div>
      </div>
      <div class="layout row">
        <div class="exact">50px</div>
        <div class="small">25%</div>
        <div class="large">75%</div>
      </div>
    </div>

  • 百分比高度:Chrome/Safari 与 Firefox/IE

    Chrome/Safari 中的 flex 元素无法识别其百分比高度的原因是因为 Webkit 浏览器坚持对规范的更传统解释:

    CSS height property

    percentage
    Specifies a percentage height. The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly and this element is not absolutely positioned, the value computes to auto.

    auto
    The height depends on the values of other properties.

    换句话说,如果您想要一个元素具有百分比高度,那么您必须在父元素上指定一个高度。

    这种语言的传统解释是,“高度”表示 height 属性的值。虽然从语言中不清楚“高度”的确切含义,但 height 属性要求一直是主要的实现。在处理百分比值时,我从未见过 min-heightmax-height 或其他形式的高度适用于父元素。

    然而,最近,正如这个问题(以及 another oneanother oneanother one )所指出的,Firefox(显然还有 IE)已经扩大了它的解释以接受 flex 高度,

    尚不清楚哪个浏览器更符合标准。

    height 定义自 1998 年以来就没有更新过(CSS2),这无济于事。

    最重要的是,Chrome 和 Safari 会根据父元素的 height 属性值来解析百分比高度。 Firefox 和 IE11/Edge 使用父级计算的 flex 高度。

    目前,在我看来,解决此问题的最简单的跨浏览器解决方案是全面使用 height 属性来设置百分比高度。

    更新:更多解决方案在这里:Chrome / Safari not filling 100% height of flex parent

关于css - Chrome 忽略列布局中的 flex-basis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049343/

相关文章:

google-chrome - 如何在扩展页面操作上添加更多链接

iphone - Webkit iPhone 应用程序 : How to dynamically change the user zoom (or scale, pic & Zoom) 在视口(viewport)中?

android - 我怎样才能从 WebView 中获取 MIME 类型?

html - 在 div 中设置列​​表样式

html - 样式表未显示在 div id 选项卡中?

Javascript 日期解析在 Chrome 中返回奇怪的结果

ios - 如果我使用 -webkit-overflow-scrolling,Div 滚动有时会卡住

html - Bootstrap 下拉菜单样式

html - 内容在移动设备上没有响应(Bootstrap)

windows - 谷歌浏览器不打开自定义协议(protocol)