Flexbox不在IE中垂直居中

Dra*_*eer 106 css internet-explorer css3 flexbox internet-explorer-11

我有一个简单的网页,其中包含一些以页面为中心的Lipsum内容.该页面在Chrome和Firefox中运行良好.如果我缩小窗口的大小,内容将填充窗口,直到它不能,然后添加滚动条并将内容从屏幕填充到底部.

但是,该页面不在IE11中.我可以通过弯曲身体让页面在IE中居中,但是如果我这样做,那么内容开始从屏幕上移到顶部并切断内容的顶部.

以下是两种情况.查看相关的小提琴.如果问题不明显,请减小结果窗口的大小,以便强制生成滚动条.

注意:此应用程序仅针对最新版本的Firefox,Chrome和IE(IE11),它们都支持Flexbox候选推荐标准,因此功能兼容性不应成为问题(理论上).

编辑:使用全屏API全屏显示外部div时,所有浏览器都使用原始CSS正确居中.但是,在离开全屏模式时,IE将恢复为水平居中和垂直顶部对齐.

编辑:IE11使用非供应商特定的Flexbox实现.灵活的框("Flexbox")布局更新


在Chrome/FF中正确调整中心和调整大小,在IE11中无法居中但正确调整大小

小提琴:http://jsfiddle.net/Dragonseer/3D6vw/

html, body
{
    height: 100%;
    width: 100%;
}

body
{
    margin: 0;
}

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

.inner
{
    width: 80%;
}
Run Code Online (Sandbox Code Playgroud)

无处不在的中心,在缩小时缩小顶部

小提琴:http://jsfiddle.net/Dragonseer/5CxAy/

html, body
{
    height: 100%;
    width: 100%;
}

body
{
    margin: 0;
    display: flex;
}

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

.inner
{
    width: 80%;
}
Run Code Online (Sandbox Code Playgroud)

Kal*_*tov 208

我发现浏览器在垂直对齐内部容器时有问题,只设置了最小高度样式或者根本没有高度样式.我所做的是添加具有一定价值的高度风格,并为我解决问题.

例如 :

.outer
    {
       display: -ms-flexbox;
       display: -webkit-flex;
       display: flex;

       /* Center vertically */
       align-items: center;

       /*Center horizontaly */
       justify-content: center;

       /*Center horizontaly ie */
       -ms-flex-pack: center;

        min-height: 220px; 
        height:100px;
    }
Run Code Online (Sandbox Code Playgroud)

所以现在我们有高度风格,但是最小高度会覆盖它.那种方式即很高兴,我们仍然可以使用最小高度.

希望这对某人有帮助.

  • 当内容大于最小高度时,此解决方案不起作用.``min-height``确实覆盖了``height``规则,但是如果内容超过``min-height``,它将简单地延伸到边界之外.[@icicodes的回答](http://stackoverflow.com/a/33222765/2093734)解决了这个问题,该解决方案适用于所有支持flex的浏览器. (23认同)
  • 不要忘记`-ms-flex-align:center;`对于IE10.[支持`align-items`仅在IE11中添加](http://msdn.microsoft.com/en-us/library/ie/dn265027%28v=vs.85%29.aspx). (8认同)
  • 用IE11 / Win7 / win10为我工作谢谢 (2认同)
  • 对于 2019 年或之后来到这里的任何人,到目前为止最好的解决方案是 @sergey-fedirko 的解决方案,不需要额外的 HTML 元素和硬高度;) 这个:/sf/answers/3835725771/ (2认同)

eri*_*des 61

尝试在flexboxed flex-direction: column的容器中包装你有的flexboxed div .

我刚刚在IE11中对此进行了测试,它确实有效.一个奇怪的修复,但直到微软将其内部错误修复为外部......它必须要做!

HTML:

<div class="FlexContainerWrapper">
    <div class="FlexContainer">
        <div class="FlexItem">
            <p>I should be centered.</p>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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%;
}
Run Code Online (Sandbox Code Playgroud)

您在IE11中测试的2个示例:http : //codepen.io/philipwalton/pen/JdvdJE http://codepen.io/chriswrightdesign/pen/emQNGZ/

  • 到目前为止,这是此bug的最佳解决方案.所有其他答案都表明容器上的固定高度,如果内容的高度可变,则不起作用.这个解决方案允许使用``min-height``而不是硬``height``规则,同时仍然在所有支持使用``display:flex``的浏览器上垂直居中. (9认同)

小智 11

来自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;
}
Run Code Online (Sandbox Code Playgroud)


Gal*_*mor 6

以防有人带着我遇到的同样问题来到这里,这在以前的评论中没有具体说明.

margin: auto在内部元素上使它粘在它的父母(或外部元素)的底部.为我修好的是将保证金更改为margin: 0 auto;.


Ser*_*rko 6

这是我的工作解决方案(SCSS):

.item{
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 120px;
  &:after{
    content:'';
    min-height:inherit;
    font-size:0;
  }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ica 5

这是一个已知的错误,似乎已在Microsoft内部修复.

https://connect.microsoft.com/IE/feedback/details/802625/min-height-and-flexbox-flex-direction-column-dont-work-together-in-ie-10-11-preview

  • 但它显然没有在外部修复. (38认同)
  • "此问题似乎已在Microsoft Edge中得到修复.我们目前还没有处理安全相关问题之外的Internet Explorer中的功能错误." 他们已经关闭了IE pre-Edge版本的问题 (4认同)

Uma*_*han 3

我已经更新了两个小提琴。我希望它能让你完成工作。

定心

    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;
}
Run Code Online (Sandbox Code Playgroud)

居中并滚动

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;
    }
Run Code Online (Sandbox Code Playgroud)