Flexbox Not Centering Vertically in IE

前端 未结 10 646
自闭症患者
自闭症患者 2020-12-04 08:15

I have a simple web page with some Lipsum content that is centered on the page. The page works fine in Chrome and Firefox. If I reduce the size of the window, the content fi

相关标签:
10条回答
  • 2020-12-04 08:30

    I don't have much experience with Flexbox but it seems to me that the forced height on the html and body tags cause the text to disappear on top when resized-- I wasn't able to test in IE but I found the same effect in Chrome.

    I forked your fiddle and removed the height and width declarations.

    body
    {
        margin: 0;
    }
    

    It also seemed like the flex settings must be applied to other flex elements. However, applying display: flex to the .inner caused issues so I explicitly set the .inner to display: block and set the .outer to flex for positioning.

    I set the minimum .outer height to fill the viewport, the display: flex, and set the horizontal and vertical alignment:

    .outer
    {
        display:flex;
        min-height: 100%;
        min-height: 100vh;
        align-items: center;
        justify-content: center;
    }
    

    I set .inner to display: block explicitly. In Chrome, it looked like it inherited flex from .outer. I also set the width:

    .inner
    {
        display:block;
        width: 80%;
    }
    

    This fixed the issue in Chrome, hopefully it might do the same in IE11. Here's my version of the fiddle: http://jsfiddle.net/ToddT/5CxAy/21/

    0 讨论(0)
  • 2020-12-04 08:30

    Found a good solution what worked for me, check this link https://codepen.io/chriswrightdesign/pen/emQNGZ/?editors=1100 First, we add a parent div, second we change min-height:100% to min-height:100vh. It works like a charm.

    // by having a parent with flex-direction:row, 
    // the min-height bug in IE doesn't stick around.
    .flashy-content-outer { 
        display:flex;
        flex-direction:row;
    }
    .flashy-content-inner {
        display:flex;
        flex-direction:column;
        justify-content:center;
        align-items:center;
        min-width:100vw;
        min-height:100vh;
        padding:20px;
        box-sizing:border-box;
    }
    .flashy-content {
        display:inline-block;
        padding:15px;
        background:#fff;
    }  
    
    0 讨论(0)
  • 2020-12-04 08:33

    Here is my working solution (SCSS):

    .item{
      display: flex;
      justify-content: space-between;
      align-items: center;
      min-height: 120px;
      &:after{
        content:'';
        min-height:inherit;
        font-size:0;
      }
    }
    
    0 讨论(0)
  • 2020-12-04 08:33

    The original answer from https://github.com/philipwalton/flexbugs/issues/231#issuecomment-362790042

    .flex-container{
    min-height:100px;
    display:flex;
    align-items:center;
    }
    
    .flex-container:after{
    content:'';
    min-height:inherit;
    font-size:0;
    }
    
    0 讨论(0)
  • 2020-12-04 08:35

    Try wrapping whatever div you have flexboxed with flex-direction: column in a container that is also flexboxed.

    I just tested this in IE11 and it works. An odd fix, but until Microsoft makes their internal bug fix external...it'll have to do!

    HTML:

    <div class="FlexContainerWrapper">
        <div class="FlexContainer">
            <div class="FlexItem">
                <p>I should be centered.</p>
            </div>
        </div>
    </div>
    

    CSS:

    html, body {
      height: 100%;
    }
    
    .FlexContainerWrapper {
      display: flex;
      flex-direction: column;
      height: 100%;
    }
    
    .FlexContainer {
      align-items: center;
      background: hsla(0,0%,0%,.1);
      display: flex;
      flex-direction: column;
      justify-content: center;
      min-height: 100%;
      width: 600px;
    }
    
    .FlexItem {
      background: hsla(0,0%,0%,.1);
      box-sizing: border-box;
      max-width: 100%;
    }
    

    2 examples for you to test in IE11: http://codepen.io/philipwalton/pen/JdvdJE http://codepen.io/chriswrightdesign/pen/emQNGZ/

    0 讨论(0)
  • 2020-12-04 08:35

    i have updated both fiddles. i hope it will make your work done.

    centering

        html, body
    {
        height: 100%;
        width: 100%;
    }
    
    body
    {
        margin: 0;
    }
    
    .outer
    {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .inner
    {
        width: 80%;
        margin: 0 auto;
    }
    

    center and scroll

    html, body
        {
            height: 100%;
            width: 100%;
        }
    
        body
        {
            margin: 0;
            display:flex;
        }
    
        .outer
        {
            min-width: 100%;  
            display: flex;
            align-items: center;
            justify-content: center;
        }
    
        .inner
        {
            width: 80%;
        margin-top:40px;
        margin: 0 auto;
        }
    
    0 讨论(0)
提交回复
热议问题