【问题标题】:Flexbox Not Centering Vertically in IEFlexbox 在 IE 中没有垂直居中
【发布时间】:2013-10-22 16:35:00
【问题描述】:

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

但是,页面不在 IE11 中居中。我可以通过弯曲正文使页面在 IE 中居中,但如果我这样做了,那么内容就会开始向顶部移出屏幕并切断内容的顶部。

以下是两种情况。请参阅相关的小提琴。如果问题没有立即显现,请减小结果窗口的大小,使其强制生成滚动条。

注意:此应用程序仅针对最新版本的 Firefox、Chrome 和 IE (IE11),它们都支持 Candidate Recommendation of Flexbox,因此功能兼容性应该不是问题(理论上)。

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

编辑:IE11 使用非供应商特定的 Flexbox 实现。 Flexible box ("Flexbox") layout updates


在 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%;
}

在任何地方都正确居中,缩小时从顶部切掉

小提琴: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%;
}

【问题讨论】:

  • 我目前无法访问IE11,您是否尝试过这个解决方案:stackoverflow.com/questions/16122341/…
  • margin:auto 似乎没有什么不同。
  • 请向微软报告错误,至少他们在正式发布时会少一个错误,因为 IE 总是会有一些东西(许多独特的错误)需要处理..
  • IE 需要使用-ms-flexbox..
  • IE11 使用当前的 Flexbox 标准 display:flex。 msdn.microsoft.com/en-us/library/ie/dn265027(v=vs.85).aspx。无论如何我都试过了,但并没有什么不同;内容仍然是顶部对齐的。另外,请注意我在原始问题中的编辑,其中指出外部 div 在全屏时正确居中,但在全屏时顶部对齐。

标签: internet-explorer css flexbox internet-explorer-11


【解决方案1】:

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

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;
    }

【讨论】:

  • 感谢您的意见。不幸的是,您的 Fiddles 似乎无法在 Chrome 或 IE11 中运行。居中示例在 Chrome 中不再居中,并且居中和滚动示例在 IE11 中未居中,仍然会截断内容。
【解决方案2】:

我对@9​​87654322@ 没有太多经验,但在我看来,htmlbody 标签上的强制高度会导致文本在调整大小时消失在顶部——我无法做到在 IE 中测试,但我在 Chrome 中发现了相同的效果。

我分叉了你的小提琴并删除了 heightwidth 声明。

body
{
    margin: 0;
}

似乎flex 设置也必须应用于其他flex 元素。但是,将display: flex 应用于.inner 会导致问题,因此我将.inner 显式设置为display: block,并将.outer 设置为flex 以进行定位。

我设置了最小的.outer高度来填充视口,display: flex,并设置了水平和垂直对齐方式:

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

我将.inner 明确设置为display: block。在 Chrome 中,它看起来像是继承自 .outerflex。我也设置了宽度:

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

这解决了 Chrome 中的问题,希望它在 IE11 中也能做到这一点。这是我的小提琴版本:http://jsfiddle.net/ToddT/5CxAy/21/

【讨论】:

  • 感谢您的回复。高度和宽度声明对于在 IE 中垂直居中是必需的。没有它,内容不再以 IE11 为中心。
  • 即使在外部 div 上设置了高度?也许给身体添加一个最小高度?
【解决方案3】:

我发现 ie 浏览器在垂直对齐内部容器时存在问题,当只设置了最小高度样式或根本没有高度样式时。我所做的是添加具有一些价值的高度样式,这为我解决了这个问题。

例如:

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

所以现在我们有了高度样式,但是最小高度会覆盖它。这样 ie 很高兴,我们仍然可以使用 min-height。

希望这对某人有帮助。

【讨论】:

  • 不要忘记-ms-flex-align:center; 用于 IE10。 Support for align-items was added only in IE11.
  • 啊!几个月后,这救了我 - 设置一个高度而不是最小高度。
  • @KaloyanStamatov 查看autoprefixer 并帮助您摆脱这些恼人的跨浏览器问题。
  • 当内容大于最小高度时,此解决方案不起作用。 min-height 确实覆盖了height 规则,但如果内容超过min-height,它将简单地扩展到边界之外。 answer by @ericodes 解决了这个问题,并且该解决方案适用于所有支持 flex 的浏览器。
  • 对于 2019 年或之后来到这里的任何人,迄今为止最好的解决方案是 @sergey-fedirko 的解决方案,不需要额外的 HTML 元素和硬高度;)这个:stackoverflow.com/a/54796082/134120
