HTML Flex 布局在 Internet Explorer 中看起来不同

HTML Flex layout looks different in Internet Explorer

我正在尝试让一个简单的弹性布局在 IE11 中像在 Chrome 中一样工作。

在Chrome中它看起来像这样:

然而在 IE11 中它是这样的:

这是代码:

html,
body {
  margin: 2px 0 0 0;
  padding: 0;
}

.wrap {
  display: flex;
  min-height: calc( 100vh - 8px);
  flex-direction: column;
  max-width: 1200px;
  margin: auto;
  border: 1px solid #222222;
}

.main {
  flex: 1 1 auto;
  display: flex;
}

header {
  background: green;
  padding: 10px;
}

#footer {
  background: green;
  padding: 10px;
}

.sidebar {
  background: blue;
  flex: 0 0 135px;
  padding: 10px;
}

.content {
  background: yellow;
  padding: 10px;
  flex: 1 1 auto;
}

@media screen and (max-width:680px) {
  .sidebar {
    flex: 0;
    order: 2
  }
  .main {
    flex-direction: column;
  }
}
<body translate="no">


  <div class="wrap">


    <header>

      <div class="header-inner-left">
      </div>

      <div class="header-inner">
      </div>

    </header>

    <main class="main">


      <div class="sidebar">

      </div>



      <div class="content">


      </div>


    </main>



    <div id="footer">
      <div class='footerleft'>

      </div>
      <div class='footerright'>

      </div>
      <div style="clear: both;"></div>
    </div>



  </div>

</body>

即显示 HTML & CSS。 有没有办法让它看起来一样呢

如果我设置 .wrap 那么它会使用:

height: calc( 100vh -  8px );

然后加载页面看起来是正确的,但我需要内容能够正确地超出 .wrap 范围,所以设置 min-height: calc( 100vh - 8px );

有什么办法吗? 谢谢

.wrap 你真的需要 display: flex 吗?因为在 https://caniuse.com/?search=calc 上你会发现“据报道 IE 和 Edge 在 'flex' 中不支持 calc”。

因此,如果您改用 display: block,它可能适用于 IE。在 .wrap 的 CSS 规则中,我看到您有 flex-direction: column;,但该规则中没有其他设置会对使用 display: block.[= 产生实际影响18=]

这里有一个可能对您有用的想法:

body {
  display: flex;
}

.wrap {
  min-height: 100vh;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  max-width: 1200px;
  border: 1px solid #222222;
}

.main {
  flex: 1 1 auto;
  display: flex;
}

header {
  background: green;
  flex: 0 0 25px;
}

#footer {
  background: green;
  flex: 0 0 25px;
}

.sidebar {
  background: blue;
  flex: 0 0 135px;
  padding: 10px;
}

.content {
  background: yellow;
  padding: 10px;
  flex: 1 1 auto;
}

body {
  margin: 0;
}

* {
  box-sizing: border-box;
}
<div class="wrap">
  <header>
    <div class="header-inner-left"></div>
    <div class="header-inner"></div>
  </header>
  <main class="main">
    <div class="sidebar"></div>
    <div class="content"></div>
  </main>
  <div id="footer">
    <div class='footerleft'></div>
    <div class='footerright'></div>
    <div style="clear: both;"></div>
  </div>
</div>