Chrome / Safari not filling 100% height of flex parent

I want to have a vertical menu with a specific height.

Each child must fill the height of the parent and have middle-aligned text.

The number of children is random, so I have to work with dynamic values.

Div .container contains a random number of children ( .item ) that always have to fill the height of the parent. To achieve that I used flexbox.

For making links with text aligned to the middle I am using display: table-cell technique. But using table displays requires using a height 100%.

My problem is that .item-inner { height: 100% } is not working in webkit (Chrome).

  • Is there a fix for this problem?
  • Or is there a different technique to make all .item fill the height of the parent with text vertical aligned to middle?
  • Example here jsFiddle, should be viewed in Firefox and Chrome

    .container {
      height: 20em;
      display: flex;
      flex-direction: column;
      border: 5px solid black
    }
    .item {
      flex: 1;
      border-bottom: 1px solid white;
    }
    .item-inner {
      height: 100%;
      width: 100%;
      display: table;
    }
    a {
      background: orange;
      display: table-cell;
      vertical-align: middle;
    }
    <div class="container">
      <div class="item">
        <div class="item-inner">
          <a>Button</a>
        </div>
      </div>
    
      <div class="item">
        <div class="item-inner">
          <a>Button</a>
        </div>
      </div>
    
      <div class="item">
        <div class="item-inner">
          <a>Button</a>
        </div>
      </div>
    </div>

    The Problem

    My problem is that .item-inner { height: 100% } is not working in webkit (Chrome).

    It's not working because you're using percentage height in a way that doesn't conform with the traditional implementation of the spec.

    10.5 Content height: the 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.

    In other words, for percentage height to work on an in-flow child, the parent must have a set height.

    In your code, the top-level container has a defined height: .container { height: 20em; } .container { height: 20em; }

    The third-level container has a defined height: .item-inner { height: 100%; } .item-inner { height: 100%; }

    But between them, the second-level container – .item – does not have a defined height. Webkit sees that as a missing link.

    .item-inner is telling Chrome: give me height: 100% . Chrome looks to the parent ( .item ) for reference and responds: 100% of what? I don't see anything (ignoring the flex: 1 rule that is there). As a result, it applies height: auto (content height), in accordance with the spec.

    Firefox, on the other hand, now accepts a parent's flex height as a reference for the child's percentage height. IE11 and Edge accept flex heights, as well.

    Also, Chrome will accept flex-grow as an adequate parent reference if used in conjunction with flex-basis (any value works, including flex-basis: 0 ). As of this writing, however, this solution fails in Safari.

    #outer {
      display: flex;
      flex-direction: column;
      height: 300px;
      background-color: white;
      border: 1px solid red;
    }
    #middle {
      flex-grow: 1;
      flex-basis: 1px;
      background-color: yellow;
    }
    #inner {
      height: 100%;
      background-color: lightgreen;
    }
    <div id="outer">
      <div id="middle">
        <div id="inner">
          INNER
        </div>
      </div>
    </div>

    Solution: Remove height: 100% in .item-inner and add display: flex in .item

    Demo: https://codepen.io/tronghiep92/pen/NvzVoo

    链接地址: http://www.djcxy.com/p/75644.html

    上一篇: 高度应用程序提供第二个滚动条

    下一篇: Chrome / Safari未填充flex父级的100%高度