Flexbox 和 Internet Explorer 11(<html> 中的 display:flex?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21600345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:49:49  来源:igfitidea点击:

Flexbox and Internet Explorer 11 (display:flex in <html>?)

htmlcssflexboxcross-browser

提问by Kodekan

I am planning to move away from "floaty" layouts and use CSS flexbox for future projects. I was delighted to see that all major browsers in their current versions seem to support (in one way or another) flexbox.

我计划摆脱“浮动”布局,并在未来的项目中使用 CSS flexbox。我很高兴看到当前版本的所有主要浏览器似乎都支持(以一种或另一种方式)flexbox。

I headed over to "Solved by Flexbox"to look at some examples. However the "Sticky Footer" example does not seem to work in Internet Explorer 11. I played around a bit and got it to work by adding display:flexto the <html>and width:100%to the <body>

我前往“由 Flexbox 解决”来查看一些示例。然而,“置顶页脚”的例子似乎并没有在Internet Explorer 11.我的工作起到了一下周围,并把它加了工作display:flex<html>width:100%<body>

So my first question is: Can anybody explain that logic to me? I just fiddled around and it worked, but I don't quite understand whyit worked that way...

所以我的第一个问题是:有人可以向我解释这个逻辑吗?我只是摆弄了一下,它起作用了,但我不太明白为什么它会这样工作......

Then there is the "Media Object" example that works in all browsers except for - you guessed it - Internet Explorer. I fiddled around with that, too, but without any success.

然后是“媒体对象”示例,它适用于所有浏览器,除了 - 您猜对了 - Internet Explorer。我也摆弄过这个,但没有任何成功。

My second question therefore is: Is there a "clean" possibility to get the "Media Object" example working in Internet Explorer?

因此,我的第二个问题是:是否有“干净”的可能性让“媒体对象”示例在 Internet Explorer 中工作?

回答by Odisseas

According to http://caniuse.com/#feat=flexbox:

根据http://caniuse.com/#feat=flexbox

"IE10 and IE11 default values for flexare 0 0 autorather than 0 1 auto, as per the draft spec, as of September 2013"

“IE10和IE11的默认值flex0 0 auto而非0 1 auto,按照该规范草案,2013年9月的”

So in plain words, if somewhere in your CSS you have something like this: flex:1, that is not translated the same way in all browsers. Try changing it to 1 0 0and I believe you will immediately see that it -kinda- works.

所以简单地说,如果你的 CSS 中的某个地方有这样的内容:flex:1,在所有浏览器中的翻译方式都不同。尝试将其更改为1 0 0,我相信您会立即看到它 - 有点 - 有效。

The problem is that this solution will probably mess up firefox, but then you can use some hacks to target only Mozilla and change it back:

问题是这个解决方案可能会搞砸 firefox,但是你可以使用一些 hacks 来只针对 Mozilla 并将其改回来:

@-moz-document url-prefix() {
 #flexible-content{
      flex: 1;
    }
}

Since flexboxis a W3C Candidate and not official, browsers tend to give different results, but I guess that will change in the immediate future.

由于flexbox是 W3C 候选者而非官方,浏览器往往会给出不同的结果,但我想这会在不久的将来发生变化。

If someone has a better answer I would like to know!

如果有人有更好的答案,我想知道!

回答by BeliG

Use another flex container to fix the min-heightissue in IE10 and IE11:

使用另一个 flex 容器修复min-heightIE10 和 IE11 中的问题:

HTML

HTML

<div class="ie-fixMinHeight">
    <div id="page">
        <div id="header"></div>
        <div id="content"></div>
        <div id="footer"></div>
    </div>
</div>

CSS

CSS

.ie-fixMinHeight {
    display:flex;
}

#page {
    min-height:100vh;
    width:100%;
    display:flex;
    flex-direction:column;
}

#content {
    flex-grow:1;
}

See a working demo.

请参阅工作演示

  • Don't use flexbox layout directly on bodybecause it screws up elements inserted via jQuery plugins (autocomplete, popup, etc.).
  • Don't use height:100%or height:100vhon your container because the footer will stick at the bottom of window and won't adapt to long content.
  • Use flex-grow:1rather than flex:1cause IE10 and IE11 default values for flexare 0 0 autoand not 0 1 auto.
  • 不要直接使用 flexbox 布局,body因为它会搞砸通过 jQuery 插件(自动完成、弹出等)插入的元素。
  • 不要在您的容器上使用height:100%height:100vh,因为页脚会粘在窗口的底部并且不会适应长内容。
  • 使用flex-grow:1而不是flex:1导致 IE10 和 IE11 的默认值flexare0 0 auto和 not 0 1 auto

回答by Ashok Kumar Gupta

You just need flex:1; It will fix issue for the IE11. I second Odisseas. Additionally assign 100% height to html,body elements.

你只需要flex:1; 它将修复 IE11 的问题。我第二个奥德赛斯。另外为 html,body 元素分配 100% 高度

CSS changes:

CSS变化:

html, body{
    height:100%;
}
body {
    border: red 1px solid;
    min-height: 100vh;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -ms-flex-direction: column;
    -webkit-flex-direction: column;
    flex-direction: column;
}
header {
    background: #23bcfc;
}
main {
    background: #87ccfc;
    -ms-flex: 1;
    -webkit-flex: 1;
    flex: 1;
}
footer {
    background: #dd55dd;
}

working url: http://jsfiddle.net/3tpuryso/13/

工作网址:http: //jsfiddle.net/3tpuryso/13/

回答by BadTree Lu

Here is an example of using flex that also works in Internet Explorer 11 and Chrome.

下面是一个使用 flex 的示例,该示例也适用于 Internet Explorer 11 和 Chrome。

HTML

HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" >
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Flex Test</title>
<style>
html, body {
    margin: 0px;
    padding: 0px;
    height: 100vh;
}
.main {
    display: -webkit-flex;
    display: flex;
    -ms-flex-direction: row;
    flex-direction: row;
    align-items: stretch;
    min-height: 100vh;
}

.main::after {
  content: '';
  height: 100vh;
  width: 0;
  overflow: hidden;
  visibility: hidden;
  float: left;
}

.left {
    width: 200px;
    background: #F0F0F0;
    flex-shrink: 0;
}

.right {
    flex-grow: 1;
    background: yellow;
}
</style>
</head>

<body>
<div class="main">
    <div class="left">
        <div style="height: 300px;">
        </div>
    </div>

    <div class="right">
        <div style="height: 1000px;">
            test test test
        </div>
    </div>
</div>
</body>
</html>

回答by Marsel Gray

Sometimes it's as simple as adding: '-ms-' in front of the style Like -ms-flex-flow: row wrap; to get it to work also.

有时就像在样式前添加:'-ms-' 一样简单,如 -ms-flex-flow: row wrap; 让它也工作。