【解决方案4】:

【讨论】:

  • 它显然没有在外部修复。
  • “此问题似乎已在 Microsoft Edge 中修复。我们目前不处理 Internet Explorer 中除安全相关问题之外的功能错误。”他们已经解决了 IE pre-Edge 版本的问题
  • 如果可以的话,解决方案似乎是使用高度而不是最小高度。
【解决方案5】:

以防万一有人遇到与我相同的问题,以前的 cmets 中没有具体解决。

我在内部元素上有margin: auto,导致它粘在其父元素(或外部元素)的底部。 为我解决的问题是将边距更改为margin: 0 auto;

【讨论】:

    【解决方案6】:

    尝试将您使用flex-direction: column flexboxed 的任何div 包装在一个也被flexboxed 的容器中。

    我刚刚在 IE11 中测试了它,它可以工作。一个奇怪的修复,但直到微软将他们的内部错误修复外部......它必须这样做!

    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 个示例供您在 IE11 中测试: http://codepen.io/philipwalton/pen/JdvdJE http://codepen.io/chriswrightdesign/pen/emQNGZ/

    【讨论】:

    • 您应该在实际答案中发布用于解决问题的代码,而不是链接到外部 URL。那么即使链接失效,您的回答也会继续提供帮助。
    • 这是迄今为止解决此错误的最佳方法。所有其他答案都建议容器上的固定高度,如果内容的高度可变,则该高度不起作用。此解决方案允许使用min-height 而不是硬性height 规则,同时仍垂直居中于所有支持使用display: flex 的浏览器。
    • 包装上只需要display:flex,而孩子可能需要其中一个width: 100%flex-basis:100%flex-grow:1,具体取决于您的内容/风格.
    • 如果设置了 FlexContainerWrapper 属性 align-items,则此解决方法不起作用。您必须用没有此属性的 flexbox 包装原始 flexbox。
    • 对于 2019 年或之后来到这里的任何人,迄今为止最好的解决方案是 @sergey-fedirko 的解决方案,不需要额外的 HTML 元素和硬高度;)这个:stackoverflow.com/a/54796082/134120
    【解决方案7】:

    如果您可以定义父级的宽度和高度,则有一种更简单的方法来集中图像,而无需为其创建容器。

    由于某种原因,如果你定义了最小宽度,IE 也会识别最大宽度。

    此解决方案适用于 IE10+、Firefox 和 Chrome。

    <div>
      <img src="http://placehold.it/350x150"/>
    </div>
    
    div {
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: center;
        justify-content: center;
        -ms-flex-align: center;
        align-items: center;
        border: 1px solid orange;
        width: 100px;
        height: 100px;
    }
    
    img{
      min-width: 10%;
      max-width: 100%;
      min-height: 10%;
      max-height: 100%;
    }
    

    https://jsfiddle.net/HumbertoMendes/t13dzsmn/

    【讨论】:

      【解决方案8】:

      找到了一个对我有用的好解决方案,请查看此链接https://codepen.io/chriswrightdesign/pen/emQNGZ/?editors=1100 首先,我们添加一个父 div,其次我们将 min-height:100% 更改为 min-height:100vh。它就像一个魅力。

      // 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;
      }  
      

      【讨论】:

        【解决方案9】:

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

        .item{
          display: flex;
          justify-content: space-between;
          align-items: center;
          min-height: 120px;
          &:after{
            content:'';
            min-height:inherit;
            font-size:0;
          }
        }
        

        【讨论】:

        • 这是唯一对我有用的解决方案,我在 React 应用程序中使用 Emotion for css。
        • 对于来到这里的每个人,尝试这个并看到元素太高,检查你的弹性项目是否有填充或边框。如果是,请手动设置后元素的最小高度,并从那里的最小高度中减去填充和边框。
        【解决方案10】:

        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;
        }
        

        【讨论】:

        • 我不敢相信这真的有效。顺便说一句,“font-size: 0”可以省略!
        猜你喜欢
        • 2019-07-19
        • 2016-09-10
        • 2019-02-25
        相关资源
        最近更新 更多