Overflow: auto causes vertically centered items to be cut off using Flexbox

前端 未结 1 1859
盖世英雄少女心
盖世英雄少女心 2020-12-11 18:50

The first link is flexbox 2009 model:

http://jsfiddle.net/vLKBV/

相关标签:
1条回答
  • 2020-12-11 19:27

    Unfortunately, the difference between the 2009 and 2012 specifications involves more than just different property names. Implementing incomplete drafts is always a gamble, so its always safer to assume that browsers following the newer spec are the ones with the correct behavior (even if it isn't the behavior you want).

    The beauty of Flexbox is that it offers a lot of different ways to achieve a particular layout. One of the underplayed features of Flexbox is that the value of auto for top and bottom margins really does what it sounds like, and its just what you need here in place of justify-contents/align-items.

    http://codepen.io/cimmanon/pen/DEnHh

    html, body {
      background: #000;
      width: 100%;
      height: 100%;
      margin: 0;
    }
    
    div.container {
      display: -webkit-box;
      display: -moz-box;
      display: -webkit-flexbox;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      height: 100%;
    }
    
    div.side {
      background: #F00;
      width: 50px;
    }
    
    div.center {
      display: -webkit-box;
      display: -moz-box;
      display: -webkit-flexbox;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      -webkit-box-align: center;
      -moz-box-align: center;
      -webkit-box-pack: center;
      -moz-box-pack: center;
      -webkit-box-flex: 1;
      -moz-box-flex: 1;
      -webkit-flex: 1;
      -ms-flex: 1;
      flex: 1;
      overflow-Y: scroll;
      background: #0f0;
    }
    
    div.child {
      margin: auto;
      background: white;
      width: 10em;
    }
    
    <div class="container">
      <div class="side"></div>
      <div class="center">
        <div class="child">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porta elit vel ante hendrerit facilisis. Curabitur aliquam sollicitudin diam, id posuere elit consectetur nec.
        </div>
      </div>
      <div class="side"></div>
    </div>
    
    0 讨论(0)
提交回复
热议问题