Chrome 忽略列布局中的 flex-basis [英] Chrome ignoring flex-basis in column layout

查看:20
本文介绍了Chrome 忽略列布局中的 flex-basis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 Chrome 在 flex-direction: column 布局中注意 flex: 1 1 25% 的 flex-basis 部分.它在 row 布局中工作正常.

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

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

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

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

.container {宽度:400px;高度:200px;显示:弹性;背景:#666;位置:相对;}.布局 {弹性:1 1 100%;/* 在 .container */边距:10px;显示:弹性;}.排 {弹性方向:行;}.柱子 {弹性方向:列;}.精确的 {弹性:1 1 50px;背景:#ffc;}.小的 {弹性:1 1 25%;背景:#cff;}.大 {弹性:1 1 75%;背景:#fcf;}

<div class="布局列"><div class="exact">50px</div><div class="small">25%</div><div class="large">75%</div>

<div class="布局行"><div class="exact">50px</div><div class="small">25%</div><div class="large">75%</div>

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

.column {弹性方向:列;高度:100%;}

我认为这是一个

.container {宽度:300px;高度:300px;显示:弹性;}.布局 {弹性:1 1 100%;/* 在它的 flex 父级中 */显示:弹性;背景:#ffc;}.排 {弹性方向:行;}.柱子 {弹性方向:列;高度:100%;/* 尝试解决 webkit */}.小的 {弹性:1 1 30%;背景:#cff;}.大 {弹性:1 1 70%;背景:#fcf;}div {/* 边框/填充只是让 div 更容易看到 --你可以在不改变问题的情况下删除所有这些 */box-sizing: 边框框;边框:1px 实心 #999;填充:10px;}

<div class="布局行"><div class="small">行:30%</div><div class="大布局栏"><div class="small">行:70%;列:30%/div<div class="large">行:70%;列:70%

解决方案

需要考虑的三项:

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

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

    <块引用>

    CSS height 属性

    百分比
    指定百分比高度.该百分比是根据生成的框的包含块的高度计算的.如果未明确指定包含块的高度并且此元素不是绝对定位,则该值计算为 auto.

    auto
    高度取决于其他属性的值.

    换句话说,如果您希望元素具有百分比高度,那么您必须指定父元素的高度.

    这种语言的传统解释是高度"表示height 属性的值.尽管从语言中不清楚高度"的确切含义,但 height 属性要求一直是主要的实现.在处理百分比值时,我从未见过 min-heightmax-height 或其他形式的高度对父级起作用.

    然而,正如本问题中所述(以及另一个另一个另一个)、Firefox(和 IE),显然)已经扩大了其解释以接受 flex 高度,以及.

    目前尚不清楚哪个浏览器更符合标准.

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

    总之,Chrome 和 Safari 会根据父级 height 属性的值解析百分比高度.Firefox 和 IE11/Edge 使用父计算的 flex 高度.

    目前,在我看来,解决这个问题的最简单的跨浏览器解决方案是使用 height 属性来表示高度百分比.

    更新:这里有更多解决方案:Chrome/Safari 未填充 100% 的 flex 父级高度

    I'm having trouble getting Chrome to pay attention to the flex-basis part of flex: 1 1 25% in a flex-direction: column layout. It works fine in a row layout.

    The snippet below demonstrates the problem: the yellow, blue, and pink bars are flex-basis 50px, 25%, and 75%, shown in both column and row flex directions.

    If you run it in Firefox (or IE11 or Edge) both column and row divide up the area as expected:

    But if you run it in Chrome (47) or Safari (9.0.3), the column layout on the left seems to ignore the flex-basis entirely -- the heights of the bars seem to have no relation to the flex-basis:

    The only difference between left and right is the 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>

    I tried adding height: 100% to .column, which makes Chrome pay attention to the flex-basis, but causes a different problem -- the flex gets bigger than its container:

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

    I gather this is a long-standing webkit bug. Is there any way to work around it? (I'm trying to create some generalized layout components, so hard-coding specific numbers of children or specific pixel heights isn't workable.)

    [EDIT] Here's an additional example showing the general problem (and avoiding the margin and total-greater-than-100% issues in the example above):

    .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>

    解决方案

    Three items to consider:

    这篇关于Chrome 忽略列布局中的 flex-basis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